Trapezoidal Rule Calculator (Composite, Romberg)

Trapezoidal rule calculator for numerical integration: composite order 2 and Romberg with Richardson extrapolation to accelerate convergence up to O(h^8).

Calculation parameters
Was this calculator useful?

Trapezoidal rule for numerical integration

The trapezoidal rule is the simplest method for approximating definite integrals. Based on approximating f with line segments between consecutive points, forming trapezoids whose total area estimates the integral. Known since antiquity (Newton-Cotes 1, already in Heron and Archimedes).

Basic formula

For interval [a,b] with two points a and b:

integral_{a}^{b} f(x) dx ~ (b-a)/2 * [f(a) + f(b)]

Exact for linear functions. Error: -(b-a)^3/12 * f''(xi) for some xi in (a,b).

Composite trapezoidal

Divides [a,b] in n sub-intervals of width h = (b-a)/n and applies trapezoidal to each:

integral ~ h [f(x_0)/2 + f(x_1) + f(x_2) + ... + f(x_{n-1}) + f(x_n)/2]

where x_i = a + i h. Global error: -(b-a) h^2/12 * max|f''|. Order 2 convergence (error /4 halving h).

Linear polynomial exactness

Trapezoidal is EXACT for linear f (ax + b). For quadratic f = x^2 on [0,1] (exact = 1/3): trapezoidal n=10 gives 0.335 (error 1.5e-3); n=100 gives 0.33335 (error 1.7e-5). For polynomials degree >= 2 error proportional to max|f''|.

Richardson extrapolation

Idea: if T_h is trapezoidal approximation with step h, then true value I is given by T_h - C h^2 - C2 h^4 - ... Combining T_h and T_{h/2} eliminates C h^2:

I ~ (4 T_{h/2} - T_h) / 3 (= refined Simpson 1/3)

Iterating eliminates successive orders. Result: Romberg table.

Romberg table

Romberg algorithm builds matrix R[k][j]:

  • R[k][0] = trapezoidal with 2^k intervals.
  • R[k][j] = R[k][j-1] + (R[k][j-1] - R[k-1][j-1]) / (4^j - 1).

R[k][k] (diagonal) has order 2(k+1). For k=4 order 10! Exponential convergence for regular (analytic) functions.

Numerical examples

Integral of sin(x) on [0, pi] (exact = 2):

  • Trapezoidal n=10: 1.9835 (error 1.6e-2)
  • Trapezoidal n=20: 1.9959 (error 4.1e-3, /4 of above)
  • Trapezoidal n=40: 1.9990 (error 1.0e-3, /4)
  • Romberg 4 levels: 1.99999992 (error 8e-8)
  • Romberg 6 levels: machine precision

Trapezoidal advantages

  • Very simple implementation, closed formula.
  • Cost: 1 f evaluation per point (n+1 total).
  • Exact for linear (useful for adaptive grid).
  • Numerically stable (formula with positive coefficients).
  • Basis for more sophisticated methods (Romberg, Simpson).

Disadvantages

  • Slow convergence: order 2 (error /4 halving h).
  • Inefficient for extreme accuracy: needs huge n.
  • Overestimates for convex functions (positive curvature).
  • Underestimates for concave functions (negative curvature).
  • For highly oscillating integrals: needs adaptive sampling.

Trapezoidal vs Simpson

Trapezoidal (order 2) and Simpson (order 4) have similar costs but very different accuracies. For same n:

  • Trapezoidal: error O(h^2), approximating polynomial degree 1.
  • Simpson 1/3: error O(h^4), approximating polynomial degree 2.
  • For smooth functions, Simpson typically 100-1000x more accurate.
  • For discontinuous functions or few points, trapezoidal may be preferable (less overfitting).

Adaptive quadrature

Strategy: divide [a,b] in sub-intervals and apply trapezoidal (or Simpson) until estimated error < tolerance. Error estimate via difference T_h and T_{h/2}: error_h ~ T_h - T_{h/2}. Result: small steps where f varies rapidly, large steps where flat.

History

