The Trapezoidal Rule
The trapezoidal rule approximates an integral by straight-line segments between samples, with error that falls as the square of the panel width.
Straight lines under the curve
The trapezoidal rule approximates the area under a curve on one interval by the trapezoid formed by connecting the endpoints with a straight line: integral over [a,b] is about (b-a)(f(a)+f(b))/2. The composite version divides [a,b] into n equal panels of width h and sums the trapezoids.
def trapezoid(f, a, b, n):
h = (b-a)/n
s = 0.5*(f(a) + f(b))
for i in range(1, n):
s += f(a + i*h)
return h*s
Error and convergence
The composite trapezoidal rule has error proportional to h^2, so halving the panel width cuts error by about a factor of four. The error coefficient involves the second derivative of the integrand, so the rule is exact for straight lines and degrades on strongly curved functions.
A surprising strength
For smooth periodic functions integrated over a full period, the trapezoidal rule is extraordinarily accurate, converging faster than any power of h. This is because the periodic error terms cancel. The result makes the trapezoidal rule the method of choice for Fourier-related and periodic integrals.
As a foundation
The trapezoidal rule is the starting point for Romberg integration, where Richardson extrapolation over successively halved step sizes rapidly boosts its accuracy. It is also the basis of the implicit trapezoidal method for ODEs, prized for stability.
Simple, robust, and easy to reason about, the trapezoidal rule is a default for one-dimensional integrals in engineering models, including profile integrals over plasma cross-sections in breeder Hyperion analyses.