III.15 · Calculus
How quickly does a numerical integral converge — and when does it slow down?
Refine rectangle, midpoint and trapezoid rules by doubling the number of panels. Measure how fast each error shrinks, and see smoothness — not the rule alone — decide the rate.
Refine a quadrature rule and measure its order
trapezoid rule, n = 4, for f(x) = x² on [0, 1]
f(x) = x² (smooth; exact answer 1/3). 4 trapezoid panels are drawn. Approximation 0.34375, reference 1/3.
log₁₀|error| vs k (n = 2ᵏ): selected rule solid, other rules dashed
Errors of all four rules for n = 1 … 1024 on a log scale. The selected trapezoid rule has error 1.04×10⁻² at n = 4; halving h divided it by about 4.00.
- Panels (n = 2ᵏ)
- 4
- Rule
- trapezoid
- Approximation
- 0.34375
- Reference integral
- 1/30.333333…
- Error
- 1/960.010417…
- Observed order (log₂ of the error ratio for n/2 and n)
- 2
- Expected order for smooth f
- 2
Exact values use =; binary64 approximations use ≈. Undefined values state their reason.
Halving h divides the trapezoid error by about 4.00: observed order 2.00, expected 2 for smooth f.
| k | n | Qₙ − I (signed) | observed order |
|---|---|---|---|
| 0 | 1 | 1/6 | — |
| 1 | 2 | 1/24 | 2.000 |
| 2 | 4 | 1/96 | 2.000 |
| 3 | 8 | 1/384 | 2.000 |
| 4 | 16 | 1/1536 | 2.000 |
| 5 | 32 | 1/6144 | 2.000 |
| 6 | 64 | 1/24576 | 2.000 |
| 7 | 128 | 1/98304 | 2.000 |
| 8 | 256 | 1/393216 | 2.000 |
| 9 | 512 | 1/1572864 | 2.000 |
| 10 | 1024 | 1/6291456 | 2.000 |
n = 1
Error 1/6.
from 0 to 10, whole numbers only
For , four trapezoids give , an error of exactly . Each doubling of divides the trapezoid error by exactly and the left-rule error by about . Now choose √x: slower: the trapezoid error shrinks by only about per doubling, because has an infinite slope at .
Intuition: how much does each refinement buy?
Every rule replaces on each panel by something simpler — a constant for rectangle rules, a straight line for the trapezoid rule. Halving the panel width makes the replacement better. How much better depends on how closely the simple shape follows the curve, which depends on the rule and on how smooth is. Plotting against the refinement level turns a rate into a straight line with slope .
Definition
With panels of width and points :
and uses right endpoints. A rule has order for a class of functions if its error behaves like . The observed order from two refinements is .
Mechanics: what the explorer computes
For , and every approximation and every error is an exact rational, computed for . For and the values are binary64, with references and from their antiderivatives. Panels are drawn while they are wider than a pixel. The convergence plot and table come from the same computed errors.
Derivation: why the trapezoid rule has order 2
On one panel , the trapezoid replaces by its linear interpolant. The interpolation error is , and integrating over the panel gives . Summing panels,
for some , provided is continuous. So halving quarters the error. A rectangle rule replaces by a constant and loses a factor : order . The midpoint rule is order with half the trapezoid’s constant and the opposite sign.
Worked example
. With trapezoids the error is exactly : gives (error ), gives (error ), gives (error ). The midpoint error is exactly , and the left-rule error is , which approaches order .
Replay the worked derivation
n = 1
Error 1/6.
When the rate changes
- Singular slope. has at ; the bound does not apply and the trapezoid error behaves like .
- A corner off the grid. has a corner that never falls on a grid point ; the first refinements behave irregularly before settling.
- Signed integrands. For on , positive and negative parts partly cancel in the exact value ; the rules must resolve both.
- No universal promise. Highly oscillatory or discontinuous integrands can make any fixed rule converge slowly or appear to converge to the wrong value for small .
Executable reference code
The reference computes exact errors of all four rules with Fraction and recovers the observed orders and for . The NumPy version measures observed orders for and and shows the drop from to about .
"""Exact errors of four quadrature rules for x**2 on [0, 1]; observed orders 1 and 2 (III.15)."""
from fractions import Fraction as F
import math
def rule(name, f, a, b, n):
h = (b - a) / n
x = [a + i * h for i in range(n + 1)]
if name == "left":
return h * sum(f(t) for t in x[:-1])
if name == "right":
return h * sum(f(t) for t in x[1:])
if name == "midpoint":
return h * sum(f((s + t) / 2) for s, t in zip(x, x[1:]))
return h * (f(x[0]) / 2 + sum(f(t) for t in x[1:-1]) + f(x[-1]) / 2)
f = lambda t: t * t
exact = F(1, 3)
for n in (1, 2, 4, 8, 16):
assert rule("trapezoid", f, F(0), F(1), n) - exact == F(1, 6 * n * n)
assert rule("midpoint", f, F(0), F(1), n) - exact == F(-1, 12 * n * n)
assert rule("left", f, F(0), F(1), n) - exact == F(-1, 2 * n) + F(1, 6 * n * n)
order = lambda e1, e2: math.log2(abs(e1) / abs(e2))
e = [rule("left", f, F(0), F(1), 2**k) - exact for k in range(8, 10)]
assert abs(order(*e) - 1) < 0.01"""Observed trapezoid orders: about 2 for sin on [0, 3], about 1.5 for sqrt on [0, 1] (III.15)."""
import numpy as np
def trapezoid(f, a, b, n):
x = np.linspace(a, b, n + 1)
return np.trapezoid(f(x), x)
def observed(f, a, b, exact, k):
e1, e2 = abs(trapezoid(f, a, b, 2**k) - exact), abs(trapezoid(f, a, b, 2**(k + 1)) - exact)
return np.log2(e1 / e2)
assert abs(observed(np.sin, 0, 3, 1 - np.cos(3), 8) - 2) < 0.05
assert abs(observed(np.sqrt, 0, 1, 2 / 3, 10) - 1.5) < 0.05Explore
Compare the trapezoid and midpoint curves: parallel lines, the midpoint one lower by a factor . On the signed integrand, switch to the left rule and watch the order drop to . On sin x, push to : the error approaches , still far above rounding level.
Common mistakes
Assuming more panels always gives the promised rate regardless of smoothness. Reading the error from a single instead of its behaviour under refinement. Confusing the number of panels with the number of function evaluations (the trapezoid rule uses points).
Numerical considerations
For smooth integrands, higher-order rules (Simpson’s rule, Gaussian quadrature) reach small errors with far fewer evaluations; adaptive methods refine only where the integrand needs it. At very large rounding in the sum eventually matters. Reliable software reports an error estimate, usually from comparing two refinements, exactly as the observed-order column does.
Engineering and ML connections
Numerical integration computes expectations, normalising constants, areas under curves and solutions of differential equations (one step of many ODE methods is a quadrature rule). Monte Carlo integration (Part IV) trades the deterministic rates here for a rate of that does not depend on dimension.
Exercises
Summary and checkpoints
Rectangle rules converge with order 1, trapezoid and midpoint rules with order 2 — for smooth integrands. Singularities and corners lower the observed order; signed integrands require resolving cancellation. Measure convergence by refinement rather than trusting a single approximation. This completes Part III: the next Part turns to probability, where integrals of densities measure chance.