Known to ancients (Babylonians, Archimedes). Newton-Cotes 1 (closed formula with 2 points) is simplest case. Roger Cotes 1722 systematizes. Werner Romberg 1955 introduces systematic Richardson extrapolation. Today basis of professional libraries (scipy.integrate.romb, MATLAB trapz, numpy.trapz).

The trapezoidal error and the Euler-Maclaurin formula

The error of the trapezoidal rule has a precise form: for a single interval it is -(b-a)^3/12 f''(xi) for some interior point xi, and in the composite version it becomes -(b-a) h^2/12 f''(xi), hence order two. The sign of the second derivative determines the direction of the error: for convex functions (f'' > 0) the trapezoids systematically overestimate the integral, because the chord lies above the curve; for concave functions they underestimate it.

The Euler-Maclaurin formula gives the full expansion of the error in even powers of h, with coefficients given by the Bernoulli numbers and the values of the derivatives at the interval endpoints. It is this structure, with only even terms, that makes Richardson extrapolation so effective: eliminating one term at a time yields the Romberg table, which rapidly derives from the trapezoidal rule approximations of ever higher order.

The surprise: spectral convergence on periodic functions

The trapezoidal rule has a surprising property: applied to a smooth periodic function, integrated over a full period, it converges exponentially instead of as h^2. The reason emerges from the Euler-Maclaurin formula: for a periodic integrand the values of the derivatives at the two endpoints coincide and all the correction terms cancel in pairs, leaving an error that decreases faster than any power of h.

This spectral convergence explains why the trapezoidal rule, apparently the crudest method, is in fact the optimal tool for Fourier analysis and the discrete transform (DFT), where signals are periodic by construction. The same idea makes the trapezoidal rule the method of choice for contour integrals in the complex plane, where the closed path is naturally periodic. An elegant example of how an elementary method can excel in the right context.

Updated July 2026

Frequently Asked Questions

What is the trapezoidal rule?

It is a numerical integration method that approximates the area under a curve by replacing it with trapezoids, that is, joining the function values with straight-line segments. The composite formula is h[(f(a)+f(b))/2 + sum of interior values], where h is the width of the subintervals.

What precision does the trapezoidal rule have?

It is a second-order method: the global error is proportional to h^2, so halving the subinterval width reduces the error by a factor of four. It is less accurate than Simpson's rule (fourth order) but simpler and more robust.

Why does the trapezoidal rule overestimate convex functions?

Because for a convex function (upward curvature, f'' > 0) the chord joining two points always lies above the curve, so the trapezoid area is larger than the real area. For concave functions the opposite happens: the trapezoids underestimate the integral.

What is Richardson extrapolation?

It is a technique that combines results obtained with different steps (h and h/2) to eliminate the dominant error term and obtain a more accurate estimate. Applied repeatedly to the trapezoidal rule it generates the Romberg table, which converges very rapidly.

What is the Romberg table?

It is a triangular scheme that starts from trapezoidal estimates with doubled numbers of subintervals and repeatedly applies Richardson extrapolation. Each column eliminates one error order: the first extrapolation recovers Simpson's rule, the later ones give ever higher orders.

When is the trapezoidal rule exact?

For linear functions (straight lines) the trapezoidal rule is exact, with zero error, because the trapezoid coincides exactly with the area under the line. For polynomials of degree two or higher the error is no longer zero, since the curve departs from the connecting segments.

Why does the trapezoidal rule work so well on periodic functions?

For a smooth periodic function integrated over a period, the Euler-Maclaurin formula shows that all the correction terms at the endpoints cancel, leaving exponential convergence instead of O(h^2). This is why the trapezoidal rule is optimal in Fourier analysis and contour integrals.

Trapezoidal or Simpson, which is better?

Simpson is more accurate (fourth order versus two) for the same evaluations on smooth functions, and is generally preferable. The trapezoidal rule remains simpler, more robust and unbeatable on smooth periodic integrands, where it achieves spectral convergence that Simpson does not offer.

Comments (0)

No comments yet. Be the first!