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
- Chapter 35: Numerical Integration
- Basics of the Taylor expansion
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
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).
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
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
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).
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
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
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)$):
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$,
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).
| Approximation | Value | Error 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
The composite trapezoidal rule ($h = 2\pi/n$) gives the following values.
| $n$ | $T_n$ | Error from $\pi$ |
|---|---|---|
| 1 | 0 | $3.1\times10^{0}$ |
| 2 | 0 | $3.1\times10^{0}$ |
| 3 | 3.1415927 | 0 (exact) |
| 4 | 3.1415927 | 0 (exact) |
| 8 | 3.1415927 | 0 (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
The composite trapezoidal rule gives the following values $T_n$ and errors.
| $n$ | $T_n$ | Error from the true value |
|---|---|---|
| 2 | 9.6953563 | $1.7\times10^{0}$ |
| 4 | 7.9893277 | $3.4\times10^{-2}$ |
| 6 | 7.9552091 | $2.8\times10^{-4}$ |
| 8 | 7.9549278 | $1.3\times10^{-6}$ |
| 10 | 7.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.
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).
Summing over all panels gives a telescoping sum in which neighboring terms cancel one after another.
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
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.
| Case | Endpoint situation | Trapezoidal rule | Remedy |
|---|---|---|---|
| 1 | Finite limit (removable) | Usable once the value is filled in | Define $f(a)$ by the limit value |
| 2 | Diverges but the integral converges | Not usable as is | Midpoint rule / change of variable (DE) / separating the singular part |
| 3 | The integral diverges | Not usable | Conclude 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.