TExcelTAD
Home › Numerical Methods › Gaussian Quadrature

Gaussian Quadrature

Newton-Cotes rules fix the nodes at equally spaced points and choose only the weights. If the nodes are also chosen freely, nn points can integrate polynomials up to degree 2n−12n - 1 exactly, twice the degree of an interpolatory rule. Gauss-Legendre quadrature does this, and for smooth functions it converges extremely fast.

The idea

A rule ∫−11f(x) dx≈∑i=1nwif(xi)\int_{-1}^{1}f(x)\,dx \approx \sum_{i=1}^nw_if(x_i) has 2n2n free parameters, enough to be exact for 1,x,…,x2n−11, x, \ldots, x^{2n-1}. The solution: the nodes xix_i are the roots of the Legendre polynomial PnP_n, and the weights are positive. For n=2n = 2: x=±13x = \pm\frac{1}{\sqrt{3}}, w=1w = 1.

python · try it yourself
import numpy as np

x, w = np.polynomial.legendre.leggauss(2)
print("nodes  :", x, " 1/sqrt(3) =", 1 / np.sqrt(3))
print("weights:", w)
for k in range(6):
    exact = (1 - (-1) ** (k + 1)) / (k + 1)
    print(f"x^{k}: rule {np.sum(w * x**k): .6f}, exact {exact: .6f}")
nodes  : [-0.57735027  0.57735027]  1/sqrt(3) = 0.5773502691896258
weights: [1. 1.]
x^0: rule  2.000000, exact  2.000000
x^1: rule  0.000000, exact  0.000000
x^2: rule  0.666667, exact  0.666667
x^3: rule  0.000000, exact  0.000000
x^4: rule  0.222222, exact  0.400000
x^5: rule  0.000000, exact  0.000000

Two points integrate cubics exactly but not x4x^4.

Other intervals

Map [a,b][a, b] to [−1,1][-1, 1] with x=a+b2+b−a2tx = \frac{a + b}{2} + \frac{b - a}{2}t:

∫abf(x) dx≈b−a2∑i=1nwif(a+b2+b−a2ti).\int_a^bf(x)\,dx \approx \frac{b - a}{2}\sum_{i=1}^nw_if\left(\frac{a + b}{2} + \frac{b - a}{2}t_i\right).

Convergence

For analytic integrands the error falls geometrically with nn, much faster than Simpson's O(n−4)O(n^{-4}):

python · try it yourself
import numpy as np
from scipy.integrate import simpson

def gauss(f, a, b, n):
    t, w = np.polynomial.legendre.leggauss(n)
    return (b - a) / 2 * np.sum(w * f((a + b) / 2 + (b - a) / 2 * t))

f = lambda x: np.exp(-x**2)
exact = 0.7468241328124270
print(" n   Gauss error   Simpson error (about n points)")
for n in (3, 5, 7, 9, 11):
    xs = np.linspace(0, 1, n + 1 if n % 2 == 0 else n)
    print(f"{n:2d}   {abs(gauss(f, 0, 1, n) - exact):.2e}      {abs(simpson(f(xs), x=xs) - exact):.2e}")
 n   Gauss error   Simpson error (about n points)
 3   9.55e-06      3.56e-04
 5   6.05e-09      3.12e-05
 7   7.89e-13      6.26e-06
 9   1.11e-16      1.99e-06
11   1.11e-16      8.15e-07

Seven Gauss points give about 12 digits and nine reach machine precision; Simpson's rule with the same number of points is still at 10−610^{-6}.

Weight functions and other families

Gaussian rules exist for integrals with a weight, ∫w(x)f(x) dx\int w(x)f(x)\,dx, with nodes at roots of the corresponding orthogonal polynomials:

Rule Interval and weight NumPy
Gauss-Legendre [−1,1][-1, 1], 11 polynomial.legendre.leggauss
Gauss-Chebyshev [−1,1][-1, 1], 11−x2\frac{1}{\sqrt{1 - x^2}} polynomial.chebyshev.chebgauss
Gauss-Laguerre [0,∞)[0, \infty), e−xe^{-x} polynomial.laguerre.laggauss
Gauss-Hermite (−∞,∞)(-\infty, \infty), e−x2e^{-x^2} polynomial.hermite.hermgauss

