Approximate the value of a definite integral by using the midpoint and trapezoidal rules.
Determine the absolute and relative error in using a numerical integration technique.
Estimate the absolute and relative error using an error-bound formula.
Recognize when the midpoint and trapezoidal rules over- or underestimate the true value of an integral.
Use Simpson’s rule to approximate the value of a definite integral to a given accuracy.
The antiderivatives of many functions either cannot be expressed or cannot be expressed easily in closed form (that is, in terms of known functions). Consequently, rather than evaluate definite integrals of these functions directly, we resort to various techniques of numerical integration to approximate their values. In this section, we explore several of these techniques. In addition, we examine the process of estimating the error in using these techniques.
The Midpoint Rule
Earlier in this text we defined the definite integral of a function over an interval as the limit of Riemann sums. In general, any Riemann sum of a function \( f(x)\) over an interval \([a,b]\) may be viewed as an estimate of \(\displaystyle ∫^b_af(x)\,dx\). Recall that a Riemann sum of a function \( f(x)\) over an interval \( [a,b]\) is obtained by selecting a partition
\[ P=\{x_0,x_1,x_2,…,x_n\} \nonumber \]
where \(\quad a=x_0<x_1<x_2<⋯<x_n=b \)
and a set
\[ S=\{x^*_1,x^*_2,…,x^*_n\} \nonumber \]
where \(x_{i−1}≤x^*_i≤x_i \quad \text{for all} \, i.\)
The Riemann sum corresponding to the partition \(P\) and the set \(S\) is given by \(\displaystyle \sum^n_{i=1}f(x^*_i)Δx_i\), where \( Δx_i=x_i−x_{i−1},\) the length of the \( i^{\text{th}}\) subinterval.
The midpoint rule for estimating a definite integral uses a Riemann sum with subintervals of equal width and the midpoints, \( m_i\), of each subinterval in place of \( x^*_i\). Formally, we state a theorem regarding the convergence of the midpoint rule as follows.
The Midpoint Rule
Assume that \( f(x)\) is continuous on \([a,b]\). Let \( n\) be a positive integer and \( Δx=\dfrac{b−a}{n}\). If \( [a,b]\) is divided into \( n\) subintervals, each of length \( Δx\), and \( m_i\) is the midpoint of the \( i^{\text{th}}\) subinterval, set
\[M_n=\sum_{i=1}^nf(m_i)Δx. \nonumber \]
Then \(\displaystyle \lim_{n→∞}M_n=∫^b_af(x)\,dx.\)
As we can see in Figure \(\PageIndex{1}\), if \( f(x)≥0\) over \( [a,b]\), then \(\displaystyle \sum^n_{i=1}f(m_i)Δx\) corresponds to the sum of the areas of rectangles approximating the area between the graph of \( f(x)\) and the \(x\)-axis over \([a,b]\). The graph shows the rectangles corresponding to \(M_4\) for a nonnegative function over a closed interval \([a,b].\)
Example \(\PageIndex{1}\): Using the Midpoint Rule with \(M_4\)
Use the midpoint rule to estimate \(\displaystyle ∫^1_0x^2\,dx\) using four subintervals. Compare the result with the actual value of this integral.
Solution: Each subinterval has length \( Δx=\dfrac{1−0}{4}=\dfrac{1}{4}.\) Therefore, the subintervals consist of
Since \(\dfrac{dy}{dx}=x\), this integral becomes \(\displaystyle ∫^4_1\sqrt{1+x^2}\,dx.\)
If \([1,4]\) is divided into six subintervals, then each subinterval has length \(Δx=\dfrac{4−1}{6}=\dfrac{1}{2}\) and the midpoints of the subintervals are \(\left\{\frac{5}{4},\frac{7}{4},\frac{9}{4},\frac{11}{4},\frac{13}{4},\frac{15}{4}\right\}\). If we set \(f(x)=\sqrt{1+x^2}\),
We can also approximate the value of a definite integral by using trapezoids rather than rectangles. In Figure \(\PageIndex{2}\), the area beneath the curve is approximated by trapezoids rather than by rectangles.
The trapezoidal rule for estimating definite integrals uses trapezoids rather than rectangles to approximate the area under a curve. To gain insight into the final form of the rule, consider the trapezoids shown in Figure \(\PageIndex{2}\). We assume that the length of each subinterval is given by \(Δx\). First, recall that the area of a trapezoid with a height of \(h\) and bases of length \(b_1\) and \(b_2\) is given by \(\text{Area}=\frac{1}{2}h(b_1+b_2)\). We see that the first trapezoid has a height \(Δx\) and parallel bases of length \( f(x_0)\) and \( f(x_1)\). Thus, the area of the first trapezoid in Figure \(\PageIndex{2}\) is
Generalizing, we formally state the following rule.
The Trapezoidal Rule
Assume that \(f(x)\) is continuous over \([a,b]\). Let \(n\) be a positive integer and \(Δx=\dfrac{b−a}{n}\). Let \( [a,b]\) be divided into \(n\) subintervals, each of length \(Δx\), with endpoints at \( P=\{x_0,x_1,x_2…,x_n\}.\)
Before continuing, let’s make a few observations about the trapezoidal rule. First of all, it is useful to note that
\(T_n=\dfrac{1}{2}(L_n+R_n)\) where \(\displaystyle L_n=\sum_{i=1}^nf(x_{i−1})Δx\) and \(\displaystyle R_n=\sum_{i=1}^nf(x_i)Δx.\)
That is, \(L_n\) and \(R_n\) approximate the integral using the left-hand and right-hand endpoints of each subinterval, respectively. In addition, a careful examination of Figure \(\PageIndex{3}\) leads us to make the following observations about using the trapezoidal rules and midpoint rules to estimate the definite integral of a nonnegative function. The trapezoidal rule tends to overestimate the value of a definite integral systematically over intervals where the function is concave up and to underestimate the value of a definite integral systematically over intervals where the function is concave down. On the other hand, the midpoint rule tends to average out these errors somewhat by partially overestimating and partially underestimating the value of the definite integral over these same types of intervals. This leads us to hypothesize that, in general, the midpoint rule tends to be more accurate than the trapezoidal rule.
Example \(\PageIndex{3}\): Using the Trapezoidal Rule
Use the trapezoidal rule to estimate \(\displaystyle ∫^1_0x^2\,dx\) using four subintervals.
Solution
The endpoints of the subintervals consist of elements of the set \(P=\left\{0,\frac{1}{4},\, \frac{1}{2},\, \frac{3}{4},1\right\}\) and \(Δx=\frac{1−0}{4}=\frac{1}{4}.\) Thus,
Use the trapezoidal rule with \( n=2\) to estimate \(\displaystyle ∫^2_1\frac{1}{x}\,dx.\)
Hint
Set \(Δx=\dfrac{1}{2}.\) The endpoints of the subintervals are the elements of the set \(P=\left\{1,\frac{3}{2},2\right\}.\)
Answer
\(\dfrac{17}{24} \approx 0.708333\)
Absolute and Relative Error
An important aspect of using these numerical approximation rules consists of calculating the error in using them for estimating the value of a definite integral. We first need to define absolute error and relative error.
Definition: absolute and relative error
If \(B\) is our estimate of some quantity having an actual value of \(A\), then the absolute error is given by \(|A−B|\).
The relative error is the error as a percentage of the actual value and is given by \[\left\lvert\frac{A−B}{A}\right\rvert⋅100\%. \nonumber \]
Example \(\PageIndex{4}\): Calculating Error in the Midpoint Rule
Calculate the absolute and relative error in the estimate of \(\displaystyle ∫^1_0x^2\,dx\) using the midpoint rule, found in Example \(\PageIndex{1}\).
Solution: The calculated value is \(\displaystyle ∫^1_0x^2\,dx=\frac{1}{3}\) and our estimate from the example is \(M_4=\frac{21}{64}\). Thus, the absolute error is given by \(\left\lvert\frac{1}{3}−\frac{21}{64}\right\rvert=\frac{1}{192}≈0.0052.\)
The relative error is \[\frac{1/192}{1/3}=\frac{1}{64}≈0.015625≈1.6\%.\nonumber \]
Example \(\PageIndex{5}\): Calculating Error in the Trapezoidal Rule
Calculate the absolute and relative error in the estimate of \(\displaystyle ∫^1_0x^2\,dx\) using the trapezoidal rule, found in Example \(\PageIndex{3}\).
Solution: The calculated value is \(\displaystyle ∫^1_0x^2\,dx=\frac{1}{3}\) and our estimate from the example is \(T_4=\frac{11}{32}\). Thus, the absolute error is given by \(\left\lvert\frac{1}{3}−\frac{11}{32}\right\rvert=\frac{1}{96}≈0.0104.\)
The relative error is given by \[\frac{1/96}{1/3}=0.03125≈3.1\%.\nonumber \]
Exercise \(\PageIndex{3}\)
In an earlier checkpoint, we estimated \(\displaystyle ∫^2_1\frac{1}{x}\,dx\) to be \(\frac{24}{35}\) using \(M_2\). The actual value of this integral is \(\ln 2\). Using \(\frac{24}{35}≈0.6857\) and \(\ln 2≈0.6931,\) calculate the absolute error and the relative error.
Hint
Use the previous examples as a guide.
Answer
absolute error \(\approx 0.0074,\) and relative error \(\approx 1.1\%\)
Error Bounds on the Midpoint and Trapezoidal Rules
In the two previous examples, we were able to compare our estimate of an integral with the actual value of the integral; however, we do not typically have this luxury. In general, if we are approximating an integral, we are doing so because we cannot compute the exact value of the integral itself easily. Therefore, it is often helpful to be able to determine an upper bound for the error in an approximation of an integral. The following theorem provides error bounds for the midpoint and trapezoidal rules. The theorem is stated without proof.
Error Bounds for the Midpoint and Trapezoidal Rules
Let \(f(x)\) be a continuous function over \([a,b]\), having a second derivative \(f''(x)\) over this interval. If \(M\) is the maximum value of \(|f''(x)|\) over \([a,b]\), then the upper bounds for the error in using \(M_n\) and \(T_n\) to estimate \(\displaystyle ∫^b_af(x)\,dx\) are
We can use these bounds to determine the value of \(n\) necessary to guarantee that the error in an estimate is less than a specified value.
Example \(\displaystyle \PageIndex{6}\): Determining the Number of Intervals to Use
What value of \(n\) should be used to guarantee that an estimate of \(\displaystyle ∫^1_0e^{x^2}\,dx\) is accurate to within \(0.01\) if we use the midpoint rule?
Solution
We begin by determining the value of \(M\), the maximum value of \( |f''(x)|\) over \( [0,1]\) for \( f(x)=e^{x^2}\). Since \( f′(x)=2xe^{x^2},\) we have
We might have been tempted to round \(8.24\) down and choose \(n=8\), but this would be incorrect because we must have an integer greater than or equal to \(8.24\). We need to keep in mind that the error estimates provide an upper bound only for the error. The actual estimate may, in fact, be a much better approximation than is indicated by the error bound.
Exercise \(\PageIndex{4}\)
Use Equation \(\ref{MidError}\) to find an upper bound for the error in using \(M_4\) to estimate \(\displaystyle ∫^1_0x^2\,dx.\)
Hint
\(f''(x)=2,\) so \(M=2.\)
Answer
\(\dfrac{1}{192}\)
Simpson’s Rule
With the midpoint rule, we estimated areas of regions under curves by using rectangles. In a sense, we approximated the curve with piecewise constant functions. With the trapezoidal rule, we approximated the curve by using piecewise linear functions. What if we were, instead, to approximate a curve using piecewise quadratic functions? With Simpson’s rule, we do just this. We partition the interval into an even number of subintervals, each of equal width. Over the first pair of subintervals we approximate \(\displaystyle ∫^{x_2}_{x_0}f(x)\,dx\) with \(\displaystyle ∫^{x_2}_{x_0}p(x)\,dx\), where \(p(x)=Ax^2+Bx+C\) is the quadratic function passing through \((x_0,f(x_0)), \,(x_1,f(x_1)),\) and \((x_2,f(x_2))\) (Figure \(\PageIndex{4}\)). Over the next pair of subintervals we approximate \(\displaystyle ∫^{x_4}_{x_2}f(x)\,dx\) with the integral of another quadratic function passing through \( (x_2,f(x_2)), \,(x_3,f(x_3)),\) and \((x_4,f(x_4)).\) This process is continued with each successive pair of subintervals.
To understand the formula that we obtain for Simpson’s rule, we begin by deriving a formula for this approximation over the first two subintervals. As we go through the derivation, we need to keep in mind the following relationships:
\[f(x_0)=p(x_0)=Ax_0^2+Bx_0+C \nonumber \]
\[f(x_1)=p(x_1)=Ax_1^2+Bx_1+C \nonumber \]
\[f(x_2)=p(x_2)=Ax_2^2+Bx_2+C \nonumber \]
\(x_2−x_0=2Δx\), where \(Δx\) is the length of a subinterval.
\(x_2+x_0=2x_1,\) since \(x_1=\dfrac{(x_2+x_0)}{2}\).
The pattern continues as we add pairs of subintervals to our approximation. The general rule may be stated as follows.
Simpson’s Rule
Assume that \(f(x)\) is continuous over \([a,b]\). Let \(n\) be a positive even integer and \(Δx=\dfrac{b−a}{n}\). Let \([a,b]\) be divided into \(n\) subintervals, each of length \(Δx\), with endpoints at \(P=\{x_0,x_1,x_2,…,x_n\}.\) Set
Just as the trapezoidal rule is the average of the left-hand and right-hand rules for estimating definite integrals, Simpson’s rule may be obtained from the midpoint and trapezoidal rules by using a weighted average. It can be shown that \(S_{2n}=\left(\frac{2}{3}\right)M_n+\left(\frac{1}{3}\right)T_n\).
It is also possible to put a bound on the error when using Simpson’s rule to approximate a definite integral. The bound in the error is given by the following rule:
Rule: Error Bound for Simpson’s Rule
Let \(f(x)\) be a continuous function over \([a,b]\) having a fourth derivative, \( f^{(4)}(x)\), over this interval. If \(M\) is the maximum value of \(∣f^{(4)}(x)∣\) over \([a,b]\), then the upper bound for the error in using \(S_n\) to estimate \(\displaystyle ∫^b_af(x)\,dx\) is given by
Example \(\displaystyle \PageIndex{7}\): Applying Simpson’s Rule 1
Use \(S_2\) to approximate \(\displaystyle ∫^1_0x^3\,dx\). Estimate a bound for the error in \(S_2\).
Solution
Since \([0,1]\) is divided into two intervals, each subinterval has length \(Δx=\frac{1−0}{2}=\frac{1}{2}\). The endpoints of these subintervals are \(\left\{0,\frac{1}{2},1\right\}\). If we set \(f(x)=x^3,\) then
Since \( f^{(4)}(x)=0\) and consequently \(M=0,\) we see that
Error in \(S_2≤\frac{0(1)^5}{180⋅2^4}=0.\)
This bound indicates that the value obtained through Simpson’s rule is exact. A quick check will verify that, in fact, \(\displaystyle ∫^1_0x^3\,dx=\frac{1}{4}.\)
Example \(\displaystyle \PageIndex{8}\): Applying Simpson’s Rule 2
Use \(S_6\) to estimate the length of the curve \(y=\frac{1}{2}x^2\) over \([1,4].\)
Solution
The length of \(y=\frac{1}{2}x^2\) over \([1,4]\) is \(\displaystyle ∫^4_1\sqrt{1+x^2}\,dx\). If we divide \([1,4]\) into six subintervals, then each subinterval has length \(Δx=\frac{4−1}{6}=\frac{1}{2}\), and the endpoints of the subintervals are \( \left\{1,\frac{3}{2},2,\frac{5}{2},3,\frac{7}{2},4\right\}.\) Setting \( f(x)=\sqrt{1+x^2}\),
We can use numerical integration to estimate the values of definite integrals when a closed form of the integral is difficult to find or when an approximate value only of the definite integral is needed.
The most commonly used techniques for numerical integration are the midpoint rule, trapezoidal rule, and Simpson’s rule.
The midpoint rule approximates the definite integral using rectangular regions whereas the trapezoidal rule approximates the definite integral using trapezoidal approximations.
Simpson’s rule approximates the definite integral by first approximating the original function using piecewise quadratic functions.
Error in \(M_n≤\dfrac{M(b−a)^3}{24n^2}\), where \(M\) is the maximum value of \(|f''(x)|\) over \([a,b]\).
Error bound for trapezoidal rule
Error in \(T_n≤\dfrac{M(b−a)^3}{12n^2}\), where \(M\) is the maximum value of \(|f''(x)|\) over \([a,b]\).
Error bound for Simpson’s rule
Error in \(S_n≤\dfrac{M(b−a)^5}{180n^4}\), where \(M\) is the maximum value of \(∣f^{(4)}(x)∣\) over \([a,b]\).
Glossary
absolute error
if \(B\) is an estimate of some quantity having an actual value of \(A\), then the absolute error is given by \( |A−B|\)
midpoint rule
a rule that uses a Riemann sum of the form \(\displaystyle M_n=\sum^n_{i=1}f(m_i)Δx\), where \( m_i\) is the midpoint of the \(i^{\text{th}}\) subinterval to approximate \(\displaystyle ∫^b_af(x)\,dx\)
numerical integration
the variety of numerical methods used to estimate the value of a definite integral, including the midpoint rule, trapezoidal rule, and Simpson’s rule
relative error
error as a percentage of the actual value, given by \[\text{relative error}=\left|\frac{A−B}{A}\right|⋅100\%\nonumber \]
Simpson’s rule
a rule that approximates \(\displaystyle ∫^b_af(x)\,dx\) using the area under a piecewise quadratic function.
The approximation \(S_n\) to \(\displaystyle ∫^b_af(x)\,dx\) is given by \[S_n=\frac{Δx}{3}\big(f(x_0)+4\,f(x_1)+2\,f(x_2)+4\,f(x_3)+2\,f(x_4)+⋯+2\,f(x_{n−2})+4\,f(x_{n−1})+f(x_n)\big).\nonumber \]
trapezoidal rule
a rule that approximates \(\displaystyle ∫^b_af(x)\,dx\) using the area of trapezoids.
The approximation \(T_n\) to \(\displaystyle ∫^b_af(x)\,dx\) is given by \[T_n=\frac{Δx}{2}\big(f(x_0)+2\, f(x_1)+2\, f(x_2)+⋯+2\, f(x_{n−1})+f(x_n)\big).\nonumber \]
Contributors and Attributions
Gilbert Strang (MIT) and Edwin “Jed” Herman (Harvey Mudd) with many contributing authors. This content by OpenStax is licensed with a CC-BY-SA-NC 4.0 license. Download for free at http://cnx.org.
Edited by Paul Seeburger (Monroe Community College). Notes added to development of area under a parabola and typos fixed in original text.