Trapezoidal Rule

Goal of this chapter

Understand the derivation of the trapezoidal rule and the composite trapezoidal rule, the $O(h^2)$ error estimate, and how Richardson extrapolation improves the accuracy.

Prerequisites

Contents

1. Basic Trapezoidal Rule

We approximate the definite integral of a function $f(x)$ over an interval $[a,b]$ by the area of the trapezoid bounded by the straight line joining the two endpoints.

Basic trapezoidal rule

$$\displaystyle\int_a^b f(x)\,dx \approx \dfrac{b-a}{2}\bigl[f(a) + f(b)\bigr]$$

This amounts to interpolating $f$ on $[a,b]$ by a degree-1 polynomial (a straight line) and taking the area beneath it. It has first-order accuracy and is exact when $f$ is linear.

In the animation below we divide $[0,1]$ into $n$ equal parts, approximate each subinterval by a trapezoid, and write the resulting sum as the approximation $T_n$ (here $n$ is the number of trapezoids the interval is split into). The larger $n$ is (the finer the subdivision), the closer $T_n$ gets to the true value of the integral. The exact formula for $T_n$ is given in Section 2 (the composite trapezoidal rule).

\(\displaystyle I=\int_0^1 e^x\,dx=e-1=1.718282\)
0 / 0
Figure 1. Geometric meaning of the trapezoidal rule (animation). The area under the curve $y = f(x) = e^x$ (the true integral $\displaystyle\int_0^1 e^x\,dx = e-1 \approx 1.71828$) is approximated with trapezoids. Use "▶ Auto-play" or "Next / Prev" to advance one step at a time; each time the subdivision count $n$ doubles, $1 \to 2 \to 4 \to 8 \to 16 \to 32$, the trapezoids become finer and the estimate $T_n$ converges to the true value. The light-red region is the true area and the blue trapezoids are the approximation; the log shows the error shrinking by about $1/4$ ($O(h^2)$) at each step.

2. Composite Trapezoidal Rule

Divide $[a,b]$ into $n$ equal parts and sum the trapezoidal rule applied to each subinterval. With step size $h = (b-a)/n$ and nodes $x_k = a + kh$,

Composite trapezoidal rule

$$\displaystyle\int_a^b f(x)\,dx \approx T_n = h\!\left[\dfrac{f(a)}{2} + \displaystyle\sum_{k=1}^{n-1} f(x_k) + \dfrac{f(b)}{2}\right]$$

The two endpoints are summed with weight $1/2$ and the interior nodes with weight $1$.

3. Error Estimate

Theorem (error of the composite trapezoidal rule)

If $f \in C^2[a,b]$, then

$$\displaystyle\int_a^b f(x)\,dx - T_n = -\dfrac{(b-a)h^2}{12}\,f''(\xi), \qquad \xi \in (a,b)$$

that is, the error is $O(h^2)$.

Halving the step size $h$ reduces the error by roughly $1/4$. This property underlies Richardson extrapolation and Romberg integration (see also Section 4).

log h log |error| Trapezoid O(h²) Simpson O(h⁴) slope 2 slope 4
Figure 2. Relationship between the step size $h$ and the error on a log-log scale. An error $\sim C h^p$ is a straight line whose slope is the order of convergence $p$. The trapezoidal rule has slope 2 ($O(h^2)$) and Simpson's rule slope 4 ($O(h^4)$). The smaller $h$ becomes (moving left), the smaller both errors get, and Simpson's rule, with the steeper slope, decreases faster.

4. Endpoint Correction

By the Euler-Maclaurin formula, the error of the composite trapezoidal rule can be expanded as a power series in $h$:

$$T_n - I = \displaystyle\sum_{k=1}^{p} \dfrac{B_{2k}}{(2k)!}\,h^{2k}\bigl[f^{(2k-1)}(b) - f^{(2k-1)}(a)\bigr] + O(h^{2p+2})$$

Here the $B_{2k}$ are the Bernoulli numbers ($B_2 = \tfrac{1}{6},\ B_4 = -\tfrac{1}{30},\ \ldots$). If the endpoint derivative values $f^{(2k-1)}(a),\,f^{(2k-1)}(b)$ are known, one can subtract the correction terms of this expansion from $T_n$ to raise the accuracy. This is called endpoint correction (gradient correction).

