Trapezoidal Rule Calculator

Approximate integrals with trapezoids and compare the result against the exact value.

Trapezoidal Rule Calculator

What Is the Trapezoidal Rule?

The trapezoidal rule approximates a definite integral by slicing the region under a curve into vertical strips and capping each strip with a straight line instead of the curve itself — turning each strip into a trapezoid whose area is elementary geometry. Where a Riemann sum caps each strip with a flat top (a rectangle), the trapezoid's slanted top follows the function's rise or fall across the strip, hugging the curve far more closely for the same number of slices.

With n subintervals of width h = (b−a)/n, the areas telescope into the classic formula:

T = (h/2)·[f(x₀) + 2f(x₁) + 2f(x₂) + ⋯ + 2f(xn−1) + f(xn)]

Interior points are counted twice because each one is shared by the two trapezoids on either side of it; the endpoints belong to only one trapezoid each, so they're counted once. That bookkeeping detail is the most common thing to get wrong when applying the rule by hand.

How to Use This Calculator

Enter a function, the interval [a, b], and how many subintervals to use. The calculator computes h, evaluates the function at every sample point (showing the first several in a table), applies the weighted sum, and reports the trapezoidal estimate alongside a high-precision reference value so the error is visible immediately. Sample points are marked on the graph when n is small enough to see them individually. Try doubling n and watch the error shrink by roughly a factor of four — that quadratic error decay is the rule's signature.

Worked Example

Approximate ∫₀⁴ x² dx with n = 4 trapezoids. Here h = 1 and the sample values are f(0)=0, f(1)=1, f(2)=4, f(3)=9, f(4)=16:

T = (1/2)·[0 + 2(1) + 2(4) + 2(9) + 16] = (1/2)(44) = 22

The exact value is 64/3 ≈ 21.333, so four trapezoids land within 0.67 — compare the left Riemann sum with the same n, which gives 14, off by more than 7. For x², the trapezoidal rule always overestimates slightly: the curve is concave up, so every chord (trapezoid top) sits above the curve.

Accuracy and the Concavity Connection

The trapezoidal rule's error is governed by the function's second derivative: |error| ≤ (b−a)h²·max|f''|/12. Two practical readings of that formula:

  • Halving h quarters the error — doubling the subinterval count buys four times the accuracy, which is why modest n values often suffice.
  • The sign of f'' predicts the bias. Concave-up regions (f'' > 0) are overestimated, since chords lie above the curve; concave-down regions are underestimated. On functions that curve both ways, the biases partially cancel.

For dramatically faster convergence at the same cost, Simpson's rule replaces the straight tops with parabolic arcs, driving the error down with h⁴ instead of h².

Common Mistakes to Avoid

  • Forgetting the factor-of-2 weights on interior points. The endpoints get weight 1, everything between gets weight 2 — miscounting is the classic slip.
  • Using h instead of h/2 out front. The leading coefficient is h/2, a residue of averaging each trapezoid's two parallel sides.
  • Applying it across a discontinuity. A vertical asymptote inside [a, b] (like 1/x across 0) makes the trapezoid areas meaningless — split the interval or treat it as an improper integral first.
  • Expecting exactness for curves. The rule is exact only for straight lines (degree ≤ 1). Any curvature produces some error, bounded by the formula above.

Real-World Applications

The trapezoidal rule is the default method for integrating measured data, where the function exists only as a table of sample points with no formula to manipulate. Engineers integrating an accelerometer log to estimate velocity, hydrologists turning streamflow gauge readings into total discharge, and clinicians computing a drug's area-under-the-curve (AUC) from periodic blood samples are all running the trapezoidal rule — it's the standard AUC method in pharmacokinetics software precisely because it needs nothing but the sampled values.

It also underlies signal processing and finance: numerical libraries' default "integrate this array" functions (like NumPy's trapz) are trapezoidal, cumulative P&L from discretely sampled rates is trapezoidal accumulation, and the rule appears inside ODE solvers as the implicit trapezoid method, prized for its stability. Its blend of simplicity, decent accuracy, and zero requirements on the integrand's form makes it the workhorse of applied integration.

Frequently Asked Questions

When should I use the trapezoidal rule instead of Simpson's rule?

Use trapezoids when your data has an arbitrary (possibly odd) number of points, when samples are unevenly spaced (the rule adapts trivially; Simpson's needs care), or when implementation simplicity matters. Use Simpson's when you can evaluate the function anywhere and want much higher accuracy per evaluation.

Why does the error shrink by 4× when I double n?

The error is proportional to h², and doubling n halves h — so the error scales by (1/2)² = 1/4. The error table this calculator shows against the reference value makes the pattern easy to verify experimentally.

How is the trapezoidal rule related to averaging Riemann sums?

It's exactly the average of the left and right Riemann sums: T = (L + R)/2. Each trapezoid's area is the mean of the left-height and right-height rectangles, so the whole sums average too — one reason the rule outperforms either one-sided sum.

Can the trapezoidal rule handle unevenly spaced data?

Yes — sum each strip individually as (Δxᵢ/2)(yᵢ + yᵢ₊₁) with its own width. This is why it dominates lab-data integration, where sampling times are rarely uniform. (This calculator uses uniform spacing for clarity.)

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

Surprisingly, yes: for smooth periodic functions integrated over a full period, the trapezoidal rule converges spectacularly fast — better than Simpson's — a fact exploited in Fourier analysis and spectral methods. For ordinary non-periodic integrands, Simpson's wins.

What is Richardson extrapolation / Romberg integration?

A clever upgrade built on the trapezoidal rule: compute T at step h and at h/2, then combine them as (4T(h/2) − T(h))/3 — the h² error terms cancel, and the result is exactly Simpson's rule. Iterating the trick with successive halvings is Romberg integration, which wrings machine-precision answers out of trapezoid evaluations alone. It's a beautiful example of squeezing extra accuracy from understanding an error's structure rather than brute-forcing more samples.