Numerical Integration & Differentiation

the trapezoidal rule

Picture the area under a curve as a piece of land with a wavy top edge. The crudest honest estimate is to pretend the top is a straight line between the two ends and compute the area of the resulting trapezoid — a rectangle with a slanted top. The trapezoidal rule is exactly that, and it becomes a genuinely good method when you chop the land into many thin vertical strips and add up a little trapezoid for each.

On a single interval [a, b], the rule approximates the integral of f by (b - a) * (f(a) + f(b)) / 2 — the width times the average of the two end heights. It is exact whenever f is a straight line, and its error is -(b - a)^3 f''(xi) / 12 for some point xi, so it errs in proportion to the integrand's curvature: convex functions are overestimated, concave ones underestimated. In composite form you divide [a, b] into n strips of width h = (b - a)/n and sum: h * (f(x_0)/2 + f(x_1) + f(x_2) + ... + f(x_{n-1}) + f(x_n)/2). The interior points get full weight, the two endpoints half. The composite error is O(h^2), so halving the strip width quarters the error.

The trapezoidal rule is the simplest composite quadrature worth using, and it is the seed of Romberg integration, where Richardson extrapolation repeatedly refines it into a high-order method. A beautiful and underappreciated fact: for a SMOOTH PERIODIC integrand integrated over a full period, the trapezoidal rule is not merely second order — it converges faster than any power of h (spectral accuracy), which is why it is the rule of choice for Fourier-type integrals. Its honest weakness is on non-periodic functions or near singularities, where it is only second order and a higher-order or adaptive rule does much better for the same number of evaluations.

Integrate f(x) = sqrt(x) on [0, 1] (true 2/3 = 0.6667). One trapezoid gives (1/2)(0 + 1) = 0.5, error 0.17. Splitting into 4 strips (h = 0.25): 0.25 * (0/2 + sqrt(0.25) + sqrt(0.5) + sqrt(0.75) + 1/2) = 0.6433, error 0.023 — better, though convergence is hurt near x = 0 where the slope is infinite.

Join the samples by straight lines; sum the strip trapezoids.

Trapezoidal is only second order on generic functions, so it is rarely the most efficient choice — Simpson or Gauss beat it per evaluation. The striking exception is smooth periodic integrands over a full period, where it is spectrally accurate and essentially unbeatable.

Also called
trapezium ruletrapezoid rule梯形公式