First-order endpoint correction

Use only the $k=1$ term. Since $\dfrac{B_2}{2!} = \dfrac{1}{12}$, the leading error term is $\dfrac{h^2}{12}\bigl[f'(b) - f'(a)\bigr]$. Subtracting it from $T_n$,

Endpoint-corrected trapezoidal rule

$$T_n^{\text{corr}} = T_n - \dfrac{h^2}{12}\bigl[f'(b) - f'(a)\bigr]$$

the $h^2$ term cancels and the error of $T_n^{\text{corr}}$ improves to $O(h^4)$. The advantage is that, knowing only the first derivatives $f'(a),\,f'(b)$ at the endpoints, the accuracy gains two orders without any additional function evaluations.

Second-order endpoint correction

Correcting further, up to the $k=2$ term (which uses $f^{(3)}$), gives $O(h^6)$. Since $\dfrac{B_4}{4!} = \dfrac{-1/30}{24} = -\dfrac{1}{720}$, the sign of the correction term becomes positive, and

Trapezoidal rule corrected to second order

$$T_n^{\text{corr2}} = T_n - \dfrac{h^2}{12}\bigl[f'(b) - f'(a)\bigr] + \dfrac{h^4}{720}\bigl[f^{(3)}(b) - f^{(3)}(a)\bigr]$$

Both the $h^2$ and $h^4$ terms cancel, so the error of $T_n^{\text{corr2}}$ is $O(h^6)$. In general, the larger $k$ is the higher the endpoint derivatives $f^{(2k-1)}(a),\,f^{(2k-1)}(b)$ required, but each correction raises the accuracy by two more orders.

Worked example: the effect of the correction

Take $\displaystyle\int_0^1 e^x\,dx$. For $f(x)=e^x$ we have $f'(x)=f^{(3)}(x)=e^x$, so $f'(1)-f'(0) = f^{(3)}(1)-f^{(3)}(0) = e - 1$. We correct the value $T_4 = 1.727222$ from Figure 1 ($n=4$, error $8.94\times10^{-3}$), taking $h=\tfrac14$.

First-order correction ($O(h^4)$):

$$T_4^{\text{corr}} = T_4 - \dfrac{h^2}{12}\,(e-1) = 1.727222 - 0.008949 = 1.718273$$

The error from the true value $e-1 = 1.718282$ is $9.3\times10^{-6}$, about $1/1000$ of the uncorrected error.

Second-order correction ($O(h^6)$): adding the $f^{(3)}$ term, with $\dfrac{h^4}{720}(e-1) = 0.0000093$,

$$T_4^{\text{corr2}} = T_4 - \dfrac{h^2}{12}(e-1) + \dfrac{h^4}{720}(e-1) = 1.727222 - 0.008949 + 0.0000093 = 1.718282$$

The error drops to $1.4\times10^{-8}$, a further reduction of about $1/700$ over the first-order correction (roughly $1/600{,}000$ of the uncorrected error).

ApproximationValueError from the true value
$T_4$ (uncorrected)1.727221905$8.94\times10^{-3}$
$T_4^{\text{corr}}$ (first order, $O(h^4)$)1.718272520$9.31\times10^{-6}$
$T_4^{\text{corr2}}$ (second order, $O(h^6)$)1.718281842$1.4\times10^{-8}$
True value $e-1$1.718281828

Adding one endpoint derivative at a time improves the accuracy by orders of magnitude without increasing $n$.

A derivative-free alternative

Endpoint correction needs $f'(a),\,f'(b)$, but there is also a way to raise the accuracy without using any derivatives at all, using the same Euler-Maclaurin expansion (only even powers $h^2,\,h^4,\,\ldots$). It combines several $T_n$ at different step sizes to cancel the $h^{2k}$ terms one by one: this is Richardson extrapolation, and Romberg integration applies it repeatedly in a table. When derivatives are not available, this is the practical choice.

5. Super-convergence for Periodic Functions

If $f$ is a smooth periodic function of period $T = b - a$, then $f^{(k)}(a) = f^{(k)}(b)$ holds for every $k$, so all the correction terms of the Euler-Maclaurin expansion vanish. As a result, the trapezoidal rule converges exponentially.

This is extremely important in practice: for integrating periodic functions the trapezoidal rule is one of the best methods available.

Example 1: trigonometric polynomials are integrated exactly

