Subsection 1.11.2 The trapezoidal rule
¶Consider again the area represented by the integral \(\int_{x_{j-1}}^{x_j} f(x)\,\dee{x}\text{.}\) The trapezoidal rule 3 (unsurprisingly) approximates this area by a trapezoid 4 whose vertices lie at
\begin{gather*}
(x_{j-1},0), (x_{j-1},f(x_{j-1})), (x_{j},f(x_{j})) \text{ and } (x_{j},0).
\end{gather*}
The trapezoidal approximation of the integral \(\int_{x_{j-1}}^{x_j} f(x)\,\dee{x}\) is the shaded region in the figure on the right above. It has width \(x_j-x_{j-1}=\De x\text{.}\) Its left hand side has height \(f(x_{j-1})\) and its right hand side has height \(f(x_j)\text{.}\)
As the figure below shows, the area of a trapezoid is its width times its average height.
So the trapezoidal rule approximates each subintegral by
\begin{equation*}
\int_{x_{j-1}}^{x_j} f(x)\,\dee{x}\approx \tfrac{f(x_{j-1})+f(x_j)}{2}\De x
\end{equation*}
Applying this approximation to each subinterval and then summing the result gives us the following approximation of the full integral
\begin{align*}
\int_a^b f(x)\,\dee{x}&=
\int_{x_0}^{x_1} f(x)\,\dee{x} + \int_{x_1}^{x_2} f(x)\,\dee{x}
+\cdots+\int_{x_{n-1}}^{x_n} f(x)\,\dee{x}\\
& \approx \tfrac{f(x_0)+f(x_1)}{2}\De x + \tfrac{f(x_1)+f(x_2)}{2}\De x
+ \cdots +
\tfrac{f(x_{n-1})+f(x_n)}{2}\De x\\
&=
\Big[\half f(x_0)+f(x_1)+f(x_2)+\cdots+ f(x_{n-1})+\half f(x_n)\Big]\De x
\end{align*}
So notice that the approximation has a very similar form to the midpoint rule, excepting that
- we evaluate the function at the \(x_j\)'s rather than at the midpoints, and
- we multiply the value of the function at the endpoints \(x_0,x_n\) by \(\frac12\text{.}\)
In summary:
Equation 1.11.6 The trapezoidal rule
The trapezoidal rule approximation is
\begin{align*}
\int_a^b f(x)\,\dee{x}
&\approx\Big[\half f(x_0)+f(x_1)+f(x_2)+\cdots+ f(x_{n-1})+\half f(x_n)\Big]\De x
\end{align*}
where
\begin{gather*}
\De x = \tfrac{b-a}{n},\quad
x_0=a,\quad x_1=a+\De x,\quad
x_2=a+2\De x,\quad
\cdots,\quad x_{n-1}=b-\De x,\quad
x_n=b
\end{gather*}
To compare and contrast we apply the trapezoidal rule to the examples we did above with the midpoint rule.
Example 1.11.7 \(\int_0^1 \frac{4}{1+x^2}\,\dee{x}\) — using the trapezoidal rule
Solution: We proceed very similarly to Example 1.11.3 and again use \(n=8\) steps.
- We again have \(f(x)=\frac{4}{1+x^2}\text{,}\) \(a=0\text{,}\) \(b=1\text{,}\) \(\De x=\tfrac{1}{8}\) and
\begin{align*}
x_0&=0 & x_1&=\tfrac{1}{8} & x_2&=\tfrac{2}{8}
&& \cdots &
x_7&=\tfrac{7}{8}&
x_8&=\tfrac{8}{8}=1
\end{align*}
- Applying the trapezoidal rule, Equation 1.11.6, gives
\begin{align*}
&\int_0^1 \frac{4}{1+x^2}\,\dee{x}
\approx
\bigg[\frac{1}{2}\overbrace{\frac{4}{1\!+\!x_0^2}}^{f(x_0)}
+\overbrace{\frac{4}{1\!+\!x_1^2}}^{f(x_1)}
+\!\cdots\!
+\overbrace{\frac{4}{1\!+\!x_7^2}}^{f(x_{n-1})}
+\frac{1}{2}\overbrace{\frac{4}{1\!+\!x_8^2}}^{f(x_n)}
\bigg]\De x\\
&\hskip0.25in=\bigg[\frac{1}{2}\frac{4}{1+0^2}+
\frac{4}{1+\tfrac{1}{8^2}}+
\frac{4}{1+\tfrac{2^2}{8^2}}+
\frac{4}{1+\tfrac{3^2}{8^2}}\\
&\hskip0.5in +\frac{4}{1+\tfrac{4^2}{8^2}}+
\frac{4}{1+\tfrac{5^2}{8^2}}+
\frac{4}{1+\tfrac{6^2}{8^2}}+
\frac{4}{1+\tfrac{7^2}{8^2}}+
\frac{1}{2}\frac{4}{1+\tfrac{8^2}{8^2}}\bigg]\frac{1}{8}\\
&\hskip0.25in=\Big[\frac{1}{2}\times 4+
3.939+
3.765+
3.507\\
&\hskip0.5in +3.2+
2.876+
2.56+
2.266+
\frac{1}{2}\times 2\Big]\frac{1}{8}\\
&\hskip0.25in =3.139
\end{align*}
to three decimal places.
- The exact value of the integral is still \(\pi\text{.}\) So the error in the approximation generated by eight steps of the trapezoidal rule is \(|3.139-\pi|=0.0026\text{,}\) which is \(100\tfrac{|3.139-\pi|}{\pi}\% =0.08\%\) of the exact answer. Notice that this is roughly twice the error that we achieved using the midpoint rule in Example 1.11.3.
Let us also redo Example 1.11.5 using the trapezoidal rule.
Example 1.11.8 \(\int_0^\pi\sin x\,\dee{x}\) — using the trapezoidal rule
Solution: We proceed very similarly to Example 1.11.5 and again use \(n=8\) steps.
- We again have \(a=0\text{,}\) \(b=\pi\text{,}\) \(\De x=\tfrac{\pi}{8}\) and
\begin{align*}
x_0&=0& x_1&=\tfrac{\pi}{8}&
x_2&=\tfrac{2\pi}{8}& \cdots&&
x_7&=\tfrac{7\pi}{8}&
x_8&=\tfrac{8\pi}{8}=\pi
\end{align*}
- Applying the trapezoidal rule, Equation 1.11.6, gives
\begin{align*}
&\int_0^\pi\sin x\,\dee{x}
\approx
\Big[\half\sin(x_0)+\sin(x_1)+\cdots+\sin(x_7)+\half\sin(x_8)\Big]\De x\\
&=\Big[\half\sin0 +
\sin\tfrac{\pi}{8}+
\sin\tfrac{2\pi}{8}+
\sin\tfrac{3\pi}{8}+
\sin\tfrac{4\pi}{8}+
\sin\tfrac{5\pi}{8}\\
&\hskip0.5in+\sin\tfrac{6\pi}{8}+
\sin\tfrac{7\pi}{8}+
\half\sin\tfrac{8\pi}{8}\Big]\tfrac{\pi}{8}\\
&=\Big[\half\!\times\! 0+
0.3827+
0.7071+
0.9239+
1.0000+
0.9239+\\
&\hskip0.5in
0.7071+
0.3827+
\half\!\times\! 0\Big]\times 0.3927\\
&=5.0274\times 0.3927
=1.974
\end{align*}
- The exact answer is \(\int_0^\pi\sin x\,\dee{x}=-\cos x\Big|_0^\pi=2\text{.}\) So with eight steps of the trapezoidal rule we achieved \(100\tfrac{|1.974-2|}{2}=1.3\%\) accuracy. Again this is approximately twice the error we achieved in Example 1.11.5 using the midpoint rule.
These two examples suggest that the midpoint rule is more accurate than the trapezoidal rule. Indeed, this observation is born out by a rigorous analysis of the error — see Section 1.11.4.