Calculus, Linear Algebra & Discrete Math Integrals & Applications of Integration Composite trapezoidal rule with the classical second-derivative error bound

Trapezoidal Rule Calculator

The trapezoidal rule joins consecutive points on a curve with straight lines and adds the areas of the resulting trapezoids. This calculator applies the composite version to your integrand, lists every node with its weight and contribution, and compares the estimate against a high-accuracy reference so you can read the true error rather than only the bound. It also reports the classical bound built from the largest second derivative on the interval, and shows what Simpson's rule would have given from the same ordinates.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Integrand f(x)Use ^ for powers, * for multiplication (2*x, 2x and a plain space all work, so x sin(x) is read as a product), and the function names sin, cos, tan, sec, csc, cot, asin, acos, atan, sinh, cosh, tanh, exp, ln, log (base 10), sqrt, cbrt and abs. The constants pi and e are recognised.sin(x)
Lower limit aWhere the interval starts.0
Upper limit bWhere it ends. The default is pi, so the exact answer is 2.3.141592653589793
Subintervals nAny positive whole number; unlike Simpson's rule the trapezoidal rule has no parity requirement.6

It returns

  • Trapezoidal estimate — Half the step size times the weighted sum of the ordinates.
  • Reference value — Computed by adaptive quadrature at a tolerance of 1e-12, for comparison.
  • Actual error
  • Classical error bound — Built from the largest absolute second derivative found on the interval.
  • Step size Δx
  • Simpson at the same n — Shown when n is even, so you can see what the same ordinates buy under a higher-order rule.

The formula

abfΔx2[f0+2f1++2fn1+fn]
|E|(ba)3max|f|12n2

In plain text: ∫ ≈ (Δx/2)[f₀ + 2f₁ + 2f₂ + … + 2f₍ₙ₋₁₎ + fₙ]

  • ΔxStep size, equal to (b − a)/n (units of x)
  • nNumber of subintervals, any positive whole number (count)
  • fᵢThe integrand at node xᵢ = a + i·Δx (units of f)

Interior ordinates carry weight 2 because each is the right edge of one trapezoid and the left edge of the next. The two endpoints belong to a single trapezoid each.

Updated Category Integrals & Applications of Integration Verified against published test cases Reading time 9 min

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 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.

  1. Step size. Δx = (1 − 0)/4 = 0.25.
  2. Nodes. 0, 0.25, 0.5, 0.75, 1.
  3. Ordinates. f₀ = 0, f₁ = 0.0625, f₂ = 0.25, f₃ = 0.5625, f₄ = 1.
  4. Weighted sum. 0 + 2(0.0625) + 2(0.25) + 2(0.5625) + 1 = 0.125 + 0.5 + 1.125 + 1 = 2.75.
  5. Multiply by Δx/2. 0.25/2 = 0.125, and 0.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 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

Every row is the composite trapezoidal rule applied to sin x from 0 to π, whose exact integral is 2. Values are produced by this calculator.
nΔxTrapezoidal estimateErrorError ratio to previous
21.57079631.57079630.4292037
40.78539821.89611890.10388114.13
60.52359881.95409720.04590282.26
80.39269911.97423160.02576841.78
160.19634951.99357030.00642974.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/2 times the sum of the two end heights.
  • Dividing by n instead of by 2. The leading factor is Δx/2, and Δx already 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)/2 one 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.

Frequently asked questions

Why do the interior points get weight 2?

Because each interior node is shared: it is the right-hand edge of one trapezoid and the left-hand edge of the next, so it contributes its height twice. The two endpoints belong to only one trapezoid each and are counted once. Factoring Δx/2 out of the sum produces the familiar 1, 2, 2, …, 2, 1 pattern.

Will the trapezoidal rule overestimate or underestimate?

It overestimates where the curve is concave up and underestimates where the curve is concave down, because the chord lies above the curve in the first case and below it in the second. When concavity changes sign inside the interval the two effects partly cancel and the direction of the total error cannot be read off the shape alone.

How many subintervals do I need?

Enough that doubling n changes the answer by less than your tolerance. Since the error falls by about four each time you double, a change of 0.004 between n and 2n suggests roughly 0.001 of remaining error. The convergence table in the reference section shows that behaviour for the sine example.

Can I use it when the data points are not equally spaced?

Not with this formula, which assumes a constant step. Instead add the trapezoids individually as (yᵢ + yᵢ₊₁)(xᵢ₊₁ − xᵢ)/2. The idea is identical; only the bookkeeping changes, and unequal spacing rules out Simpson's rule entirely.

Why is my error exactly equal to the error bound?

Because your integrand has a constant second derivative — a quadratic. The bound assumes the worst-case curvature applies on every strip, and for a quadratic that worst case is achieved everywhere, so the inequality becomes an equality. For any function whose curvature varies, the bound will be strictly larger than the measured error.

Is the trapezoidal rule ever better than Simpson's rule?

Yes, in two situations. On a smooth periodic function integrated over a whole period it converges faster than any power of 1/n, beating Simpson comfortably. And on noisy measured data its low order is a virtue, because fitting parabolas through scatter amplifies the scatter rather than averaging it out.

What is Romberg integration?

It is the systematic version of combining trapezoidal estimates at different step sizes to cancel error terms. The first combination, (4T₂ₙ − Tₙ)/3, reproduces composite Simpson exactly. Repeating the process cancels successively higher error terms and yields a very high-order method from nothing but trapezoids.

How is the maximum second derivative estimated?

The calculator differentiates your expression symbolically twice and evaluates the result at 201 evenly spaced points across the interval, keeping the largest absolute value. That is an estimate: a narrow spike between samples would be missed and the bound understated. Where the second derivative does not exist at all, the bound has no meaning and the calculator flags it when the measured error exceeds it.

References

  • Numerical Analysis, 10th edition (Newton-Cotes formulas, composite rules and Romberg integration) — Burden & Faires, Cengage Learning
  • Numerical Recipes: The Art of Scientific Computing, 3rd edition (classical formulas for equally spaced abscissas) — Press, Teukolsky, Vetterling & Flannery, Cambridge University Press
  • Handbook of Mathematical Functions, Chapter 25: Numerical Interpolation, Differentiation and Integration — Abramowitz & Stegun, National Bureau of Standards