Error Orders and Adaptive Quadrature
Learning Objectives
- Define the order of a quadrature rule as an asymptotic statement, and estimate it even without the exact integral.
- Use the Euler-Maclaurin expansion to explain the even powers in the trapezoid error and its behavior on periodic integrands.
- Apply Richardson extrapolation, derive Simpson's rule from it, and build a Romberg table.
- Show what extrapolation does when the assumed power of is wrong, and repair it.
- Turn two Simpson estimates into an error estimate and an adaptive algorithm that spends evaluations where the error is.
- Compare an adaptive routine with
scipy.integrate.quadby evaluations and achieved error, and name inputs that defeat both.
Context & Motivation
In trapezoid-and-simpson-rules, choosing needed a bound on or over the whole interval, and one was used everywhere. Neither fits real use. A library receives only a callable and a tolerance, and an integrand such as a sharp peak needs fine panels in one place and coarse ones elsewhere.
Both problems have the same answer: compare the rule with itself at two resolutions. The difference between them estimates the error, without any derivative. It can improve the result (Richardson extrapolation) or decide where to refine (adaptive quadrature).
Core Theory
Order, measured
A method for has order on an integrand if with as . Three things are packed in there: it is about the limit , it depends on , and it says nothing about a particular . For a composite rule of degree of precision on , each panel contributes and there are panels, so the error is : for trapezoid, for Simpson. The order is exactly when the leading constant is nonzero. It can be higher for a particular , as the next section shows.
With the exact value, . Without it, three resolutions are enough, since
For the trapezoid rule on over , from . On , is , , starting from , , , heading to . The exact integral was not used.
The Euler-Maclaurin expansion
If , the composite trapezoid rule satisfies
Only even powers appear, and only endpoint derivatives enter. On with : the first term is , two terms give , and the true difference is . The series is asymptotic: for fixed it need not converge as more terms are added.
Two consequences follow at once.
- If , the term vanishes. For on , is at and at , a measured order of 4.00, matching the next term .
- If is and periodic with period , every bracket vanishes, so the error beats every power of . That explains the table for in the previous lesson.
Richardson extrapolation
Suppose with and known. Eliminating from and gives
No new function evaluations are needed. For the trapezoid rule, , , and the combination is exactly Simpson's rule: . So Simpson can be read as the trapezoid rule with its leading error removed.
The expansion has only even powers, so this can be repeated. Romberg integration fills a table with and
From extrapolation to an error estimate
On a panel with midpoint , let be one Simpson panel and the sum of two half panels. If is nearly constant on , then , and so
The approximation rests on that condition. Where varies strongly, or does not exist, the factor 15 is wrong. Adding the correction, , is Boole's rule on the five points, which is exact for polynomials of degree 5.
Adaptive Simpson
The algorithm: estimate the error on a panel; if it is within tolerance accept, otherwise split in two, give each half half the tolerance, and recurse. Function values are passed down, so each split costs only two new evaluations, and accepted panels cost evaluations.
plaintextdef adaptive_simpson(f, a, b, tol): def simp(a, fa, fm, b, fb): return (b - a) / 6 * (fa + 4 * fm + fb) def rec(a, fa, m, fm, b, fb, whole, tol, depth): l, r = (a + m) / 2, (m + b) / 2 fl, fr = f(l), f(r) left, right = simp(a, fa, fl, m, fm), simp(m, fm, fr, b, fb) d = left + right - whole if abs(d) <= 15 * tol or depth == 0: return left + right + d / 15 return (rec(a, fa, l, fl, m, fm, left, tol / 2, depth - 1) + rec(m, fm, r, fr, b, fb, right, tol / 2, depth - 1)) m = (a + b) / 2 fa, fm, fb = f(a), f(m), f(b) return rec(a, fa, m, fm, b, fb, simp(a, fa, fm, b, fb), tol, 60)
What scipy.integrate.quad does differently
quad calls QUADPACK. For a finite interval without weights or break points the routine is qagse, which the SciPy documentation describes as globally adaptive subdivision with extrapolation and a 21-point Gauss-Kronrod rule on each subinterval. "Globally adaptive" means it keeps a list of panels and refines the one with the largest error estimate, instead of splitting the tolerance in advance as the recursion above does. It has the defaults epsabs=1.49e-8, epsrel=1.49e-8, limit=50. A Gauss-Kronrod rule evaluates only interior points, so an integrand that blows up at an endpoint is not a problem for it by itself.
Worked Examples
Example 1: Richardson by hand
On over : and , with errors and . Then
with error , which is to rounding. Nine evaluations were already spent on , so the gain of about a factor of 1000 cost nothing.
For the error estimator on the single panel : , , so , close to the true .
Example 2: a Romberg table
Errors on over , each column one order of extrapolation further:
textpanels evals order 2 order 4 order 6 order 8 order 10 1 2 1.41e-01 2 3 3.56e-02 5.79e-04 4 5 8.94e-03 3.70e-05 8.59e-07 8 9 2.24e-03 2.33e-06 1.38e-08 3.35e-10 16 17 5.59e-04 1.46e-07 2.16e-10 1.34e-12 3.31e-14 32 33 1.40e-04 9.10e-09 3.38e-12 4.88e-15 2.22e-16
With 33 evaluations the table has reached rounding, so the under order 10 is noise: in 60-digit arithmetic that entry has error , and the next one on the diagonal, not printed, has . scipy.integrate.romb on the same 33 samples also ends at .
Example 3: extrapolating with the wrong power
On the trapezoid expansion is not in even powers. It is with , a generalized Euler-Maclaurin form for an endpoint singularity. At those two terms give , matching the computed difference to ten digits. Richardson with the textbook only changes the constant. With the measured , the leading term goes away:
textn T error p=2 error order p=3/2 error order 256 5.012e-05 1.982e-05 4.074e-07 512 1.779e-05 7.008e-06 1.50 1.018e-07 2.00 1024 6.304e-06 2.478e-06 1.50 2.546e-08 2.00 2048 2.233e-06 8.759e-07 1.50 6.365e-09 2.00 4096 7.905e-07 3.097e-07 1.50 1.591e-09 2.00
Romberg, which assumes even powers, inherits the problem: with 1025 evaluations its last diagonal entry has error , against for 33 evaluations on . Measure first, then extrapolate.
Example 4: adaptive Simpson against quad
Tolerance for both, with epsabs=1e-6, epsrel=0, limit=500, counting calls with a wrapper (the count equals infodict['neval'] from quad). The last column comes from a linear scan over even : it is the first , plus 1, at which uniform Simpson reaches the adaptive error.
textintegrand on [0,1] adaptive Simpson quad uniform Simpson evals error evals error evals e^x 17 2.2e-10 21 2.2e-16 83 sqrt(x) 105 2.0e-08 231 2.2e-16 25849 1/(1e-6 + (x-0.3)^2) 5009 1.7e-09 567 2.7e-12 8493 |x - 1/sqrt(2)| 37 4.0e-08 357 4.8e-10 413 step at 1/sqrt(2) 217 1.1e-16 735 6.5e-08 (see below)
Read the columns with care, because the errors differ. Evaluations alone do not rank the methods. The uniform column is also a first hit, not a stable threshold, on the peak and the kink: the error is not monotone in , and larger even still miss the target, up to on the peak and on the kink.
- Smooth:
quadreaches rounding in 21 evaluations. Adaptive Simpson stops at 17 with a much larger error, and needs 129 evaluations (tolerance ) to reach . The higher-degree rule wins. - Peak: on the peak of half-width , adaptive Simpson needs 5009 evaluations against 8493 uniform. Adaptivity is not automatically a win: with it needs 1413 to the uniform rule's 779, and with it needs 16433 to 89463. The gain grows with how concentrated the difficulty is. At , 234 of the 353 accepted panels sit in , a tenth of the interval.
- Singular: on the panels shrink toward (the narrowest accepted panel has width ) and the order in evaluations is restored: fitted over tolerances to , error falls like , against for uniform Simpson. The rate exceeds 4 because the routine returns the extrapolated value.
- Step: the recursion costs 217 evaluations for every tolerance tried. On a panel holding the jump, and the tolerance both halve at each split, so the test never passes until the panel is about wide and rounding makes zero. Uniform Simpson is erratic here, with error near at 65537 evaluations.
quadgiven the location, withpoints=[c], needs 42 evaluations and has error 0.
Example 5: what defeats an adaptive method
Any method that samples finitely many points can be fooled, because a function can differ from only between the samples. Each of these was run:
- A spike between the samples. Take on with , whose integral is . Adaptive Simpson sees five values near zero and returns after 5 evaluations.
quadreturns with error estimate after 21, and no warning.points=[1/pi]fixes this case, but for it still returns about . Knowing the scale, split the interval by hand around the spike. - An estimate that is too small. Adaptive Simpson on with tolerance uses 9 evaluations and has error , over three times the tolerance. The factor 15 assumed a nearly constant .
- A reported error that is wrong. For the step at with
epsabs=1e-10, epsrel=0, limit=500,quadreturns an error of , reports , and raises no warning. The error is above the tolerance and the report is more than five orders of magnitude too small. - An integral that does not exist. diverges.
quadreturns (which is , the principal value) with error estimate , and raisesIntegrationWarning: "The integral is probably divergent, or slowly convergent." The documentation says: "For valid results, the integral must converge; behavior for divergent integrals is not guaranteed." - An endpoint blow-up. For , the routine above fails with
ZeroDivisionErrorat .quadsucceeds with 231 evaluations and error , because it never evaluates . The substitution gives , which is smooth, andquadthen needs 21 evaluations.
Book versus today: older tutorials use scipy.integrate.romberg and scipy.integrate.quadrature, which are absent from current SciPy. Use quad for a function and romb for equally spaced samples.
Common Misconceptions & Pitfalls
- "Order tells me the error at my ." It describes the limit, under hypotheses. Measure with .
- "Richardson always adds accuracy." Only if the power is right and is smooth enough. With on the order stays 1.50.
- "The tolerance is a guarantee." Example 5 has the error at three times the tolerance. It is a target that an estimate tries to meet.
- "The reported error from
quadis an upper bound." Example 5 has it more than five orders of magnitude too small. - "Adaptive is always cheaper than uniform." Not for a mild peak: 1413 against 779 evaluations.
- "A warning-free result is correct." The spike returned , with no warning. If the integrand has known features, pass them with
pointsor split the interval. - Comparing routines by evaluations at the same tolerance. Compare at the same achieved error, since the routines overshoot a tolerance by very different amounts.
Summary
Order is an asymptotic statement, measurable without the exact answer through the ratio . The Euler-Maclaurin expansion shows that the trapezoid error is a series in even powers driven by endpoint derivatives, which explains order 4 when and the very fast convergence on smooth periodic integrands. Richardson extrapolation removes a known power: applied to the trapezoid rule it gives Simpson, and repeated it gives Romberg. It needs the right power, as shows. Comparing one Simpson panel with two gives an error estimate, and refining where it is large gives adaptive quadrature, which helps most when the difficulty is concentrated. quad adds a much higher-degree rule and global refinement, and it wins on smooth integrands. Neither can see between samples, so their tolerances and estimates are not guarantees.