Skip to main content
\(\require{cancel}\newcommand{\highlight}[1]{{\color{blue}{#1}}} \newcommand{\apex}{A\kern -1pt \lower -2pt\mbox{P}\kern -4pt \lower .7ex\mbox{E}\kern -1pt X} \newcommand{\colorlinecolor}{blue!95!black!30} \newcommand{\bwlinecolor}{black!30} \newcommand{\thelinecolor}{\colorlinecolor} \newcommand{\colornamesuffix}{} \newcommand{\linestyle}{[thick, \thelinecolor]} \newcommand{\bmx}[1]{\left[\hskip -3pt\begin{array}{#1} } \newcommand{\emx}{\end{array}\hskip -3pt\right]} \newcommand{\ds}{\displaystyle} \newcommand{\fp}{f'} \newcommand{\fpp}{f''} \newcommand{\lz}[2]{\frac{d#1}{d#2}} \newcommand{\lzn}[3]{\frac{d^{#1}#2}{d#3^{#1}}} \newcommand{\lzo}[1]{\frac{d}{d#1}} \newcommand{\lzoo}[2]{{\frac{d}{d#1}}{\left(#2\right)}} \newcommand{\lzon}[2]{\frac{d^{#1}}{d#2^{#1}}} \newcommand{\lzoa}[3]{\left.{\frac{d#1}{d#2}}\right|_{#3}} \newcommand{\plz}[2]{\frac{\partial#1}{\partial#2}} \newcommand{\plzoa}[3]{\left.{\frac{\partial#1}{\partial#2}}\right|_{#3}} \newcommand{\inflim}[1][n]{\lim\limits_{#1 \to \infty}} \newcommand{\infser}[1][1]{\sum_{n=#1}^\infty} \newcommand{\Fp}{F\primeskip'} \newcommand{\Fpp}{F\primeskip''} \newcommand{\yp}{y\primeskip'} \newcommand{\gp}{g\primeskip'} \newcommand{\dx}{\Delta x} \newcommand{\dy}{\Delta y} \newcommand{\ddz}{\Delta z} \newcommand{\thet}{\theta} \newcommand{\norm}[1]{\left\lVert#1\right\rVert} \newcommand{\vnorm}[1]{\left\lVert\vec #1\right\rVert} \newcommand{\snorm}[1]{\left|\left|\ #1\ \right|\right|} \newcommand{\la}{\left\langle} \newcommand{\ra}{\right\rangle} \newcommand{\dotp}[2]{\vec #1 \cdot \vec #2} \newcommand{\proj}[2]{\text{proj}_{\,\vec #2}{\,\vec #1}} \newcommand{\crossp}[2]{\vec #1 \times \vec #2} \newcommand{\veci}{\vec i} \newcommand{\vecj}{\vec j} \newcommand{\veck}{\vec k} \newcommand{\vecu}{\vec u} \newcommand{\vecv}{\vec v} \newcommand{\vecw}{\vec w} \newcommand{\vecx}{\vec x} \newcommand{\vecy}{\vec y} \newcommand{\vrp}{\vec r\, '} \newcommand{\vsp}{\vec s\, '} \newcommand{\vrt}{\vec r(t)} \newcommand{\vst}{\vec s(t)} \newcommand{\vvt}{\vec v(t)} \newcommand{\vat}{\vec a(t)} \newcommand{\px}{\partial x} \newcommand{\py}{\partial y} \newcommand{\pz}{\partial z} \newcommand{\pf}{\partial f} \newcommand{\mathN}{\mathbb{N}} \newcommand{\zerooverzero}{\ds \raisebox{8pt}{\text{``\ }}\frac{0}{0}\raisebox{8pt}{\textit{ ''}}} \newcommand{\deriv}[2]{\myds\frac{d}{dx}\left(#1\right)=#2} \newcommand{\myint}[2]{\myds\int #1\ dx= {\ds #2}} \DeclareMathOperator{\sech}{sech} \DeclareMathOperator{\csch}{csch} \newcommand{\primeskip}{\hskip.75pt} \newcommand{\plotlinecolor}{blue} \newcommand{\colorone}{blue} \newcommand{\colortwo}{red} \newcommand{\coloronefill}{blue!15!white} \newcommand{\colortwofill}{red!15!white} \newcommand{\abs}[1]{\left\lvert #1\right\rvert} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section5.5Numerical Integration

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.

<<SVG image is unavailable, or your browser cannot render it>>

<<SVG image is unavailable, or your browser cannot render it>>

<<SVG image is unavailable, or your browser cannot render it>>

Figure5.5.1Graphically representing three definite integrals that cannot be evaluated using antiderivatives.

Subsection5.5.1The Left and Right Hand Rule Methods

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.

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.

Example5.5.4Approximating definite integrals with rectangles

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.


Subsection5.5.2The Trapezoidal Rule

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!)

<<SVG image is unavailable, or your browser cannot render it>>

Figure5.5.7Approximating \(\int_0^1 e^{-x^2}\ dx\) using 5 trapezoids of equal widths.

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.

<<SVG image is unavailable, or your browser cannot render it>>

Figure5.5.8The area of a trapezoid.

Example5.5.9Approximating definite integrals using trapezoids

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*}

Example5.5.11Using the Trapezoidal Rule

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.

Subsection5.5.2.1The Midpoint 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*}

Example5.5.12Using the Midpoint Rule

Use the Midpoint Rule with \(n=5\) to approximate \(\ds \int_0^1e^{-x^2}\ dx\text{.}\)

Example5.5.14Using the Midpoint Rule

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.

Subsection5.5.3Simpson's Rule

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}

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*}

<<SVG image is unavailable, or your browser cannot render it>>

Figure5.5.16A graph of a function \(f\) and a parabola that approximates it well on \([1,3]\text{.}\)

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.

<<SVG image is unavailable, or your browser cannot render it>>

Figure5.5.17An illustration of Simpson's rule on \(f(x)=\sin(\pi x)+2\) over \([0,5]\) using \(8\) subintervals, resulting in \(4\) quadratic approximations.

Let's demonstrate Simpson's Rule with a concrete example.

Example5.5.18Using Simpson's Rule

Approximate \(\ds\int_0^1 e^{-x^2}\ dx\) using Simpson's Rule and 4 equally spaced subintervals.

Example5.5.20Using Simpson's Rule

Approximate \(\ds\int_{-\frac{\pi}4}^{\frac{\pi}2} \sin(x^3)\ dx\) using Simpson's Rule and 10 equally spaced intervals.


Subsection5.5.4Summary and Error Analysis

We summarize the key concepts of this section thus far in the following Key Idea.

Key Idea5.5.23Numerical Integration

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:

  1. How was the “right” answer computed?

  2. If the right answer can be found, what is the point of approximating?

  3. 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.

There are some key things to note about this theorem.

  1. The larger the interval, the larger the error. This should make sense intuitively.

  2. The error shrinks as more subintervals are used (i.e., as \(n\) gets larger).

  3. 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).

  4. 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.

Example5.5.25Computing error bounds

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.

Example5.5.28Approximating distance traveled

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.

We have seen in this section several techniques to find approximations to definite integrals.