Example5.5.2Approximating definite integrals with rectangles
Approximate \(\ds \int_0^1e^{-x^2}\ dx\) using the Left and Right Hand Rules with 5 equally spaced subintervals.
The Fundamental Theorem of Calculus gives a concrete technique for finding the exact value of a definite integral. That technique is based on computing antiderivatives. Despite the power of this theorem, there are still situations where we must approximate the value of the definite integral instead of finding its exact value. The first situation we explore is where we cannot compute the antiderivative of the integrand. The second case is when we actually do not know the integrand, but only its value when evaluated at certain points.
An elementary function is any function that is a combination of polynomials, \(n^{\text{ th } }\) roots, rational, exponential, logarithmic and trigonometric functions. We can compute the derivative of any elementary function, but there are many elementary functions of which we cannot compute an antiderivative. For example, the following functions do not have antiderivatives that we can express with elementary functions: \begin{equation*} e^{-x^2}, \sin(x^3) \text{ and } \frac{\sin(x) }{x}. \end{equation*}
The simplest way to refer to the antiderivatives of \(e^{-x^2}\) is to simply write \(\int e^{-x^2}\ dx\text{.}\)
This section outlines three common methods of approximating the value of definite integrals. We describe each as a systematic method of approximating area under a curve. By approximating this area accurately, we find an accurate approximation of the corresponding definite integral.
We will apply the methods we learn in this section to the following definite integrals: \begin{equation*} \int_0^1 e^{-x^2} \ dx, \int_{-\frac{\pi}{4}}^{\frac{\pi}{2}} \sin(x^3) \ dx, \text{ and } \int_{0.5}^{4\pi} \frac{\sin(x)}{x} \ dx, \end{equation*} as pictured in Figure 5.5.1.
In Section 5.3 we addressed the problem of evaluating definite integrals by approximating the area under the curve using rectangles. We revisit those ideas here before introducing other methods of approximating definite integrals.
We start with a review of notation. Let \(f\) be a continuous function on the interval \([a,b]\text{.}\) We wish to approximate \(\ds \int_a^b f(x)\ dx\text{.}\) We partition \([a,b]\) into \(n\) equally spaced subintervals, each of length \(\ds\dx = \frac{b-a}{n}\text{.}\) The endpoints of these subintervals are labeled as \begin{equation*} x_1=a,\ x_2 = a+\dx,\ x_3 = a+ 2\dx,\ \ldots,\ x_i = a+(i-1)\dx,\ \ldots,\ x_{n+1} = b. \end{equation*}
Key Idea 5.3.14 states that to use the Left Hand Rule we use the summation \(\ds \sum_{i=1}^n f(x_i)\dx\) and to use the Right Hand Rule we use \(\ds \sum_{i=1}^n f(x_{i+1})\dx\text{.}\) We review the use of these rules in the context of examples.
Approximate \(\ds \int_0^1e^{-x^2}\ dx\) using the Left and Right Hand Rules with 5 equally spaced subintervals.
Approximate \(\ds\int_{-\frac{\pi}4}^{\frac{\pi}2} \sin(x^3)\ dx\) using the Left and Right Hand Rules with 10 equally spaced subintervals.
In Example 5.5.2 we approximated the value of \(\ds \int_0^1 e^{-x^2}\ dx\) with 5 rectangles of equal width. Figure 5.5.3 shows the rectangles used in the Left and Right Hand Rules. These graphs clearly show that rectangles do not match the shape of the graph all that well, and that accurate approximations will only come by using lots of rectangles.
Instead of using rectangles to approximate the area, we can instead use trapezoids. In Figure 5.5.7, we show the region under \(f(x) = e^{-x^2}\) on \([0,1]\) approximated with 5 trapezoids of equal width; the top “corners” of each trapezoid lies on the graph of \(f(x)\text{.}\) It is clear from this figure that these trapezoids more accurately approximate the area under \(f\) and hence should give a better approximation of \(\int_0^1 e^{-x^2}\ dx\text{.}\) (In fact, these trapezoids seem to give a great approximation of the area!)
The formula for the area of a trapezoid is given in Figure 5.5.8. We approximate \(\int_0^1 e^{-x^2}\ dx\) with these trapezoids in the following example.
Use 5 trapezoids of equal width to approximate \(\ds \int_0^1e^{-x^2}\ dx\text{.}\)
There are many things to observe in this example. Note how each term in the final summation was multiplied by both 1/2 and by \(\dx = 0.2\text{.}\) We can factor these coefficients out, leaving a more concise summation as: \begin{equation*} \frac12(0.2)\Big[(1+0.9608) + (0.9608+0.8521) + (0.8521+0.6977) + ( 0.6977+ 0.5273) +(0.5273 + 0.3679)\Big]. \end{equation*}
Now notice that all numbers except for the first and the last are added twice. Therefore we can write the summation even more concisely as \begin{equation*} \frac{0.2}{2}\Big[1 + 2(0.9608+0.8521+0.6977+0.5273) + 0.3679\Big]. \end{equation*}
This is the heart of the Trapezoidal Rule, wherein a definite integral \(\ds \int_a^b f(x) \ dx\) is approximated by using trapezoids of equal widths to approximate the corresponding area under \(f\text{.}\) Using \(n\) equally spaced subintervals with endpoints \(x_1\text{,}\) \(x_2\text{,}\) \(\ldots\text{,}\) \(x_{n+1}\text{,}\) we again have \(\ds \dx = \frac{b-a}n\text{.}\) Thus: \begin{align*} \int_a^b f(x)\ dx \amp \approx \sum_{i=1}^n \frac{f(x_i)+f(x_{i+1})}2\dx\\ \amp = \frac{\dx}2 \sum_{i=1}^n \big(f(x_i)+f(x_{i+1})\big)\\ \amp = \frac{\dx}2\Big[f(x_1)+ \left(2\sum_{i=2}^n f(x_i)\right) + f(x_{n+1})\Big]. \end{align*}
Revisit Example 5.5.4 and approximate \(\ds\int_{-\frac{\pi}{4}}^{\frac{\pi}{2}} \sin(x^3)\ dx\) using the Trapezoidal Rule and 10 equally spaced subintervals.
Notice how “quickly” the Trapezoidal Rule can be implemented once the table of values is created. This is true for all the methods explored in this section; the real work is creating a table of \(x_i\) and \(f(x_i)\) values. Once this is completed, approximating the definite integral is not difficult. Again, using technology is wise. Spreadsheets can make quick work of these computations and make using lots of subintervals easy.
Also notice the approximations the Trapezoidal Rule gives. It is the average of the approximations given by the Left and Right Hand Rules! This effectively renders the Left and Right Hand Rules obsolete. They are useful when first learning about definite integrals, but if a real approximation is needed, one is generally better off using the Trapezoidal Rule instead of either the Left or Right Hand Rule. However, there are two other methods that are also generally more accurate than the Left or Right Hand Rule.
Another method that can be more accurate than the Trapezoidal Rule is the Midpoint Rule: \begin{align*} S_M(n)\amp =\sum_{i=1}^n f\left(\frac{x_i+x_{i+1}}{2}\right)\Delta x\\ \amp = \sum_{i=1}^n f\left(\overline{x_i}\right)\Delta x \\ \amp \text{ where } \overline{x_i} \text{ is the midpoint of each subinterval,}\\ \amp \overline{x_i}=a+\dx\left(i-\frac12\right) \end{align*}
Use the Midpoint Rule with \(n=5\) to approximate \(\ds \int_0^1e^{-x^2}\ dx\text{.}\)
Revisit Example 5.5.11 and approximate \(\ds\int_{-\frac{\pi}{4}}^{\frac{\pi}{2}} \sin(x^3)\ dx\) using the Midpoint Rule and 10 equally spaced subintervals.
In many cases, the Midpoint Rule will more accurate than the Trapezoidal Rule. You may wonder though, how can we improve on the Trapezoidal and Midpoint Rules, apart from using more and more subintervals? The answer is clear once we look back and consider what we have really done so far. The Left Hand Rule, Right Hand Rule and Midpoint Rules are not really about using rectangles to approximate area. Instead, they approximates a function \(f\) with constant functions on small subintervals and then computes the definite integral of these constant functions. The Trapezoidal Rule is really approximating a function \(f\) with a linear function on a small subinterval, then computes the definite integral of this linear function. In all of these cases the definite integrals are easy to compute in geometric terms.
So we have a progression: we start by approximating \(f\) with a constant function and then with a linear function. What is next? A quadratic function. By approximating the curve of a function with lots of parabolas, we generally get an even better approximation of the definite integral. We call this process Simpson's Rule, named after Thomas Simpson (1710-1761), even though others had used this rule as much as 100 years prior.
Given one point, we can create a constant function that goes through that point. Given two points, we can create a linear function that goes through those points. Given three points, we can create a quadratic function that goes through those three points (given that no two have the same \(x\)–value).
Consider three points \((x_1,y_1)\text{,}\) \((x_2,y_2)\) and \((x_3,y_3)\) whose \(x\)–values are equally spaced and \(x_1\lt x_2\lt x_3\text{.}\) Let \(f\) be the quadratic function that goes through these three points. It is not hard to show that \begin{equation} \int_{x_1}^{x_3} f(x)\ dx = \frac{x_3-x_1}{6}\big(y_1+4y_2+y_3\big). \label{eq_simpsons}\tag{5.5.1} \end{equation}
While it's not hard to show the results of Equation (5.5.1), it's also not exactly easy. This video might help (Note that they use \(y_0, y_1 \text{ and } y_2\) instead of \(y_1, y_2 \text{ and } y_3\)): https://www.youtube.com/embed/uc4xJsi99bk
Consider Figure 5.5.16. A function \(f\) goes through the 3 points shown and the parabola \(g\) that also goes through those points is graphed with a dashed line. Using our equation from above, we know exactly that \begin{equation*} \int_1^3 g(x) \ dx = \frac{3-1}{6}\big(3+4(1)+2\big)= 3. \end{equation*}
Since \(g\) is a good approximation for \(f\) on \([1,3]\text{,}\) we can state that \begin{equation*} \int_1^3 f(x)\ dx \approx 3. \end{equation*}
Notice how the interval \([1,3]\) was split into two subintervals as we needed 3 points. Because of this, whenever we use Simpson's Rule, we need to break the interval into an even number of subintervals.
In general, to approximate \(\ds \int_a^b f(x)\ dx\) using Simpson's Rule, subdivide \([a,b]\) into \(n\) subintervals, where \(n\) is even and each subinterval has width \(\dx = (b-a)/n\text{.}\) We approximate \(f\) with \(n/2\) parabolic curves, using Equation (5.5.1) to compute the area under these parabolas. Adding up these areas gives the formula: \begin{equation*} \int_a^b f(x) \ dx \approx \frac{\dx}3\Big[f(x_1)+4f(x_2)+2f(x_3)+4f(x_4)+\ldots+2f(x_{n-1})+4f(x_n)+f(x_{n+1})\Big]. \end{equation*}
Note how the coefficients of the terms in the summation have the pattern 1, 4, 2, 4, 2, 4, \(\ldots\text{,}\) 2, 4, 1.
Figure 5.5.17 illustrates how the area calculated by Simpson's Rule approximates \(\int_0^5 f(x)\ dx\) for the function \(f(x)=\sin(\pi x)\text{.}\) In this case, \(8\) subintervals were used, resulting in \(4\) quadratic curves (dashed lines) being fitted to each pair of subintervals. The actual answer (accurate to \(4\) decimal places) is about \(10.6366\text{,}\) while Simpson's rule gives \(10.7294\text{.}\) Of course more subintervals would result in better accuracy. However \(8\) intervals were chosen specifically so that you could see how the parabolas compare to the original function. With larger values of \(n\text{,}\) it becomes difficult to distinguish the function and its quadratic approximations on each subinterval.
Let's demonstrate Simpson's Rule with a concrete example.
Approximate \(\ds\int_0^1 e^{-x^2}\ dx\) using Simpson's Rule and 4 equally spaced subintervals.
Approximate \(\ds\int_{-\frac{\pi}4}^{\frac{\pi}2} \sin(x^3)\ dx\) using Simpson's Rule and 10 equally spaced intervals.
We summarize the key concepts of this section thus far in the following Key Idea.
Let \(f\) be a continuous function on \([a,b]\text{,}\) let \(n\) be a positive integer, and let \(\ds\dx = \frac{b-a}{n}\text{.}\)
Set \(x_1=a\text{,}\) \(x_2 = a+\dx\text{,}\) \(\ldots\text{,}\) \(x_i = a+(i-1)\dx\text{,}\) \(x_{n+1}=b\text{.}\)
Consider \(\ds\int_a^b f(x)\ dx\text{.}\)
Left Hand Rule: \(\ds\int_a^b f(x)\ dx \approx \dx\big[f(x_1) + f(x_2) + \ldots + f(x_n)\big]\text{.}\)
Right Hand Rule: \(\ds\int_a^b f(x)\ dx \approx \dx\big[f(x_2) + f(x_3) + \ldots + f(x_{n+1})\big]\text{.}\)
Trapezoidal Rule: \(\ds\int_a^b f(x)\ dx \approx \frac{\dx}2\big[f(x_1) + 2f(x_2) + 2f(x_3) +\ldots + 2f(x_n)+ f(x_{n+1})\big]\text{.}\)
Midpoint Rule: \(\ds \int_a^b f(x)\ dx \approx \sum_{i=1}^n f\left(\frac{x_i+x_{i+1}}{2}\right)\Delta x\text{.}\)
Simpson's Rule: \(\ds\int_a^b f(x)\ dx \approx \frac{\dx}3\big[f(x_1) + 4f(x_2) + 2f(x_3) +\ldots + 4f(x_n)+ f(x_{n+1})\big] \text{ for } n \text{ even}\text{.}\)
In our examples, we approximated the value of a definite integral using a given method then compared it to the “right” answer. This should have raised several questions in the reader's mind, such as:
How was the “right” answer computed?
If the right answer can be found, what is the point of approximating?
If there is value to approximating, how are we supposed to know if the approximation is any good?
These are good questions, and their answers are educational. In the examples, the right answer was never computed. Rather, an approximation accurate to a certain number of places after the decimal was given. In Example 5.5.2, we do not know the exact answer, but we know it starts with \(0.7468\text{.}\) These more accurate approximations were computed using numerical integration but with more precision (i.e., more subintervals and the help of a computer).
Since the exact answer cannot be found, approximation still has its place. How are we to tell if the approximation is any good?
“Trial and error” provides one way. Using technology, make an approximation with, say, 10, 100, and 200 subintervals. This likely will not take much time at all, and a trend should emerge. If a trend does not emerge, try using yet more subintervals. Keep in mind that trial and error is never foolproof; you might stumble upon a problem in which a trend will not emerge.
A second method is to use Error Analysis. While the details are beyond the scope of this text, there are some formulas that give bounds for how good your approximation will be. For instance, the formula might state that the approximation is within 0.1 of the correct answer. If the approximation is 1.58, then one knows that the correct answer is between 1.48 and 1.68. By using lots of subintervals, one can get an approximation as accurate as one likes. Theorem 5.5.24 states what these bounds are.
Let \(E_T\) and \(E_M\)be the error in approximating \(\ds \int_a^b f(x)\ dx\) using the Trapezoidal and Midpoint Rules respectively. If \(f\) has a continuous 2\(^\text{ nd }\) derivative on \([a,b]\) and \(K\) is any upper bound of \(\abs{\fpp(x)}\) on \([a,b]\text{,}\) then \begin{equation*} \ds E_T \leq \frac{(b-a)^3}{12n^2}K. \end{equation*} and \begin{equation*} \ds E_M \leq \frac{(b-a)^3}{24n^2}K. \end{equation*}
Let \(E_S\) be the error in approximating \(\ds \int_a^b f(x)\ dx\) using Simpson's Rule. If \(f\) has a continuous 4\(^\text{ th }\) derivative on \([a,b]\) and \(K\) is any upper bound of \(\abs{f^{(4)}(x)}\) on \([a,b]\text{,}\) then \begin{equation*} E_S \leq \frac{(b-a)^5}{180n^4}K. \end{equation*}
There are some key things to note about this theorem.
The larger the interval, the larger the error. This should make sense intuitively.
The error shrinks as more subintervals are used (i.e., as \(n\) gets larger).
The maximum error in the Midpoint Rule is half of the maximum error in the Trapezoidal Rule. (Usually the errors in these two rules have opposite signs as well, that is one will be an under approximation and the other will be an over approximation).
The error in Simpson's Rule has a term relating to the 4\(^{\text{ th } }\) derivative of \(f\text{.}\) Consider a cubic polynomial: it's \(4^{\text{ th } }\) derivative is 0. Therefore, the error in approximating the definite integral of a cubic polynomial with Simpson's Rule is 0 — Simpson's Rule computes the exact answer!
We revisit Examples 5.5.9 and 5.5.18 and compute the error bounds using Theorem 5.5.24 in the following example.
Find the error bounds when approximating \(\ds \int_0^1 e^{-x^2}\ dx\) using the Trapezoidal and Midpoint Rules and 5 subintervals, and using Simpson's Rule with 4 subintervals.
At the beginning of this section we mentioned two main situations where numerical integration was desirable. We have considered the case where an antiderivative of the integrand cannot be computed. We now investigate the situation where the integrand is not known. This is, in fact, the most widely used application of Numerical Integration methods. “Most of the time” we observe behavior but do not know “the” function that describes it. We instead collect data about the behavior and make approximations based off of this data. We demonstrate this in an example.
One of the authors drove his daughter home from school while she recorded their speed every 30 seconds. The data is given in Figure 5.5.29. Approximate the distance they traveled.
Terms and Concepts
In the following exercises, a definite integral is given.