Adaptive Simpson’s Rule
Source inspiration: (Mathew 2000-2019).
Description
Adaptive Simpson quadrature approximates \(\int_a^b f(x)\,dx\) to a requested tolerance \(\varepsilon\) by recursively halving intervals until a local error estimate passes, so small subintervals appear only where \(f\) needs them. On an interval \([a,b]\) with midpoint \(m\), \[ S(a,b)=\frac{b-a}{6}\left[f(a)+4f(m)+f(b)\right], \] and refinement compares \(S(a,b)\) against \(S(a,m)+S(m,b)\), which needs only two new values of \(f\), at the quarter-points.
The animation uses the acceptance test of Mathews’ routine: \([a,b]\) is accepted when \[ \left|S(a,m)+S(m,b)-S(a,b)\right| < \varepsilon_{[a,b]}, \] where the whole interval starts with \(\varepsilon\) and each half of a split interval gets half its tolerance, so refinement concentrates where curvature or oscillation is strongest. An accepted interval contributes \(S(a,m)+S(m,b)\) to the result. A common variant uses \(15\,\varepsilon_{[a,b]}\) on the right; for the example below it accepts 9 subintervals (the finest is \([0,1/32]\)) instead of 20 (the finest is \([0,1/2048]\)).
The test rests on a general result that needs \(f\in C^4\) on each interval. Simpson’s error on an interval of width \(w\) is \(-\tfrac{w^5}{2880}f^{(4)}(c)\) for some \(c\) in it, so halving the interval divides the error by about 16, and if \(f^{(4)}\) is nearly constant across \([a,b]\), \[ \int_a^b f(x)\,dx-\bigl[S(a,m)+S(m,b)\bigr]\approx\frac{S(a,m)+S(m,b)-S(a,b)}{15}. \] Under that assumption each accepted interval’s error is below about \(\varepsilon_{[a,b]}/15\), and because each split divides a tolerance between two halves, the accepted tolerances add up to exactly \(\varepsilon\); the total error is then at most about \(\varepsilon/15\), an estimate rather than a guaranteed bound. In Case 1 the routine’s value \(2.016273\) is within \(7.1\times10^{-6}\) of the integral, well inside \(\varepsilon=10^{-3}\), even though \(f\) is not \(C^4\) at \(x=0\). There the error on \([0,w]\) shrinks only like \(w^{5/3}\), and on \([0,1/2048]\) it is about 7 times the estimate, yet under its tolerance.
The test sees only five values of \(f\) per interval and can be fooled. For \(f(x)=\sin^2(4\pi x)\) on \([0,1]\), all five samples are \(0\), so the first test passes (at \(\varepsilon=10^{-3}\) or \(10^{-6}\)) and the routine returns \(0\) for an integral of \(1/2\). Mathews’ routine also has no limit on the depth of recursion. For the step \(f(x)=0\) on \([0,1/3)\) and \(1\) on \([1/3,1]\), the interval holding the jump fails the test at every level, since \(|S(a,m)+S(m,b)-S(a,b)|\) and the tolerance both shrink in proportion to its width; the recursion stops at depth 54 only when a floating-point midpoint rounds onto an endpoint.
Simpson’s Rule applies the same formula with one fixed step size; adding the error estimate to \(S(a,m)+S(m,b)\) is one Richardson Extrapolation step and gives Boole’s Rule on the interval, as in Romberg Integration.
Animations
Each animation below shows the adaptive Simpson refinement process for \(f(x)=1+e^{-x}\sin(8x^{2/3})\) on \([0,2]\) with tolerance \(\varepsilon=10^{-3}\). Starting from the single panel \([0,2]\), each frame splits the widest panel that fails the test (leftmost first, except that \([1/2,1]\) is split before \([0,1/2]\), as in Mathews’ trace) and marks the two new sample points in blue; “Approximation” is the composite Simpson sum of \(S(a,b)\) over the panels drawn.
Julia source scripts that generated these animations are linked under each case.
Case 1 — Adaptive Refinement, \(f(x)=1+e^{-x}\sin(8x^{2/3})\), \([0,2]\), \(\varepsilon=10^{-3}\)
Behavior: Refinement stops when all 20 panels pass the test. The 41 points drawn are the nodes of the composite rule; the test also evaluates each panel’s two quarter-points, so reaching this partition samples \(f\) at 81 distinct points. \([1.5,2]\) is never split, while 19 of the 41 nodes lie in \([0,1/8]\). The last seven frames split \([0,1/16], [0,1/32], \dots, [0,1/1024]\) in turn, because \(f'\) is unbounded at \(x=0\) (\(f'(x)\approx \tfrac{16}{3}x^{-1/3}\) near \(0\)); the panels split in the last five frames are at most \(1/64\) wide (under 5 pixels at this scale), so the blue label names each one. The final estimate \(2.016252\) differs from the integral \(2.016280\) by \(-2.8\times10^{-5}\); summing \(S(a,m)+S(m,b)\) over the same panels, as the recursive routine returns, gives \(2.016273\).