Gauss-Hermite rules compute expectations under a normal distribution, a common need in statistics and finance:

python · try it yourself
import numpy as np

x, w = np.polynomial.hermite.hermgauss(20)
# E[cos(Z)] for Z ~ N(0, 1): substitute z = sqrt(2) x
est = np.sum(w * np.cos(np.sqrt(2) * x)) / np.sqrt(np.pi)
print(f"Gauss-Hermite: {est:.15f}, exact exp(-1/2) = {np.exp(-0.5):.15f}")
Gauss-Hermite: 0.606530659712633, exact exp(-1/2) = 0.606530659712633

Limitations

  • Nodes change completely with nn, so results for different nn cannot share function values (Gauss-Kronrod rules fix this, see Adaptive Integration).
  • Convergence is fast only for smooth integrands; singularities and kinks need splitting the interval or special weights.

In software

  • scipy.integrate.fixed_quad(f, a, b, n) is Gauss-Legendre with nn points; quad uses Gauss-Kronrod rules adaptively.
  • scipy.special.roots_legendre, roots_hermite and friends return nodes and weights; for large nn they use fast asymptotic algorithms rather than eigenvalue problems.

Exercises

Exercise 1: Degree of exactness

What is the highest degree integrated exactly by a 5-point Gauss-Legendre rule? By a 5-point Newton-Cotes rule?

Answer

9 and 5.

Solution

Gauss: 2n−1=92n - 1 = 9. A 5-point closed Newton-Cotes rule (Boole's rule) integrates degree 4 by construction, and 5 by symmetry.

Exercise 2: Two-point rule by hand

Use the 2-point Gauss rule to approximate ∫02x3 dx\int_0^2 x^3\,dx.

Answer

44, exact.

Solution

Map: x=1+tx = 1 + t, nodes 1±131 \pm \frac{1}{\sqrt{3}}, weights 11, factor 22=1\frac{2}{2} = 1. (1+s)3+(1−s)3=2+6s2=2+2=4(1 + s)^3 + (1 - s)^3 = 2 + 6s^2 = 2 + 2 = 4 with s2=13s^2 = \frac{1}{3}.

Exercise 3: Nodes as eigenvalues (code)

The Gauss-Legendre nodes are the eigenvalues of the symmetric tridiagonal Jacobi matrix with off-diagonal entries k4k2−1\frac{k}{\sqrt{4k^2 - 1}}, k=1,…,n−1k = 1, \ldots, n - 1 (Golub-Welsch). Check for n=6n = 6, and get the weights as 2v122v_{1}^2 from the eigenvectors.

Answer

Nodes and weights match leggauss to rounding error.

Solution
python · try it yourself
import numpy as np
n = 6
k = np.arange(1, n)
J = np.diag(k / np.sqrt(4 * k**2 - 1), 1); J = J + J.T
vals, vecs = np.linalg.eigh(J)
x, w = np.polynomial.legendre.leggauss(n)
print(f"{np.max(np.abs(vals - x)):.1e} {np.max(np.abs(2 * vecs[0] ** 2 - w)):.1e}")
2.2e-16 1.1e-15

Test yourself

An nn-point Gauss-Legendre rule is exact for polynomials up to degree…

  1. n−1n - 1
  2. nn
  3. 2n−12n - 1
  4. 2n2n

The Gauss-Legendre nodes are…

  1. equally spaced
  2. roots of a Legendre polynomial
  3. Chebyshev points always
  4. random

Gauss-Hermite quadrature is suited to integrals of the form…

  1. ∫01f(x) dx\int_0^1 f(x)\,dx
  2. ∫−∞∞e−x2f(x) dx\int_{-\infty}^{\infty}e^{-x^2}f(x)\,dx
  3. ∫0∞e−xf(x) dx\int_0^\infty e^{-x}f(x)\,dx
  4. periodic integrals