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

(1)I=11dxf(x)n=1Nwnf(xn)

where xn[1,1] with integration weights wn. An N-point grid with fixed {xn} points can be constructed to ensure exact integration for functions with Taylor polynomials up to order N1. This can be shown by comparing the expansion of the exact and approximated forms:

(2)I11dxf(x)=11dxp=0f(p)(0)p!xp=p=0f(p)(0)p!11dxxpn=1Nwnf(xn)=n=1Nwnp=0f(p)(0)p!xnp=p=0f(p)(0)p!n=1Nwnxnp

It is exact if for any pN, there exists

(3)n=1Nwnxnp=11dxxp

For a fixed {xn}, this is equivalent to solving a set of linear equations for {ωn}. A unique solution can be found when p is truncated at N1, i. e. N equations for N variables.

If we relax the constraint of fixed nodes {xn} and treat them as unknown variables, the degrees of freedom becomes 2N and p needs to go to 2N1. This leads to a better estimate of an integral, as it now can integrate exactly polynomials up to order of 2N1. It is proposed by Gauss and hence called Gaussian quadrature.

Gauss-Legendre quadrature

To determine the nodes {xn} and weights {wn} for Gaussian quadrature, we consider the integration of polynomials up to 2N1 order. Such function can be written in the following form

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

where PN(x) is the N-th-order Legendre polynomial, and Q(x) and R(x) are general polynomials of N1 or less order. According to the orthogonality of Legendre polynomials

(4)11dxPi(x)Pj(x)=22i+1δij,

PN is orthogonal to Q and R, therefore the formal integral

I11dxf(x)=11dx[Q(x)PN(x)+R(x)]=11dxR(x)

For the n-series, we assign the roots of PN(x) to {xn}, so that PN(xn)=0,n=1,N

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

These two equations are the same as Eq. (2) 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 xn is the n-th root of PN(x), and PN 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 nn is zero as we have chosen {xn} be the roots of PN 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

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

On the other hand, the integral

11dxf(x)=11dxPN(x)PN(x)xxn=11d[PN(x)]PN(x)xxn=PN2(x)xxn|1111PN(x)dPN(x)xxn

For the second term, as xn is a root of PN, PN(x)/(xxn) must be a polynomial of order N1. Taking derivative will lead to a polynomial of order N2 in the integrand, which can always be expressed as a linear combination using Legendre polynomials of order no more than N2. Thus the integral vanishes due to their orthogonality with PN, and

(6)11dxf(x)=PN2(x)xxn|11=PN2(1)1xn+PN2(1)1+xn=21xn2

where we have used the property of Legendre polynomials: PN(1)=1,PN(1)=(1)N. Equating Eqs. (5) and (6) results in

(7)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 6x8+4x21. 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

(8)x=ba2t+a+b2,

therefore

I=abdxf(x)=ba211dtf(ba2t+a+b2)=ba211dtg(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

(9)wnba2wnxnba2xn+a+b2

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

(10)x=1+t1t+c

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

I=0dxf(x)=0d(1+t1t)f(1+t1t+c)=11dt2(1t)2f(1+t1t+c)n=1N2wn(1xn)2f(1+xn1xn+c)

therefore

(11)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.