Numerical integration
Most integrals that arise in science and engineering cannot be done in closed form. Even has no elementary antiderivative, and the integrands met in practice are often known only as computer procedures: a simulation, a measured curve, the solution of another equation. Numerical integration, or quadrature, approximates
a weighted sum of function values at chosen nodes with weights . Every method in this chapter is a strategy for choosing those nodes and weights. The word quadrature comes from the classical problem of constructing a square (quadratum) with the same area as a given figure.
The central trade-off
A quadrature rule is judged by how quickly its error falls as the number of function evaluations grows, and how robustly it does so when the integrand misbehaves.
- Algebraic convergence, , is what you get from piecewise polynomial rules such as the trapezoid () and Simpson () rules. It is robust: the rate depends only on how many derivatives has.
- Exponential convergence, or , is possible when is analytic (has a convergent Taylor series) on and near the interval. Gaussian quadrature, Romberg integration (in its limit) and the trapezoid rule for periodic functions all exploit analyticity.
The two figures below show the methods of this chapter on two integrands, plotted as error against the number of function evaluations.
Figure 2.1. Convergence on a smooth integrand.
On the smooth Runge function , Gaussian quadrature reaches full double precision with about 100 evaluations. The trapezoid rule, still at after 1000 evaluations, would need tens of millions to match it. Romberg integration and tanh-sinh quadrature also converge faster than any power of .
Figure 2.2. Convergence on an integrand with an endpoint singularity.
On , whose derivative is infinite at , the picture changes completely. Every polynomial-based rule, Gauss included, falls back to slow algebraic convergence, and Romberg extrapolation no longer helps. Only tanh-sinh quadrature, which is designed for endpoint singularities, keeps its exponential rate.
Choosing a method
| Your integrand | Recommended | Entry |
|---|---|---|
| Smooth, cheap to evaluate | Romberg integration | Romberg integration |
| Smooth, expensive to evaluate | Gauss-Legendre quadrature | Gaussian quadrature |
| Weight on , or on | Gauss-Hermite, Gauss-Laguerre | Gaussian quadrature |
| Singular or infinitely steep at an endpoint | Tanh-sinh quadrature | Tanh-sinh quadrature |
| Localised peaks, kinks or discontinuities inside the interval, or unknown behaviour | Adaptive Gauss-Kronrod | Adaptive Gauss-Kronrod quadrature |
| Many related integrands; oscillating, algebraic-logarithmic or Cauchy weights | quad_vec, quad_weighted | Vector integrands and weighted quadrature |
| Periodic over its interval | Trapezoid rule | Newton-Cotes rules |
| Several dimensions | Cubature, then quasi-Monte Carlo above about ten | Multidimensional cubature, Monte Carlo |
Beyond quadrature
The same ideas underlie the rest of this chapter. Integrating an ordinary differential equation is quadrature in which the integrand depends on the unknown solution. Runge-Kutta methods are, at heart, quadrature rules applied stage by stage, and the collocation methods for boundary value problems are Simpson’s rule in disguise. Spectral methods for differential equations rely on the same Chebyshev and Fourier approximations that make Gaussian and trapezoid quadrature converge exponentially.
| Your differential equation | Recommended | Entry |
|---|---|---|
| Initial value problem, non-stiff | Adaptive Dormand-Prince; DOP853 at tight tolerances | Runge-Kutta methods, explicit methods in solve_ivp |
| Samples of a function (measured data), possibly unequally spaced; running integrals | trapezoid, simpson, romb, cumulative_simpson | Integrating sampled data |
| Initial value problem with fast and slow time scales (stiff) | BDF, or Rosenbrock at loose tolerances | Stiff ODE solvers |
| Stiff at tight tolerances, stiffness unknown or changing; dense output; events | Radau, LSODA | Radau, LSODA and event location |
| Conditions at both ends of an interval | Collocation (shooting if well conditioned) | Boundary value problems |
| Heat, wave or Poisson equation on a rectangle | Finite differences | Finite difference methods |
| Irregular domains, variable or discontinuous coefficients | Finite elements | Finite element methods |
| Smooth solution on an interval or a periodic domain | Chebyshev or Fourier spectral methods | Spectral methods |
Testing against SciPy
The references come from SciPy 1.17.1, written by book/generator/data/make_integration_refs.py, with the Gauss-Legendre weights also computed by mpmath at 40 digits.
The Gaussian rules agree with scipy.special’s to rounding error. At 100 points the library’s Legendre weights are in fact the more accurate: SciPy’s differ from the exact weights by .
Table 2.1. Quadrature rules against SciPy.
| rule (SciPy reference) | largest difference |
|---|---|
Gauss-Legendre, 5 points (special.roots_legendre) | nodes 0; weights 2e-16 (relative); against mpmath, library 1e-16 and SciPy 2e-16 |
Gauss-Legendre, 20 points (special.roots_legendre) | nodes 1e-16; weights 3e-15 (relative); against mpmath, library 1e-15 and SciPy 4e-15 |
Gauss-Legendre, 100 points (special.roots_legendre) | nodes 1e-16; weights 2e-13 (relative); against mpmath, library 3e-15 and SciPy 2e-13 |
Gauss-Hermite, 10 points (special.roots_hermite) | nodes 4e-16, weights 1e-15 (relative) |
Gauss-Hermite, 40 points (special.roots_hermite) | nodes 9e-16, weights 6e-16 (relative) |
Gauss-Laguerre, 10 points, alpha = 0 (special.roots_genlaguerre) | nodes 4e-16, weights 7e-15 (relative) |
Gauss-Laguerre, 20 points, alpha = 1.5 (special.roots_genlaguerre) | nodes 4e-15, weights 1e-14 (relative) |
For one-dimensional integrals the table gives the relative error against the exact value, for the library’s adaptive Gauss-Kronrod and tanh-sinh rules and for SciPy’s QUADPACK quad and its own tanhsinh:
Table 2.2. Integrals by Gauss-Kronrod and tanh-sinh against SciPy's `quad`.
| integral | library Gauss-Kronrod | library tanh-sinh | SciPy quad | SciPy tanhsinh |
|---|---|---|---|---|
| exp(-x^2) on [0, 1] | 6e-16 | 6e-16 | 4e-16 | 3e-16 |
| 1/(1 + 25 x^2) on [-1, 1] | 0 | 4e-16 | 0 | 4e-16 |
| sqrt(x) ln(x) on [0, 1] | 2e-16 | 0 | 7e-16 | 2e-16 |
| 1/sqrt(1 - x^2) on [-1, 1] | 1e-8 | 7e-9 | 6e-14 | 7e-9 |
| cos(30 x) exp(x) on [0, 2] | 4e-15 | 2e-15 | 5e-15 | 3e-14 |
| 1/sqrt(1 - x^2), library tanh-sinh with the endpoint offset | 0 |
All four reach about double precision, except on , where loses its digits near the ends when is rounded. QUADPACK’s algebraic-singularity extrapolation recovers most of them. Tanh-sinh recovers all of them when the integrand is given the exact offset of from the nearer endpoint, which integrate_with_complement provides.
Cubature, initial and boundary value problems, compared with integrate.cubature, solve_ivp (DOP853 for the non-stiff problem, Radau for the stiff ones) and solve_bvp:
Table 2.3. ODE and boundary-value solvers against SciPy.
| problem (SciPy reference) | result |
|---|---|
cubature of exp(-r^2) on [0, 1]^3 (integrate.cubature) | relative difference 9e-15 |
cubature of cos(x + 2y) on [0, 1] x [0, 2] (integrate.cubature) | relative difference 4e-15 |
van der Pol, mu = 1, state at t = 10: Dormand-Prince (solve_ivp, DOP853) | largest difference 1e-11 |
van der Pol, mu = 100, state at t = 100 (solve_ivp, Radau) | Rosenbrock 4e-7, BDF 6e-8 |
Robertson’s reactions, state at t = 1000 (solve_ivp, Radau) | relative: Rosenbrock 5e-11, BDF 4e-8 |
Bratu’s problem y’’ + e^y = 0, solution at 11 points: collocation (solve_bvp) | largest difference 4e-15 |
The ODE differences are those expected from the tolerances requested: for Dormand-Prince and for the stiff solvers.
In the library
The integration crate is lib_jsl_integration
(API reference). Most one-dimensional rules implement
the trait CanIntegrate1D,
which sets the interval and integrates a closure Fn(f64) -> f64, so methods can be swapped
freely.