Gaussian quadrature
Introduction
Numerical integration of function is pervasive in scientific computations. It is also termed as quadrature and is generally evaluated on discretized grids. The simplest example would be
where $x_n \in [-1,1]$ with integration weights $w_n$. An $N$-point grid with fixed $\{x_n\}$ points can be constructed to ensure exact integration for functions with Taylor polynomials up to order $N-1$. This can be shown by comparing the expansion of the exact and approximated forms:
It is exact if for any $p\in\mathbb{N}$, there exists
For a fixed $\{x_n\}$, this is equivalent to solving a set of linear equations for $\{\omega_n\}$. A unique solution can be found when $p$ is truncated at $N-1$, i. e. $N$ equations for $N$ variables.
If we relax the constraint of fixed nodes $\{x_n\}$ and treat them as unknown variables, the degrees of freedom becomes $2N$ and $p$ needs to go to $2N-1$. This leads to a better estimate of an integral, as it now can integrate exactly polynomials up to order of $2N-1$. It is proposed by Gauss and hence called Gaussian quadrature.
Gauss-Legendre quadrature
To determine the nodes $\{x_n\}$ and weights $\{w_n\}$ for Gaussian quadrature, we consider the integration of polynomials up to $2N-1$ order. Such function can be written in the following form
where $P_N(x)$ is the $N$-th-order Legendre polynomial, and $Q(x)$ and $R(x)$ are general polynomials of $N-1$ or less order. According to the orthogonality of Legendre polynomials
$P_N$ is orthogonal to $Q$ and $R$, therefore the formal integral
For the $n$-series, we assign the roots of $P_{N}(x)$ to $\{x_n\}$, so that $P_N(x_n) = 0, \forall n = 1, \cdots N$
These two equations are the same as Eq.
where $x_n$ is the $n$-th root of $P_N(x)$, and $P’_N$ being its derivative. Insert it to the $n$-series
Each term in the summation over $n\ne n’$ is zero as we have chosen $\{x_n\}$ be the roots of $P_{N}$ and the denominator is nonzero. The limit, using L’Hospital’s rule, is
Therefore
On the other hand, the integral
For the second term, as $x_n$ is a root of $P_N$, $P_N(x)/(x-x_n)$ must be a polynomial of order $N-1$. Taking derivative will lead to a polynomial of order $N-2$ in the integrand, which can always be expressed as a linear combination using Legendre polynomials of order no more than $N-2$. Thus the integral vanishes due to their orthogonality with $P_N$, and
where we have used the property of Legendre polynomials: $P_N(1)=1, P_{N}(-1) = (-1)^N$. Equating Eqs.
which is the desired weight.
Gauss-Legendre quadrature is available in SciPy. The roots and weights can be obtained by calling roots_legendre
in the scipy.special
module. Below is an example to integrate $6x^8 + 4x^2 - 1$. The exact integral between $[-1,1]$ is 2.
1
2
3
4
5
6
7
8
9
10
11
import scipy
import numpy as np
def f(x):
return 6. * x ** 8 + 4. * x ** 2 - 1.
for n in [2, 3, 4, 5, 6]:
xs, ws = scipy.special.roots_legendre(n)
integral = np.sum(ws * f(xs))
print("N = {:d}, 2N-1 = {:2d}, integral = {:.10f}"
.format(n, 2 * n - 1, integral))
N = 2, 2N-1 = 3, integral = 0.8148148148
N = 3, 2N-1 = 5, integral = 1.5306666667
N = 4, 2N-1 = 7, integral = 1.9303401361
N = 5, 2N-1 = 9, integral = 2.0000000000
N = 6, 2N-1 = 11, integral = 2.0000000000
Extension to any interval
For any function $f$ defined on closed interval $[a,b]$, we can do the following change of variables
therefore
The $t$ integral can then be approximated by the Gauss-Legendre quadrature
Therefore for interval $[a,b]$, the nodes and weights are mapped to
It can be also extended to some open intervals. For example,
can be used for semi-infinite integral $[c, \infty)$. In this case,
therefore
In practice, the semi-infinite integral can be divided into separate regions, and each region is integrated using its own Gauss-Legendre grids. [1]
See also
- Gaussian quadrature - Wikipedia
- Legendre polynomials - Wikipedia
- Gauss-Legendre quadrature - Wikipedia
roots_legendre
- SciPy Manual
References
[1] R. W. Godby, M. Schlüter, and L. J. Sham, Phys. Rev. B 37, 10159 (1988) [DOI].
Comments powered by Disqus.