Trapezoidal Rule for Numerical Integration

← Numerical Methods

Source inspiration: (Mathew 2000-2019).

Description

The trapezoidal rule approximates a definite integral \(\int_a^b f(x)\,dx\) from values of \(f\) at equally spaced nodes. It splits \([a,b]\) into \(n\) subintervals of width \(h=(b-a)/n\), with nodes \(x_i=a+ih\) for \(i=0,1,\dots,n\), and on each subinterval it replaces \(f\) by the line segment between neighboring nodes. Summing the trapezoid areas gives the composite rule \[ T_n = h\left[\tfrac12 f(x_0) + \sum_{i=1}^{n-1} f(x_i) + \tfrac12 f(x_n)\right], \quad h=\frac{b-a}{n}. \]

The rule works for any \(n\ge 1\) and is exact when \(f\) is a linear function. Since \(T_n\) is the average of the left and right Riemann sums, it converges to the integral for every continuous \(f\); the error formula below needs more, namely \(f\in C^2[a,b]\).

The general result, as Mathews states it: if \(f\in C^2[a,b]\), there is a \(c\) with \(a<c<b\) such that \[ \int_a^b f(x)\,dx - T_n = -\frac{(b-a)\,f''(c)}{12}\,h^2 . \] The error is therefore \(O(h^2)\), and halving \(h\) typically divides it by about 4. If \(f''\) keeps one sign on \([a,b]\), then \(T_n-\int_a^b f\,dx\) has that sign: if \(f''>0\), every chord lies above the curve and \(T_n\) overestimates the integral.

Case 1 shows what happens when the hypothesis fails. Its integrand \(f(x)=e^{-x}\sin(8x^{2/3})+1\) has \(f'\) unbounded at \(x=0\), so it is not in \(C^2[0,2]\), and the error shrinks only like \(h^{5/3}\): in the table below, halving \(h\) divides it by 3.08, 3.14 and then 3.16, approaching \(2^{5/3}\approx 3.17\), not 4. The same case shows that one coarse sum can look accurate by luck: the single trapezoid gives 2.017925, only about 0.0016 above the integral, but with two subintervals the error jumps to about \(+0.357\).

\(n\) \(h\) \(T_n\) \(T_n-I\) ratio
6 \(1/3\) 1.855188 \(-0.1611\)
12 \(1/6\) 1.957234 \(-0.05905\) 2.73
24 \(1/12\) 1.997086 \(-0.01919\) 3.08
48 \(1/24\) 2.010170 \(-0.006110\) 3.14
96 \(1/48\) 2.014345 \(-0.001934\) 3.16

The rows are frames of Case 1, each halving \(h\), against \(I=\int_0^2 f(x)\,dx\approx 2.016280\); the ratio is the previous row’s error divided by this row’s.

The trapezoidal rule is the degree-1 member of the Newton-Cotes family, and the Midpoint Rule has the same \(O(h^2)\) order; one Richardson step \((4T_{2n}-T_n)/3\) gives Simpson’s Rule \(S_{2n}\), and Romberg Integration repeats that step on successive trapezoidal sums.

Animations

Each animation below shows the trapezoidal rule diagram over \([0,2]\) for \(y = e^{-x}\sin(8x^{2/3})+1\). Each new frame recomputes the approximation on the full interval with a denser partition.

Julia source scripts that generated these animations are linked under each case.

Case 1 — Composite Trapezoidal Rule, \(f(x)=e^{-x}\sin(8x^{2/3})+1\) on \([0,2]\)

Behavior: As the number of sample points increases, trapezoids across the entire interval become narrower, and the approximation converges toward the true integral \(\int_0^2 f(x)\,dx \approx 2.016280\); the last frame (121 sample points) gives 2.014944, about 0.0013 low. The approach is not steady at first. The single trapezoid in the first partition frame (2 sample points, 2.017925) is close to the integral only by coincidence: the areas where the curve lies above and below the chord from \(f(0)\) to \(f(2)\) almost cancel. With 2 subintervals the error jumps to about \(+0.36\), and from 4 subintervals (5 sample points) on its size shrinks at every frame. The \(x^{2/3}\) term makes \(f'\) unbounded at \(x=0\) (\(f'(x)\approx \tfrac{16}{3}x^{-1/3}\) near \(0\)), so the textbook error order, which assumes smooth \(f\), does not hold for this integrand. The measured error shrinks like \(O(h^{5/3})\) rather than \(O(h^2)\): halving \(h\) divides it by about 3.14 (from 24 to 48 subintervals), tending to \(2^{5/3}\approx 3.17\), instead of 4.

Julia source

Trapezoidal Rule animation over [0,2] with full-interval density refinement: each frame uses a denser partition and updates the global approximation

References

Mathew, John H. 2000-2019. Numerical Analysis - Numerical Methods Modules. https://web.archive.org/web/20190808102217/http://mathfaculty.fullerton.edu/mathews/n2003/NumericalUndergradMod.html.