$\sin^2 x = \dfrac{1 - \cos 2x}{2}$ is a trigonometric polynomial of frequency up to 2. Its true value over one period $[0,2\pi]$ is

$$\int_0^{2\pi}\sin^2 x\,dx = \pi \approx 3.1415927$$
0 / 0
Figure 3. Animation integrating the integrand $f(x)=\sin^2 x$ on $[0,2\pi]$ with the trapezoidal rule. Light red is the true area $\pi$ and the blue trapezoids are the approximation $T_n$. Use "▶ Auto-play" or "Next / Prev" to vary the subdivision count $n$ over $1,2,3,4,8$. Since $\sin^2 x$ is a trigonometric polynomial of frequency 2, $T_n$ equals $\pi$ exactly for $n\ge 3$ (for $n=1,2$ every sample lands on a zero, giving $T_n=0$).

The composite trapezoidal rule ($h = 2\pi/n$) gives the following values.

$n$$T_n$Error from $\pi$
10$3.1\times10^{0}$
20$3.1\times10^{0}$
33.14159270 (exact)
43.14159270 (exact)
83.14159270 (exact)

For $n \ge 3$ it equals the true value $\pi$ exactly. This is because, over one full period, the trapezoidal rule integrates a trigonometric polynomial with zero error once the subdivision count $n$ exceeds the highest frequency of the integrand (here the frequency is 2, so $n \ge 3$). For $n = 1, 2$ the frequency-2 component is aliased onto the mean, producing an error. Likewise the simplest case $\displaystyle\int_0^{2\pi}\sin x\,dx = 0$ is obtained exactly as $0$ for every $n$.

This exactness, however, is a privilege of integrating over one complete period. Computing the half-period integral $\displaystyle\int_0^{\pi}\sin^2 x\,dx = \dfrac{\pi}{2}$ with the trapezoidal rule does not benefit from the periodicity and reverts to the usual $O(h^2)$ error.

Example 2: exponential convergence for a non-polynomial integrand

If the integrand is not a trigonometric polynomial the result is never exact, but for a smooth periodic function it still converges to the true value exponentially. The Fourier series of $e^{\cos x}$ is infinite, so it is never exact for finite $n$; the true value can be written using the modified Bessel function of the first kind $I_0$ as

$$\int_0^{2\pi}e^{\cos x}\,dx = 2\pi I_0(1) \approx 7.9549265$$
0 / 0
Figure 4. Animation integrating the integrand $f(x)=e^{\cos x}$ on $[0,2\pi]$ with the trapezoidal rule. Light red is the true area $2\pi I_0(1)\approx 7.955$ and the blue trapezoids are the approximation $T_n$. Use "▶ Auto-play" or "Next / Prev" to double the subdivision count $n$ over $1,2,4,8,16$. It is not a trigonometric polynomial, so the result is never exact, but because the integrand is a smooth periodic function the error decreases exponentially and $T_n$ converges rapidly to the true value.

The composite trapezoidal rule gives the following values $T_n$ and errors.

$n$$T_n$Error from the true value
29.6953563$1.7\times10^{0}$
47.9893277$3.4\times10^{-2}$
67.9552091$2.8\times10^{-4}$
87.9549278$1.3\times10^{-6}$
107.9549265$3.5\times10^{-9}$

Simply going from $n = 4$ to $n = 8$ cuts the error from $3.4\times10^{-2}$ to $1.3\times10^{-6}$, a factor of about $1/20{,}000$. This far exceeds $O(h^2)$ (where doubling $n$ reduces the error only by $1/4$): the error decreases geometrically in $n$, which is super-convergence. The smoother and more periodic the integrand, the more the Euler-Maclaurin correction terms vanish one after another, giving this fast convergence.

Why correcting only the two ends works

It is not that "only the boundary trapezoids are special." The local error of every subinterval is governed by the curvature, $-\dfrac{h^3}{12}f''(\xi_k)$, and is of the same order. What is special is that, when the panels are summed, the interior contributions cancel.

In the Euler-Maclaurin form, the error contributed by a single panel can be written as the difference of derivatives at that panel's two endpoints. Summing over all panels, each interior node $x_k$ receives $+f^{(2k-1)}(x_k)$ from the panel on its left and $-f^{(2k-1)}(x_k)$ from the panel on its right; with opposite signs they cancel exactly (a telescoping sum). Only the outermost terms $-f^{(2k-1)}(a)$ and $+f^{(2k-1)}(b)$ survive, which is why only the endpoints appear in the correction.

