Numerical integration: trapezoid vs Simpson
The question
For the area under ex from 0 to 1, what changes when you double the number of intervals?
Choose an error reduction before the calculation opens.
- 1 SeePredict the effect of doubling intervals.
- 2 TryTest a corner and meet error goals.
- 3 ProveBuild the integration weights.
- 4 ApplyEstimate totals and choose a grid.
Before you start · 3 quick checks on integrals, curvature, and error
Two ideas before you start
2 ideasJoin neighboring samples
The exponential curve and straight segments joining its sampled heights.
A trapezoid replaces the curve between two samples by a straight segment. Change the grid to see which points are joined.
Exact definition
Let \(a\lt b\), let \(n\) be a positive integer, and set \(h=(b-a)/n\), \(x_i=a+ih\), and \(f_i=f(x_i)\). The composite trapezoid rule is
\[\begin{aligned}T_n&=h\left(\frac{f_0+f_n}{2}+\sum_{i=1}^{n-1}f_i\right).\end{aligned}\]Each interval contributes its width times the average of its endpoint heights. An interior sample belongs to two neighboring trapezoids, so its two half-weights add. The formula also works for negative heights; it approximates signed area.
Fit a curve through each triple
The exponential curve and quadratic pieces passing through triples of samples.
Simpson's rule uses a quadratic through each consecutive triple. Each piece spans a pair of equal intervals.
Exact definition
For an even integer \(n\ge2\) on the same uniform grid, composite Simpson integration is
\[\begin{aligned}S_n&=\frac h3\bigg[f_0+f_n\\&\qquad+4\sum_{\substack{i=1\\i\text{ odd}}}^{n-1}f_i\\&\qquad+2\sum_{\substack{i=2\\i\text{ even}}}^{n-2}f_i\bigg].\end{aligned}\]An empty sum contributes zero. The midpoint of each pair gets a different weight from its endpoints. Stage 3 derives those weights by integrating the interpolating polynomial. The even-count restriction belongs to this composite rule.
Refresher: what interpolation means
Interpolation chooses a polynomial that passes through given samples. Two distinct horizontal positions determine a line; three distinct positions determine a polynomial of degree at most two. The polynomial can have lower degree when the samples lie on a line. It need not match the curve's derivatives.
The fundamental theorem of calculus evaluates an integral through an antiderivative. Numerical integration instead uses sampled values. We use functions with known integrals here so the approximation can be checked against a reference. Real data may have measurement error as well as integration error.
Double the grid, then inspect the error
- Choose the pair of error reductions you expect for the smooth exponential curve.
- After checking, select an interval count and a rule to inspect the gaps and refinement table.
| n | Trapezoid error | Ratio | Simpson error | Ratio |
|---|
The top graph shades the gap between the true function and its replacement. The lower graph magnifies the signed vertical difference, approximation minus function. Its scale changes with the rule and grid; compare the printed scales before comparing heights. A thin region can disappear at screen resolution even when its integral is nonzero.
The table measures the absolute value of the signed integral of that difference. Regions on opposite sides can cancel. It does not add the absolute sizes of all shaded regions. Simpson's curve can cross the function within a pair, so a close integral does not mean the curves coincide everywhere.
When a refinement ratio is meaningful
An upper bound alone does not prove an observed ratio. If the leading error coefficient vanishes, a method can converge faster, or already be exact. A ratio with a zero denominator is undefined. A finite grid also need not display its limiting rate yet.
Change the shape
A smooth curve is only one test. Make a new prediction for a curve with a corner before looking at either approximation.
The corner lies on a grid point, but it lies inside the pair used by the quadratic. Splitting the integral at a known corner can be more useful than fitting a smoother curve across it. Do not apply a smooth-function error formula where its required derivative does not exist.
Use what you found above: choose enough samples
Return to the exponential curve. Reach each goal using the smallest interval count among the choices. The target is an absolute integral error. Each new goal resets the grid, so check the method named in the goal before changing it.
What if the exact integral is unknown?
In practice, compare two grids and estimate the remaining error only after checking that the expected rate has appeared. A difference between two estimates is evidence, not a guarantee. Both grids can miss a narrow peak between their nodes. Derivative bounds can supply a guarantee when they are known.
More samples also mean more evaluations and additions. The lesson on floating point explains why roundoff can eventually obscure mathematical improvement. The grids here stop before that becomes important for the displayed exponential errors. Measurement noise is another limitation that a smaller interval does not remove.
Build Simpson's weights
Let \(h\gt0\) and let \(m\) be real. On \([m-h,m+h]\), let \(p\) be the polynomial of degree at most two that takes the values \(f_-\), \(f_0\), and \(f_+\) at the left endpoint, midpoint, and right endpoint. Then
\[\begin{aligned}&\int_{m-h}^{m+h}p(x)\,dx\\&=\frac h3(f_-+4f_0+f_+).\end{aligned}\]For a uniform grid with even \(n\ge2\), adding this formula over disjoint pairs gives the composite rule. This statement concerns the interpolating polynomial. It does not say an arbitrary sampled function equals that polynomial between nodes.
Use \(t=(x-m)/h\) and the three basis polynomials below. Each is one at its own node and zero at the other two:
\[\begin{aligned}L_-(t)&=(t^2-t)/2,\\L_0(t)&=1-t^2,\\L_+(t)&=(t^2+t)/2.\end{aligned}\]Thus \(p(m+ht)=f_-L_-(t)+f_0L_0(t)+f_+L_+(t)\). These are interpolation basis polynomials, not probabilities; some values between nodes are negative.
Error guarantees and why the rates differ
Write \(I=\int_a^b f(x)\,dx\), with \(a\lt b\) and \(h=(b-a)/n\). The notation \(f\in C^k[a,b]\) means the derivatives through order \(k\) are continuous on the interval. If \(f\in C^2[a,b]\) and \(|f''|\le M_2\), then
\[|T_n-I|\le\frac{(b-a)M_2h^2}{12}.\]The interpolation remainder on a single interval \([u,u+h]\) bounds the vertical gap by \(M_2(x-u)(u+h-x)/2\). Integrating that nonnegative bound gives \(M_2h^3/12\). Adding over all intervals gives the displayed global bound. The Taylor remainder lesson develops the same principle of replacing an unknown derivative by an upper bound.
For even \(n\), if \(f\in C^4[a,b]\) and \(|f^{(4)}|\le M_4\), the standard Simpson remainder gives
\[|S_n-I|\le\frac{(b-a)M_4h^4}{180}.\]The local remainder on a pair is \(I_{\rm pair}-S_{\rm pair}=-h^5f^{(4)}(\xi)/90\) for some point inside that pair. Summing its absolute bound over \(n/2\) pairs proves the global bound. We use this local remainder theorem here; the weight derivation above is not a proof of it.
Simpson's rule also integrates every cubic exactly: after shifting to the midpoint, the odd cubic term integrates to zero, and its weighted symmetric samples cancel. That extra cancellation explains why the remainder uses a fourth derivative even though the fitted curve is only quadratic. Higher degree is not an unconditional accuracy ranking.
Another choice: move the nodes
Trapezoid and Simpson rules use equally spaced samples, which is convenient when a sensor already supplied them. Gaussian quadrature chooses the sample locations as well as their weights. The two-node Gauss rule on \([-1,1]\) samples at \(\pm1/\sqrt3\), gives each sample weight one, and integrates every polynomial of degree at most three exactly.
To check that claim, apply it to the constant, linear, quadratic, and cubic terms separately. Symmetry handles the odd terms. For the quadratic, the two squared node values add to the exact quadratic integral. This is a different sampling plan, not a way to reweight arbitrary existing data. Mapping another interval to this one multiplies the weights by half its length.
Estimate a total and choose a grid
Use the formulas above. Enter a number in each box. A tolerance is shown for approximate answers; the interval count must be a whole number. Each question includes hints and a worked solution.
Further reading
- R. L. Burden and J. D. Faires, Numerical Analysis, chapter 4, “Numerical Differentiation and Integration.”
- L. N. Trefethen, Approximation Theory and Approximation Practice, chapter 19, “Clenshaw–Curtis and Gauss Quadrature.”