Mikkel Paltorp

Quadrature for one-dimensional integrals

The basic idea behind quadrature schemes is to approximate integrals as follows

\[ \int_{-1}^1 f(x)\ \mathrm{d}x \approx \sum_{i=1}^n w_if(x_i), \]

where \(w_i\) is called weights and \(x_i\) the gauss ponts. Note that the sum of the weights \(w_i\) is equal to length of integral length (\(\sum_i^nw_i = 1-(-1)=2\)). One could argue that looking at the interval \(-1\) to \(1\) is not interesting, but it turns out that that is all you need since through a simple change of variables one can can rewrite an intergral on the domain from \(a\) to \(b\) as

\[ \int_a^b f(x)\ \mathrm{d}x = \frac{b-a}{2}\int_{-1}^1 f\left(\frac{b-a}{2}\xi + \frac{a+b}{2}\right)\ \mathrm{d}\xi. \]

Written differently one could see the above as simply modifying the weights, \(w_i\), and the gauss nodes, \(x_i\), as

\[ \overline{w}_i = \frac{b-a}{2}w_i, \quad \overline{x}_i = \frac{b-a}{2}x_i + \frac{a+b}{2}, \]

such that

\[ \int_a^b f(x)\ \mathrm{d}x \approx \sum_{i=1}^n\overline{w}_if(\overline{x}_i). \]

Example: (Quadrature in 1D) As an example we look at the following integral

\[ \int_0^{2\pi}|\sin(x)|\ \mathrm{d}x = 2(-\cos(\pi)+\cos(0)) = 4, \]

In Julia this integral can be estimated using the following code

using FastGaussQuadrature, LaTeXStrings, LinearAlgebra, Plots
N = 50
a = 0 
b = 2*pi
xi,wi = gausslegendre(N)
f(x) = abs.(sin.(x))
xbar = 0.5*(b-a)*xi .+ 0.5*(b+a)
wbar = 0.5*(b-a)*wi
wbar'*f(xbar)
4.003187673366941

Quadrature for two-dimensional integrals

For two-dimensional integrals the basic idea is integrals of the form

\[ \int_{-1}^1\int_{-1}^1 f(x,y)\ \mathrm{d}x\mathrm{d}y \approx \sum_{i=1}^n w_if(x_i,y_i). \]

In this case we have that the sum of the weights is equal to the area of domain of the integral (\(\sum_{i=1}^nw_i = (1-(-1))(1-(-1))=4\)). Again using change of variables (in both \(x\) and \(y\)) one find that

\[ \int_{c}^d\int_a^b f(x)\ \mathrm{d}x = \frac{b-a}{2}\frac{d-c}{2}\int_{-1}^1 f\left(\frac{b-a}{2}\xi + \frac{a+b}{2}, \frac{d-c}{2}\eta + \frac{d+c}{2}\right)\ \mathrm{d}\xi\mathrm{d}\eta. \]

This again means that

\[ \int_{c}^d\int_a^b f(x)\ \mathrm{d}x \approx \sum_{i=1}^n\overline{w}_if(\overline{x}_i, \overline{y}_i), \]

where

\[ \overline{w}_i = \frac{b-a}{2}\frac{d-c}{2}w_i, \quad \overline{x}_i = \frac{b-a}{2}x_i + \frac{a+b}{2}, \quad \overline{y}_i = \frac{d-c}{2}y_i + \frac{d+c}{2}. \]