Each panel emits ∓f′ at its two ends ── interior cancels, only the ends remain panel 1 panel 2 panel 3 panel 4 + + + + = 0 = 0 = 0 −f′(a) +f′(b) x₀ = a x₁ x₂ x₃ x₄ = b
Figure 5. Why endpoint correction works using only the two ends. Each subinterval (panel $i$) contributes $-f'(x_{i-1})$ at its left end and $+f'(x_i)$ at its right end to the error. At every interior node, the $+f'(x_k)$ from the panel on the left and the $-f'(x_k)$ from the panel on the right cancel to $0$. The only terms that survive are $-f'(a)$ at the far left and $+f'(b)$ at the far right, which is exactly the leading error $(h^2/12)[f'(b)-f'(a)]$.

Since words alone may not convince, let us write out how the sum actually collapses. The local error on panel $i$ (from $x_{i-1}$ to $x_i$) is $I_i - T_i = -\dfrac{h^3}{12}f''(\xi_i)$, and the key is to read the curvature $f''$ as a change of slope. Because $\displaystyle\int_{x_{i-1}}^{x_i} f''\,dx = f'(x_i) - f'(x_{i-1})$, that is $h\,f''(\xi_i) \approx f'(x_i) - f'(x_{i-1})$, each panel's error is the difference of the slopes at its two ends ($I_i$ is the exact integral over panel $i$ and $T_i$ its trapezoidal value).

$$I_i - T_i \;\approx\; -\frac{h^2}{12}\,\bigl[\,f'(x_i) - f'(x_{i-1})\,\bigr]$$

Summing over all panels gives a telescoping sum in which neighboring terms cancel one after another.

$$\begin{aligned} I - T_n \;&=\; -\frac{h^2}{12}\sum_{i=1}^{n}\bigl[\,f'(x_i) - f'(x_{i-1})\,\bigr] \\[2pt] &=\; -\frac{h^2}{12}\Bigl[\,\bigl(f'(x_1)-f'(x_0)\bigr) + \bigl(f'(x_2)-f'(x_1)\bigr) + \cdots + \bigl(f'(x_n)-f'(x_{n-1})\bigr)\,\Bigr] \\[2pt] &=\; -\frac{h^2}{12}\,\bigl[\,f'(x_n) - f'(x_0)\,\bigr] \;=\; -\frac{h^2}{12}\,\bigl[\,f'(b) - f'(a)\,\bigr] \end{aligned}$$

The slope $f'(x_k)$ at every interior node $x_k$ ($1 \le k \le n-1$) appears exactly once in two neighboring brackets, as $+f'(x_k)$ and $-f'(x_k)$. The $+f'(x_k)$ emitted at the right end of the panel on its left and the $-f'(x_k)$ emitted at the left end of the panel on its right have opposite signs and cancel exactly. Only the two outermost terms survive, $-f'(x_0)=-f'(a)$ and $+f'(x_n)=+f'(b)$. Written as the error of the trapezoidal value this is $T_n - I = \dfrac{h^2}{12}\bigl[f'(b)-f'(a)\bigr]$, exactly the term subtracted in the endpoint correction. This is the precise meaning of "the interior cancels and only the two ends remain."

Most fundamentally, the trapezoidal rule has zero error when a smooth periodic function is integrated over one full period, and the error on a finite interval $[a,b]$ comes entirely from the mismatch at the seam of the periodic extension of $f$, namely $f^{(2k-1)}(b) - f^{(2k-1)}(a)$. Interior nodes have no seam, so they produce no error. If $f$ is periodic this mismatch vanishes at every order, giving the super-convergence of Section 5. Endpoint correction and super-convergence are two sides of the same fact: the error is a boundary quantity.

6. Singular Endpoints

The composite trapezoidal rule $T_n = h\left[\dfrac{f(a)}{2} + \displaystyle\sum_{k=1}^{n-1} f(x_k) + \dfrac{f(b)}{2}\right]$ contains the endpoint values $f(a),\,f(b)$ directly. So when $f$ is undefined at an endpoint (its value does not exist, or it diverges), the rule cannot be applied as is. This is a question about improper integrals; we organize it into three cases.

