Gaussian Quadrature
Newton-Cotes rules fix the nodes at equally spaced points and choose only the weights. If the nodes are also chosen freely, points can integrate polynomials up to degree 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 has free parameters, enough to be exact for . The solution: the nodes are the roots of the Legendre polynomial , and the weights are positive. For : , .
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 .
Other intervals
Map to with :
Convergence
For analytic integrands the error falls geometrically with , much faster than Simpson's :
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 .
Weight functions and other families
Gaussian rules exist for integrals with a weight, , with nodes at roots of the corresponding orthogonal polynomials:
| Rule | Interval and weight | NumPy |
|---|---|---|
| Gauss-Legendre | , | polynomial.legendre.leggauss |
| Gauss-Chebyshev | , | polynomial.chebyshev.chebgauss |
| Gauss-Laguerre | , | polynomial.laguerre.laggauss |
| Gauss-Hermite | , | polynomial.hermite.hermgauss |
Gauss-Hermite rules compute expectations under a normal distribution, a common need in statistics and finance:
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 , so results for different 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 points;quaduses Gauss-Kronrod rules adaptively.scipy.special.roots_legendre,roots_hermiteand friends return nodes and weights; for large 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: . 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 .
Answer
, exact.
Solution
Map: , nodes , weights , factor . with .
Exercise 3: Nodes as eigenvalues (code)
The Gauss-Legendre nodes are the eigenvalues of the symmetric tridiagonal Jacobi matrix with off-diagonal entries , (Golub-Welsch). Check for , and get the weights as from the eigenvectors.
Answer
Nodes and weights match leggauss to rounding error.
Solution
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 -point Gauss-Legendre rule is exact for polynomials up to degree…
The Gauss-Legendre nodes are…
- equally spaced
- roots of a Legendre polynomial
- Chebyshev points always
- random
Gauss-Hermite quadrature is suited to integrals of the form…
- periodic integrals