∫Numerical Differentiation and Integration
Romberg and Adaptive Quadrature
Romberg integration, adaptive Simpson, error estimation and scipy.integrate.quad
QUADPACK, 1983
In the late 1970s Robert Piessens and Elise de Doncker at the University of Leuven, Christoph Überhuber in Vienna and David Kahaner at the US National Bureau of Standards set out to write a definitive library of one-dimensional integration routines in Fortran. Each routine had to take a function, an interval and a requested accuracy, and return the integral and an estimate of its own error — without asking the user for a grid, a step size or a number of points.
The library, QUADPACK, appeared with a book in 1983. Its routines bisect intervals where the integrand is difficult, apply 21-point Gauss–Kronrod rules on each piece, estimate errors from the difference between two rules, and even extrapolate their way past endpoint singularities.
Four decades later it is still running everywhere. When you type
scipy.integrate.quad(f, 0, 1), SciPy calls QUADPACK's routine QAGS — Fortran from 1983 — and it is the reason quad usually just works.💡Romberg integration
The composite trapezoid rule's error has an expansion in even powers of : (the Euler–Maclaurin formula). So Richardson extrapolation (lesson 8-1) applies repeatedly. Compute with panels — each reusing all previous function values — and build the table
Column has error ; the first extrapolated column is exactly Simpson's rule. Werner Romberg proposed the scheme in 1955. For , the diagonal errors go , , , , , — thirteen digits from 33 function values.
Derivation
The Romberg recurrence is Richardson, column by column
Suppose the entries of column have leading error , where is the panel width of row . The entry one row up used panels twice as wide, so its leading error is . Richardson's combination with (lesson 8-1) removes it:
The first column is Simpson. On one double panel , and , so
Richardson discovers Simpson's rule by itself. Each further column cancels the next power of , which the Euler–Maclaurin formula guarantees is all the trapezoid rule's error contains for a smooth integrand.
Error estimates for free. Any two approximations of different accuracy give an estimate of the error of the worse one: their difference. Romberg's table, Simpson on one panel versus two half panels, a Gauss rule versus its Kronrod extension — all use the same idea. It is the foundation of every automatic integrator, and of step-size control in ODE solvers (Unit 9).
💡Adaptive Simpson
Integrate a panel with Simpson's rule () and again as two half panels (). Because Simpson's error scales like , 's error is about . If , accept — the extrapolated value, at no extra cost. Otherwise apply the same test to each half with tolerance .
The recursion spends evaluations only where the test fails. For — infinite slope at 0, like the displacement near a crack tip — adaptive Simpson reaches an error of with 985 evaluations, nearly all of them crowded near . Uniform Simpson with 65,537 evaluations is still off by .
Derivation
Where the 15 comes from
Let the panel have width . One Simpson rule on it has error , with proportional to (lesson 8-2). The two half panels have width , so each has error , and together
We do not know , but we know the difference of the two results:
So the error of the better estimate is about . Testing it against the tolerance is the acceptance test, and adding it to is a free Richardson step — the corrected value is exact for quintic polynomials. The argument assumed roughly constant over the panel, which is true for small panels on smooth functions and false across a spike: that is how adaptive routines are fooled.
I wish to God these calculations had been executed by steam
⚠How adaptive integrators are fooled
An adaptive routine only knows the function at the points it sampled. A narrow spike that falls between all of its first samples is invisible: the two rules agree, the error estimate is small, and the answer is confidently wrong. Help it: pass known trouble spots with
points=[...] (for a step at 0.3, quad needs 42 evaluations instead of 357), split the interval at discontinuities, transform away singularities, and use the special weights for oscillatory integrands (weight='cos', wvar=50 integrates over to twelve digits). And always read the error estimate quad returns.💡Where you will use this
Orbital Mechanics: averaged perturbation integrals and probability-of-collision integrals. Structures and Geotechnics: influence-line integrals, stress integrals around singular points, Boussinesq-type load integrals. Propulsion: integrating heat flux and pressure distributions over nozzle contours. Statistics and reliability: probabilities as integrals of densities with long tails over infinite intervals —
quad handles by a change of variables.📚Further reading
- Robert Piessens, Elise de Doncker-Kapenga, Christoph Überhuber and David Kahaner, QUADPACK: A Subroutine Package for Automatic Integration (Springer, 1983) — the book behind
scipy.integrate.quad. - Walter Gander and Walter Gautschi, 'Adaptive quadrature — revisited', BIT Numerical Mathematics 40(1), 84–101 (2000) — adaptive Simpson and Gauss–Lobatto, their error estimates and how to stop them.
- Philip Davis and Philip Rabinowitz, Methods of Numerical Integration (2nd ed., Academic Press, 1984) — Romberg and the other extrapolation methods, and the theory of automatic integration.
↓But there is a problem
Romberg and adaptive Simpson are clever ways of using equally spaced points within each panel. Every rule so far fixed the nodes first and then chose the weights.
In 1814 Gauss asked the obvious question nobody had asked: if the nodes are free too, where should they go? With points chosen freely, he found, you can integrate polynomials of degree exactly — twice what equally spaced points achieve.