Trapezoid and Simpson Rules
Learning Objectives
- Derive the trapezoid and Simpson rules by integrating an interpolating polynomial, and write their composite forms.
- State each error term with its hypothesis: for trapezoid, for Simpson.
- Explain why Simpson's rule, built on a parabola, is exact for cubics.
- Measure an order of convergence from a table of errors, and show it dropping on an integrand that lacks the required derivatives.
- Choose from an error bound, and tell a bound from the true error.
- Say where rounding stops the convergence, and when a higher-degree rule makes things worse.
Context & Motivation
The lesson the-definite-integral-and-riemann-sums built the trapezoid rule as the average of the left and right sums and observed its rate on a table. An observed rate does not say which property of is responsible, or what to do when a function has no antiderivative and only samples are available.
This lesson reaches the same rule by another road: replace by a polynomial that interpolates it, then integrate the polynomial exactly. The interpolation error from polynomial-interpolation-lagrange-and-newton then turns into an exact error term, with a specific derivative in it. That derivative is the hypothesis, and removing it is what makes the order fall.
Core Theory
Integrate the interpolant
Let interpolate at nodes in . Writing in the Lagrange basis gives
The weights depend on the nodes only, so they are computed once. With and :
- Degree 1, nodes : for each node, so .
- Degree 2, nodes : integrating the three basis parabolas gives , , , so .
The trapezoid error
If , for each there is a with . The factor never changes sign on , so the mean value theorem for integrals with a one-signed weight applies. Since ,
Split into panels of width and add the panels. Because is continuous, the average of the values is itself a value of , which gives the composite result:
For convex this says overestimates. As the mean value tends to the average of , so .
Simpson gets one degree for free
The same argument stalls for Simpson. The factor changes sign at , and its integral over is by symmetry. That zero is good news: it means the rule integrates every cubic exactly, one degree more than the parabola it was built from. Its degree of precision is 3.
To get the error, use a cubic that also matches . For ,
and now the factor keeps one sign. Its integral is , and because is exact for cubics and agrees with at the three nodes. Hence . Each panel has , so the composite rule needs even, and
Both composite rules use evaluations of :
plaintextdef trapezoid(f, a, b, n): x = np.linspace(a, b, n + 1); y = f(x); h = (b - a) / n return h * (y[0] / 2 + y[1:-1].sum() + y[-1] / 2) def simpson(f, a, b, n): # n even 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())
Why not one polynomial of high degree
More nodes in one polynomial can help when is very smooth: on over , Newton-Cotes of degree 8 has error , against for Simpson on the same nine nodes. On equally spaced nodes it can also fail badly. Take on , where . The closed Newton-Cotes rule of degree integrates the interpolant at equispaced nodes. Its weights, in units of the spacing, come from scipy.integrate.newton_cotes:
textd Newton-Cotes error most negative weight composite Simpson, same nodes 4 7.46e-02 (none) 1.93e-02 8 2.49e-01 -1.28 2.59e-02 12 6.12e-01 -16.7 9.00e-03 16 1.80e+00 -168 2.70e-03 20 5.92e+00 -1800 7.79e-04
This is Runge's phenomenon, integrated. Negative weights also amplify rounding in . Low degree on many panels is the usual remedy.
Where the order drops
The orders 2 and 4 are promises under and . Measured here by doubling from 1024 to 2048, except the last two rows, which fit a line through because their errors are erratic:
textintegrand on [0,1] T order S order what fails x^(1/2) 1.50 1.50 f' unbounded at 0 x^(3/2) 2.00 2.50 f'' unbounded at 0 x^(5/2) 2.00 3.49 f''' unbounded at 0 x^(7/2) 2.00 3.99 f'''' unbounded at 0 |x - 1/sqrt(2)| 2.02 1.83 kink step at 1/sqrt(2) 0.98 1.05 jump
On Simpson's advantage disappears: both rules have order . The row shows the hypotheses are sufficient, not necessary: is unbounded, the theorem says nothing, and trapezoid still has order 2. For a kink, the remedy is to split the integral there. With and a multiple of 4 the kink is a node and is exact, because is linear on every panel.
Order can also be better than promised. For over the full period , a smooth periodic integrand:
textn trapezoid error Simpson error 8 1.25e-06 1.15e-02 16 1.78e-15 4.17e-07 32 1.78e-15 2.66e-15
Both are far ahead of and , and trapezoid beats Simpson. The next lesson, error-orders-and-adaptive-quadrature, explains why.
Rounding stops the convergence
The error terms above are truncation only. Run the trapezoid rule on over in float32, with a plain running sum:
textn float32 error truncation h^2(e-1)/12 16 5.591e-04 5.593e-04 256 2.182e-06 2.185e-06 1024 2.018e-07 1.366e-07 4096 5.594e-07 8.535e-09 65536 6.786e-07 3.334e-11 262144 7.474e-06 2.084e-12 1048576 3.066e-04 1.302e-13
Up to truncation dominates. Past the error stops falling, and past it grows, because the accumulated rounding of the sum (see sources-of-error-truncation-rounding-propagation) passes the shrinking truncation. In float64 the same effect sits far lower. Simpson on this integral has error at , which is still truncation (predicted ), and rounding shows at : against a predicted .
Worked Examples
Example 1: exact for cubics, not for quartics
Take on with one Simpson panel: , , , so , and the exact integral is . For on with , : against , so . The error formula gives , exactly, because is constant.
Example 2: measuring both orders on
For , a script prints the errors, the ratio on doubling , the order , and the predicted leading terms and .
textn |I-T_n| ratio order predicted |I-S_n| ratio order predicted 4 8.940e-03 8.949e-03 3.701e-05 3.729e-05 8 2.237e-03 3.997 2.00 2.237e-03 2.326e-06 15.91 3.99 2.331e-06 16 5.593e-04 3.999 2.00 5.593e-04 1.456e-07 15.98 4.00 1.457e-07 32 1.398e-04 4.000 2.00 1.398e-04 9.103e-09 15.99 4.00 9.104e-09 64 3.496e-05 4.000 2.00 3.496e-05 5.690e-10 16.00 4.00 5.690e-10 128 8.740e-06 4.000 2.00 8.740e-06 3.556e-11 16.00 4.00 3.556e-11
Both and are positive here, as the signs of the error terms predict for and .
Example 3: choosing from a bound
The integrand of has no elementary antiderivative; the value is . Aim for error . Here and , whose absolute values on peak at with values and . So:
- Trapezoid: needs .
- Simpson: needs , so the first even value is .
That is 410 evaluations against 19. Both bounds are safe and pessimistic: at the trapezoid error is , and at the Simpson error is . Scanning with the exact value shows the target is first met at for trapezoid () and for Simpson ().
Example 4: where trapezoid is exact and Simpson is wrong
The ROC curve of evaluation-metrics-precision-recall-and-roc, built from a finite sample with distinct scores, is a polyline. Rank ten examples by score, with labels (five positives, five negatives). The trapezoid rule through the ROC points gives AUC , which equals the fraction of positive-negative pairs ranked correctly, computed separately. It is exact because the rule integrates each straight segment exactly. Passing the same points to scipy.integrate.simpson returned with no warning (SciPy 1.15.3): the curve has vertical segments, so repeats, and a parabola through such points means nothing. The higher-order rule is the wrong tool for data that is not smooth.
Example 5: checking against the libraries
On with and , the two functions above agree with scipy.integrate.trapezoid and scipy.integrate.simpson to within .
Book versus today: books say must be even, which means an odd number of samples. scipy.integrate.simpson also accepts an even number of samples, and handles the leftover interval with a correction. With 12 samples the order stays near 4 (measured 3.96), but exactness for cubics is gone: on the error is with 12 samples and with 11. Older code calls scipy.integrate.trapz and simps, which no longer exist in SciPy, and numpy.trapz, which was deprecated in NumPy 2.0 and removed in 2.4 in favor of numpy.trapezoid.
Common Misconceptions & Pitfalls
- "Simpson is always more accurate than trapezoid." It has the higher order on integrands. On the orders tie, on a smooth periodic integrand trapezoid wins, and on a step at with the Simpson error () is twice the trapezoid error ().
- "A parabola can only be exact for parabolas." Simpson is exact for cubics, by symmetry.
- "Doubling divides the error by 16." Only under and in the asymptotic range. On the factor is 2.83, and in float32 it can be below 1.
- "Higher degree means higher accuracy." On the Runge function, degree 20 on equispaced nodes has error 5.92.
- "The bound is the error." It is a guarantee, here about 2.7 times too large for trapezoid at .
- "Simpson needs an even number of points." It needs an even number of panels, so an odd number of points.
Summary
Integrating a degree-1 or degree-2 interpolant gives the trapezoid and Simpson rules. For the composite trapezoid error is , and for the composite Simpson error is , with even. Simpson gains a degree because the cubic term integrates to zero. These orders were measured on as 2.00 and 4.00, and they fall on integrands without the derivatives (1.50 for both on , about 1 at a jump). They can also be exceeded, as trapezoid does on smooth periodic integrands. Raising the degree of a single equispaced rule diverges on the Runge function, so low degree on many panels is the usual choice. Rounding sets a floor that float32 shows clearly. Fixed still needs a derivative bound you rarely have, which is the problem the next lesson solves.