Case 1: the limit at the endpoint is finite (a removable singularity)

For example, $f(x)=\dfrac{\sin x}{x}$ is undefined at $x=0$, yet $\displaystyle\lim_{x\to 0}\frac{\sin x}{x}=1$. For such a removable singularity, redefining $f(a)$ by the limit value ($f(0):=1$) makes the function continuous and the trapezoidal rule applies directly. Once $f$ is again twice continuously differentiable up to the endpoints, the $O(h^2)$ error estimate holds as well.

Case 2: the function diverges at the endpoint but the integral converges

Example: $\displaystyle\int_0^1 \frac{1}{\sqrt{x}}\,dx = 2$ converges, but $f(0)=+\infty$, so $f(a)$ cannot be written down. This is the situation the trapezoidal rule handles worst; the following remedies are available.

  • Use an open quadrature rule: switch to a rule that does not evaluate the endpoints, such as the midpoint rule, so the singular point is never touched.
  • Remove the singularity by a change of variable: a suitable substitution makes the integrand smooth. For endpoint singularities the double-exponential (DE, tanh-sinh) formula of Takahashi and Mori is especially powerful and is widely used in practice.
  • Separate the singular part analytically: factor out the singular factor, as in $f(x)=\dfrac{g(x)}{\sqrt{x}}$, integrate it by hand, and apply numerical integration only to the remaining smooth part.
  • Use weighted quadrature: absorb the endpoint singularity into a weight function, as in Gauss-Jacobi quadrature.

Example: removing a singularity by a change of variable

In $\displaystyle\int_0^1 \frac{1}{\sqrt{x}}\,dx$, setting $x=t^2$ ($dx = 2t\,dt$) gives

$$\int_0^1 \frac{1}{\sqrt{x}}\,dx = \int_0^1 \frac{1}{t}\cdot 2t\,dt = \int_0^1 2\,dt = 2$$

The integrand becomes the constant $2$ (infinitely differentiable) and the singularity disappears. After the transformation the trapezoidal rule returns the exact value $2$ regardless of the step size.

Case 3: the integral itself diverges

Example: $\displaystyle\int_0^1 \frac{1}{x}\,dx = \infty$. Since the value of the integral does not exist, no numerical method can produce a correct finite value (the value keeps growing as the mesh is refined). Here the rule is used to detect the divergence numerically.

Summary of the three cases

Cases 1 to 3 above can be summarized as follows.

CaseEndpoint situationTrapezoidal ruleRemedy
1Finite limit (removable)Usable once the value is filled inDefine $f(a)$ by the limit value
2Diverges but the integral convergesNot usable as isMidpoint rule / change of variable (DE) / separating the singular part
3The integral divergesNot usableConclude divergence analytically

The key point is that the $O(h^2)$ error of the trapezoidal rule assumes $f \in C^2[a,b]$ (twice continuously differentiable up to the endpoints). If $f$ or its derivatives blow up at an endpoint, then even when a value can be filled in and the rule computed, convergence becomes extremely slow and the accuracy guarantee is lost. The basic strategy for endpoint singularities is therefore to make the integrand smooth first, by a change of variable or by separating the singular part, and only then apply the trapezoidal rule.

7. FAQ

Q1. What is the trapezoidal rule?

It is a method of numerical integration that approximates a definite integral by the area of the trapezoid joining the two endpoint values of the integrand. In the composite rule the interval is divided into $n$ equal parts and a trapezoidal approximation is summed over each subinterval.

Q2. How large is the error of the trapezoidal rule?

The error of the composite trapezoidal rule is $O(h^2)$. Halving the step size $h$ reduces the error by roughly $1/4$. Endpoint correction can improve the accuracy further.

Q3. Are there cases where the trapezoidal rule is especially accurate?

When integrating a periodic function over one full period, the trapezoidal rule exhibits super-convergence (exponential convergence). This is because all the correction terms of the Euler-Maclaurin expansion vanish.

8. References

  • Wikipedia, "Trapezoidal rule"
  • L. N. Trefethen & J. A. C. Weideman, "The Exponentially Convergent Trapezoidal Rule," SIAM Review, 56(3), 2014.
  • R. L. Burden & J. D. Faires, Numerical Analysis, 10th ed., Cengage, 2016.