Straight lines instead of a curve
Cut the interval into n equal strips. On each strip, replace the curve by the straight line joining its two endpoints. The area of the resulting trapezoid is the average of the two heights times the width, (f(x₀) + f(x₁))·Δx/2. Add up all the strips and you have the composite trapezoidal rule.
Collecting terms explains the weights. Every interior node appears twice — as the right edge of one trapezoid and the left edge of the next — so it carries a coefficient of 2. The two endpoints appear once each and carry a coefficient of 1. Factor out Δx/2 and you have the formula.
The rule is exact whenever the integrand is a straight line, because then the chord and the curve are the same object. That is the content of the error term: the error depends on the second derivative, which measures how far the curve bends away from its chords. A function that does not bend has no error at all, which is one of this calculator's test vectors.
The direction of the error is predictable. On a stretch where the curve is concave up, every chord lies above the curve, so the trapezoidal rule overestimates. On a concave-down stretch every chord lies below and the rule underestimates. For sin x on [0, π], which is concave down throughout, the estimate of 1.9541 sits below the true value of 2, exactly as that reasoning predicts.
How the error behaves, and why it is second order
The classical bound is |E| ≤ (b − a)³ · max|f″| / (12 n²). Three features of it are worth reading carefully.
The n² in the denominator makes this a second-order method. Doubling the number of strips divides the error by about four. Compare that with Simpson's rule, where doubling divides by about sixteen. To reach a given accuracy the trapezoidal rule needs many more evaluations of the integrand.
The second derivative sets the difficulty. A nearly straight integrand is nearly free; a sharply bending one is expensive. That is why the rule performs so well on smooth, gently varying data and so poorly near a spike.
The (b − a)³ means the interval width matters more than it looks. Doubling the width at fixed n multiplies the bound by eight, because both the width and the step size grew.
There is one striking exception worth knowing. For a smooth periodic function integrated over a whole period, the trapezoidal rule is not second order at all — it converges faster than any power of 1/n. That is why it is the standard rule for Fourier coefficients and for contour integrals in the complex plane, and it is a genuine surprise the first time you meet it.
Worked example: ∫ from 0 to 1 of x² dx with n = 4
The exact answer is [x³/3] from 0 to 1, which is 1/3 = 0.3333333.
- Step size.
Δx = (1 − 0)/4 = 0.25. - Nodes.
0, 0.25, 0.5, 0.75, 1. - Ordinates.
f₀ = 0,f₁ = 0.0625,f₂ = 0.25,f₃ = 0.5625,f₄ = 1. - Weighted sum.
0 + 2(0.0625) + 2(0.25) + 2(0.5625) + 1 = 0.125 + 0.5 + 1.125 + 1 = 2.75. - Multiply by Δx/2.
0.25/2 = 0.125, and0.125 × 2.75 = 0.34375.
The estimate is high by 0.34375 − 0.3333333 = 0.0104167, which is the overestimate that a concave-up integrand always produces.
Check it against the bound. For x² the second derivative is the constant 2, so max|f″| = 2. The bound is 1³ × 2 / (12 × 16) = 2/192 = 0.0104167. The measured error equals the bound exactly — an unusual case, and it happens because the second derivative is constant, so the worst case the bound assumes is achieved on every strip.
A less tidy case. For ∫ from 1 to 2 of dx/x with n = 4, the ordinates are 1, 0.8, 0.6666667, 0.5714286, 0.5. The weighted sum is 1 + 1.6 + 1.3333333 + 1.1428571 + 0.5 = 5.5761905, and 0.125 × 5.5761905 = 0.6970238. The exact value is ln 2 = 0.6931472, so the error is 0.0038766. The bound here uses max|f″| = max|2/x³| = 2 at x = 1, giving 1 × 2 / 192 = 0.0104167 — comfortably above the measured error, because the second derivative falls to 0.25 by the right end of the interval.
How to read the outputs
Compare the estimate against the reference value. The reference comes from adaptive quadrature at a tolerance of 1e−12, so on a smooth integrand it is exact to more digits than any hand method will produce. The difference between them is the actual error, reported separately.
The bound is a guarantee, not a forecast. It uses the largest absolute second derivative found anywhere on the interval and assumes that worst case applies to every strip. When the second derivative is constant, as it is for a quadratic, the bound is attained exactly. When the second derivative varies, the bound overstates the error, sometimes by a large factor.
A zero bound means the integrand is a straight line, and in that case the trapezoidal rule reproduces the integral exactly for any n. The calculator says so explicitly.
The Simpson comparison shows what better weights buy. It appears whenever n is even, and it uses exactly the same ordinates you already computed. On the default sine example with n = 6, the trapezoidal rule gives 1.9540972 and Simpson gives 2.0008632 — the same six strips and the same seven function evaluations, with errors of 0.0459028 and 0.0008632, a factor of fifty-three apart.
Watch the ratio of errors as you double n. It should approach 4. A ratio much smaller than that means the integrand is not smooth enough for the second-order analysis to hold, typically because of a singularity or a corner inside the interval.
The trapezoidal rule against the exact value, at five panel counts
| n | Δx | Trapezoidal estimate | Error | Error ratio to previous |
|---|---|---|---|---|
| 2 | 1.5707963 | 1.5707963 | 0.4292037 | — |
| 4 | 0.7853982 | 1.8961189 | 0.1038811 | 4.13 |
| 6 | 0.5235988 | 1.9540972 | 0.0459028 | 2.26 |
| 8 | 0.3926991 | 1.9742316 | 0.0257684 | 1.78 |
| 16 | 0.1963495 | 1.9935703 | 0.0064297 | 4.01 |
The ratio column is 4 only between panel counts that differ by a factor of two. The rows for n = 6 and n = 8 are not doublings of their predecessors, which is why their ratios are smaller; compare n = 4 with n = 8 instead and the error falls by 4.03.
Where the trapezoidal rule goes wrong
- Giving the endpoints weight 2. There are n + 1 ordinates and only the interior ones are doubled. Doubling the ends inflates the answer by
Δx/2times the sum of the two end heights. - Dividing by n instead of by 2. The leading factor is
Δx/2, andΔxalready contains the division by n. Applying both is a classic slip. - Assuming the estimate brackets the true value. It does so only when the concavity has one sign across the whole interval. Where the curve changes concavity, the over- and under-estimates partly cancel and the sign of the total error is not predictable from the shape alone.
- Applying the error bound across a singularity. The bound assumes a continuous second derivative. For an integrand such as
1/sqrt(x)near zero, no bound of this form exists and the observed convergence is far slower than second order. - Using unequal spacing with this formula. The composite formula assumes equal strips. For unequally spaced data, add the individual trapezoids
(y_i + y_{i+1})(x_{i+1} − x_i)/2one at a time. - Reaching for a higher-order rule on noisy data. Fitting parabolas to measurements with scatter amplifies the scatter. The trapezoidal rule's low order is a virtue there.
When the trapezoidal rule is the right choice
Measured data. If your integrand is a column of readings rather than a formula, the trapezoidal rule is almost always the correct tool. It is stable, makes no assumption beyond continuity, and does not amplify measurement noise the way higher-order fits do. Integrating a flow-rate log to get total volume, or a power trace to get energy, is exactly this calculation.
Periodic integrands over a full period. Here the trapezoidal rule converges spectacularly fast, faster than any fixed power of 1/n, and it beats Simpson's rule outright. This is the reason it underpins numerical Fourier analysis.
Quick hand estimates. The weights are 1, 2, 2, …, 2, 1, which is the easiest pattern to apply on paper without error.
When to reach elsewhere: use Simpson's rule for smooth analytic functions where you control the node count, since it delivers far more accuracy per evaluation. Use adaptive quadrature when you want the answer rather than a demonstration of a method. Use Riemann sums when the point is to illustrate the definition of the integral rather than to compute it accurately.
One refinement worth knowing. Combining trapezoidal estimates at n and 2n as (4T₂ₙ − Tₙ)/3 cancels the leading error term and produces exactly the composite Simpson value. Repeating that idea is Romberg integration, and it turns the humble trapezoidal rule into a high-order method at almost no extra cost.
