Post

Gaussian quadrature

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

I=11\ddxf(x)n=1Nwnf(xn)

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:

I11\ddxf(x)=11\ddxp=0f(p)(0)p!xp=p=0f(p)(0)p!11\ddxxpn=1Nwnf(xn)=n=1Nwnp=0f(p)(0)p!xnp=p=0f(p)(0)p!n=1Nwnxnp

It is exact if for any $p\in\mathbb{N}$, there exists

n=1Nwnxnp=11\ddxxp

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

f(x)=Q(x)PN(x)+R(x)

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

11\ddxPi(x)Pj(x)=22i+1δij,

$P_N$ is orthogonal to $Q$ and $R$, therefore the formal integral

I11\ddxf(x)=11\ddx[Q(x)PN(x)+R(x)]=11\ddxR(x)

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$

n=1Nwnf(xn)=n=1Nwn[Q(xn)PN(xn)+R(xn)]=n=1NwnR(xn)

These two equations are the same as Eq. (???) except with $R(x)$ rather than $f(x)$. Since now the nodes are settled down, we only have to solve the weights. They can be solved by letting

f(x)=PN(x)PN(x)xxn

where $x_n$ is the $n$-th root of $P_N(x)$, and $P’_N$ being its derivative. Insert it to the $n$-series

n=1Nwnf(xn)=n=1,nnNwnPN(xn)PN(xn)xnxn+wnlimxxnPN(x)PN(x)xxn

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

limxxnPN(x)PN(x)xxn=limxxn[PN(x)PN(x)]=PN(xn)PN(xn)+PN(xn)PN(xn)=[PN(xn)]2

Therefore

n=1Nwnf(xn)=wn[PN(xn)]2

On the other hand, the integral

11\ddxf(x)=11\ddxPN(x)PN(x)xxn=11\dd[PN(x)]PN(x)xxn=PN2(x)xxn|1111PN(x)\ddPN(x)xxn

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

11\ddxf(x)=PN2(x)xxn|11=PN2(1)1xn+PN2(1)1+xn=21xn2

where we have used the property of Legendre polynomials: $P_N(1)=1, P_{N}(-1) = (-1)^N$. Equating Eqs. (???) and (???) results in

wn=2(1xn2)[PN(xn)]2

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

x=ba2t+a+b2,

therefore

I=ab\ddxf(x)=ba211\ddtf(ba2t+a+b2)=ba211\ddtg(t).

The $t$ integral can then be approximated by the Gauss-Legendre quadrature

Iba2n=1Nwng(xn)=n=1N(ba2wn)f(ba2xn+a+b2)

Therefore for interval $[a,b]$, the nodes and weights are mapped to

wnba2wnxnba2xn+a+b2

It can be also extended to some open intervals. For example,

x=1+t1t+c

can be used for semi-infinite integral $[c, \infty)$. In this case,

I=0\ddxf(x)=0\dd(1+t1t)f(1+t1t+c)=11\ddt2(1t)2f(1+t1t+c)n=1N2wn(1xn)2f(1+xn1xn+c)

therefore

wn2(1xn)2wnxn1+xn1xn+c

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

References

[1] R. W. Godby, M. Schlüter, and L. J. Sham, Phys. Rev. B 37, 10159 (1988) [DOI].

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.