Trapezoid and Simpson Rules
Most integrals have no closed form: , the arc length of an ellipse, the energy of a measured signal. Numerical integration (quadrature) approximates by a weighted sum of function values. The Newton-Cotes rules integrate an interpolating polynomial on equally spaced points, and their composite versions are the everyday tools.
The basic rules
On one interval of width :
| Rule | Formula | Error |
|---|---|---|
| midpoint | ||
| trapezoid | ||
| Simpson |
Simpson's rule integrates the quadratic through three points and, by symmetry, is exact even for cubics.
Composite rules
Split into subintervals of width and apply a rule on each:
( even for Simpson). The composite trapezoid error is and Simpson's is .
import numpy as np
from scipy.integrate import simpson
def trap(f, a, b, n):
x = np.linspace(a, b, n + 1); y = f(x); h = (b - a) / n
return h * (y.sum() - (y[0] + y[-1]) / 2)
def simp(f, a, b, n):
x = np.linspace(a, b, n + 1); y = f(x); h = (b - a) / n
return h / 3 * (y[0] + y[-1] + 4 * y[1:-1:2].sum() + 2 * y[2:-1:2].sum())
f = lambda x: np.exp(-x**2)
exact = 0.7468241328124270 # = sqrt(pi)/2 erf(1)
print(" n trapezoid error ratio Simpson error ratio")
pt = ps = None
for n in (4, 8, 16, 32, 64):
et, es = abs(trap(f, 0, 1, n) - exact), abs(simp(f, 0, 1, n) - exact)
rt = f"{pt / et:6.2f}" if pt else " "
rs = f"{ps / es:6.2f}" if ps else " "
print(f"{n:4d} {et:.3e} {rt} {es:.3e} {rs}")
pt, ps = et, es
x = np.linspace(0, 1, 65)
print("scipy.integrate.simpson agrees:", np.isclose(simpson(f(x), x=x), simp(f, 0, 1, 64))) n trapezoid error ratio Simpson error ratio
4 3.840e-03 3.125e-05
8 9.585e-04 4.01 1.988e-06 15.72
16 2.395e-04 4.00 1.246e-07 15.95
32 5.988e-05 4.00 7.795e-09 15.99
64 1.497e-05 4.00 4.872e-10 16.00
scipy.integrate.simpson agrees: True
Error ratios of 4 and 16 confirm second and fourth order.
The Euler-Maclaurin formula and periodic functions
The trapezoid error has an expansion in even powers of whose coefficients involve derivatives at the endpoints:
For a smooth periodic function integrated over a full period, every term vanishes and the trapezoid rule converges exponentially fast:
import numpy as np
f = lambda x: np.exp(np.cos(x)) # periodic with period 2 pi
exact = 2 * np.pi * 1.2660658777520082 # 2 pi I_0(1)
for n in (4, 8, 12, 16, 20):
x = np.linspace(0, 2 * np.pi, n, endpoint=False)
T = 2 * np.pi / n * f(x).sum()
print(f"n = {n:2d}: error {abs(T - exact):.1e}")n = 4: error 3.4e-02
n = 8: error 1.3e-06
n = 12: error 6.5e-12
n = 16: error 0.0e+00
n = 20: error 8.9e-16
Romberg integration
Since , Richardson extrapolation removes one term at a time: is exactly Simpson's rule, and further levels give , , and so on. This is Romberg integration.
In software
np.trapezoid(y, x)andscipy.integrate.simpson(y, x=x)for sampled data (including unequal spacing);cumulative_trapezoidfor running integrals.- For functions you can evaluate anywhere, adaptive and Gaussian methods are far more efficient (Gaussian Quadrature, Adaptive Integration).
Exercises
Exercise 1: By hand
Approximate by the trapezoid rule with and by Simpson's rule with .
Answer
Trapezoid , Simpson (exact).
Solution
: ; .
Exercise 2: Choose
How many trapezoid subintervals guarantee error below for , using ?
Answer
.
Solution
gives .
Exercise 3: Romberg (code)
Build a Romberg table for starting from with 5 levels and print the diagonal errors.
Answer
The diagonal errors fall rapidly, to about at the last level, using only 17 function values.
Solution
import numpy as np
f = lambda x: np.exp(-x**2); exact = 0.7468241328124270
R = np.zeros((5, 5))
for i in range(5):
n = 2**i; x = np.linspace(0, 1, n + 1); y = f(x)
R[i, 0] = (y.sum() - (y[0] + y[-1]) / 2) / n
for j in range(1, i + 1):
R[i, j] = R[i, j - 1] + (R[i, j - 1] - R[i - 1, j - 1]) / (4**j - 1)
print([f"{abs(R[i, i] - exact):.1e}" for i in range(5)])['6.3e-02', '3.6e-04', '9.6e-06', '1.1e-07', '2.8e-10']
Test yourself
The composite trapezoid rule has error…
Simpson's rule is exact for polynomials up to degree…
- 1
- 2
- 3
- 4
For a smooth periodic function over a full period, the trapezoid rule converges…
- like
- exponentially fast
- not at all
- like