 ## Numerical Integration miscellaneous on-line topics for Calculus Applied to the Real World

Index of On-Line Topics
Exercises for This Topic
Everything for Calculus
Utility: Numerical Integration Utility
TI-83: Graphing Calculator Programs
Español The Fundamental Theorem of Calculus gives us an exact formula for computing $\int_{a}^{b}\ f(x)\ dx,$ provided we can find an antiderivative for $f.$ This method of evaluating definite integrals is called the analytic method. However, there are times when this is difficult or impossible. In these cases, it is usually good enough to find an approximate, or numerical solution, and there are some very straighforward ways to do this.

The simplest numerical approximations to the integral are the left and right Riemann sums. More efficient approximations (below) are the trapezoidal and Simpson approximations.

Left and Right Riemann Sums
All numerical approximations of the integral $\int_{a}^{b}\ f(x)\ dx$ we will consider start with a partition of the interval $[a\ .\ b]$ into $n$ equal parts:

$Δx = (b-a)/n,$
$x_0 = a,$
$x_1 = a + Δx,$
$x_2 = a + 2Δx,$
$. . .$
$x_n = a + nΔx = b$ In Calculus Applied to the Real World, we defined the left Riemann sum as follows:

 Left Riemann Sum $= \sum_{n = 0}^{n-1} f(x_k)Δx$ $= f(x_0)Δx + f(x_1)Δx + . . . + f(x_{n-1})Δx$ $= [f(x_0) + f(x_1) + . . . + f(x_{n-1})]Δx$

The left Riemann sum gives the area shown below. Notice that the left side of each rectangle matches the height of the graph -- hence the name "left sum." The right Riemann sum is defined similarly:

 Right Riemann Sum $= \sum_{n = 1}^{n} f(x_k)Δx$ $= f(x_1)Δx + f(x_2)Δx + ... + f(x_n)Δx$ $= [f(x_1) + f(x_2) + ... + f(x_n)]Δx$

The right Riemann sum gives the area shown below.  Example 1 Computing Left- and Right Riemann Sums

Let $f(x) = 1 -x^2.$ Compute the left and right Riemann sum approximations of $\int_{0}^{1}\ f(x)\ dx$ with $n = 8.$

Solution

The left Riemann sum for the function is illustrated here: To compute it, we use the following setup:

 $Δx = (b-a)/n =$  We now calcuate the partition values in the following table.

 $x_0 = a$ $x_1 = a+Δx$ $x_2 = a+2Δx$ $x_3 = a+3Δx$ $x_4 = a+4Δx$ $x_5 = a+5Δx$ $x_6 = a+6Δx$ $x_7 = a+7Δx$ $x_8 = b =$         $x$ $x_0$ $x_1$ $x_2$ $x_3$ $x_4$ $x_5$ $x_6$ $x_7$ $x_8$ Value $\color{blue}{f(x) = 1-x^2}$

We can now compute the left- and right Riemann sums as follows:

 Left Riemann Sum $=$ $[f(x_0) + f(x_1) + ... + f(x_7)]Δx$ $=$ Sum of the first $8$ terms in the bottom row above $\times Δx$ $=$ $5.8125 \times 0.125 = 0.7265625.$ Right Riemann Sum $=$ $[f(x_1) + f(x_2) + ... + f(x_8)]Δx$ $=$ Sum of the last $8$ terms in the bottom row above $\times Δx$ $=$ $4.8125 \times 0.125 = 0.6015625.$

Trapezoid Sums

A trapezoid is a four-sided region with two opposite sides parallel. In the figure below, it is the two vertical sides that are parallel. The area of a trapezoid is the average length of the parallel sides, times the distance between them.

Given a partition of $[a, b]$ as above, we can define the associated trapezoid sum to correspond to the area shown below. The areas of the individual trapezoids (from left to right) are as follows.

 Leftmost trapezoid: $\frac{1}{2} (f(x_0) + f(x_1))Δx$ Average height $\times$ width Next trapezoid: $\frac{1}{2} (f(x_1) + f(x_2))Δx$ . . . Last trapezoid: $\ \ \frac{1}{2} (f(x_{n-1}) + f(x_n))Δx$

Adding these together, gives the trapezoid sum:

 Trapezoid Sum $= \frac{1}{2} (f(x_0) + f(x_1))Δx + \frac{1}{2} (f(x_1) + f(x_2))Δx + . . . + \frac{1}{2} (f(x_{nµ1}) + f(x_n))Δx$

Simplifying gives

Trapezoid Sum

The trapezoid approximation of $\int_{a}^{b}\ f(x)\ dx$ associated with the partition $a = x_0 < x_1 < ... < x_n = b$ is given by

 Trapezoid Sum $= \frac{b-a}{2n} [f(x_0) + 2f(x_1) + ... + 2f(x^{n-1}) + f(x_n)]$

If you look carefully at the above formula, you will see that the trapezoid sum is just the average of the left- and right Riemann sums. Example 2 Computing a Trapezoid Sum

Compute the trapezoid sum approximation of $\int_{0}^{1}\ (1-x^2)\ dx$ with $n = 8.$

Solution

In view of the comment immediately above, we have already computed it, in effect:

 Trapezoid Sum $=$ Average of Left- and Right Riemann Sums $=\frac{0.7265625 + 0.6015625}{2} = 0.6640625$

Q How about computing the trapezoid sum without first computing the left- and right Riemann sums?
A Here is an example. Example 3 Computing a Trapezoid Sum

Compute the trapezoid sum approximation of $\int_{1}^{2}\ e^{-x^2} dx$ with $n = 4.$

Solution

Since $n = 4,$ the width of the subintervals is $(b - a)/4 = (2 - 1)/4 = 0.25.$

Now proceed as follows using the table below:

1. Enter the correct partition values $(x_0,$ $x_1, ... ).$
2. Entering the correct formula for $f(x)$ (graphing calculator format). (This is similar to what you would do using on a graphing calculator or spreadsheet. Here are some examples of correctly formatted expressions.)
3. Press "Compute" to obtain the values of $f(x_i)$ and to complete the table.

 $\color{blue}{x}$ $x_0 = a$ $x_1$ $x_2$ $x_3$ $x_4$     Enter Formula Here:$f(x) =$ $\times 2$ $\times 2$ $\times 2$  Total:

We can now compute the Trapezoid sum as follows:

 Trapezoid Sum $=$ $\frac{1}{2} [f(x_0) + 2f(x_1) + ... + 2f(x_{n-1}) + f(x_n)]Δx$ $=$ $\frac{1}{2} \times$   Total on last row above $\times 0.25$ If your total is not $1.10975...,$ check the values of $x$ you used. $\approx$ $0.5 \times 1.109758 \times 0.25 = 0.13871975$

Before we go on...

To automate the entire calculation, or to use much larger values of $n,$ try our Numerical Integration Utility.

Simpson's Rule

Simpson's rule gives us another approximation of the integral. Again, we start by partitioning $[a, b]$ into intervals all of the same width, but this time we must use an even number of intervals, so n will be even.

Simpson's Rule

If $n$ is even, and, as before, $x_k = a + kΔx = a + k (b - a)/n,$ then

 $\int_{a}^{b}\ f(x)\ dx \approx \frac{b - a}{3n} [f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + ... + 2f(x_{n-2}) + 4f(x_{n-1}) + f(x_n)]$

Q Why?
A As with the trapezoid rule, we want to approximate the areas in each strip by something more complicated than a rectangle. This time we take the strips in pairs (which is why we need an even number of them) and draw a parabola through the three points $(x_{k-1}, f(x_{k-1})), (x_k, f(x_k)),$ and $(x_{k+1}, f(x_{k+1})),$ as shown in the figure.

It is then not too difficult to find the equation of this parabola (it has the form $y = Ax^2 + Bx + C$), and from that to find the area underneath by integrating. The remarkably simple answer is

 Area under parabola $= \frac{b - a}{3n} [f(x_{k-1}) + 4f(x_k) + f(x_{k+1})].$

When we add the area under the parabola over the first two strips to the area under the parabola over the 3rd and 4th strips, and so on, we get Simpson's rule. Example 4 Simpson's Rule

Use 4 intervals in Simpson's rule to approximate $\int_{0}^{1} x^2 dx.$

Solution

Since $n = 4,$ we have $(b - a)/n = 1/4,$ and Simpson's rule tells us that

 $\int_{0}^{1}\ f(x)\ dx \approx \frac{b - a}{3n} [f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + f(x_4)]$ $= \frac{1}{12} [0 + 4(0.0625) + 2(0.25) + 4(0.5625) + 1] = \frac{1}{3}.$

Before we go on...

This is the exact answer. What is going on? Remember that Simpson's rule is based on approximating the graph by quadratic functions. If the function is already quadratic, as it is here, the approximation is exact. Example 5 Simpson's Rule

Use 6 intervals in Simpson's rule to approximate $\int_{0}^{6}\ e^{-x^2} dx.$ (We already approximated a similar integral using the trapezoid rule here.)

Solution

The following table summarizes the main part of the computation. (Figures are rounded to 6 decimal places.)

 $x$ $0$ $1$ $2$ $3$ $4$ $5$ $6$ $f(x) = e^{-x^2}$ $1$ $0.367879$ $0.018316$ $0.000123$ $0$ $0$ $0$ $\times 4$ $\times 2$ $\times 4$ $\times 2$ $\times 4$  $1$ $1.471518$ $0.036631$ $0.000494$ $0$ $0$ $0$

Thus, Simpson approximation $= (6 - 0)/18 \times$ Sum of bottom row $\approx 0.83621$

Accuracy of Trapezoid and Simpson Approximations

If you look at the diagrams above, you will see that the approximations we have been using become more accurate as the number of subdivisions $n$ gets larger. This raises an interesting and important question: How large does $n$ have to be to get "close enough" to the exact integral? To answer these questions, we need to know something about the error in these two rules, that is, how far they are from the exact integral.

Error = |Approximation - Exact integral|

Q Doesn't this raise a "Catch 22" situation? In order to know the error, we need to know the exact answer. But if we knew the exact answer, then we would hardly need to find a numerical approximation in the first place!
A We remedy this dilemma as follows: since we can't always calculate exactly what the error is, we look instead for a bound on the error. For instance, instead of trying to say "the error is exactly $0.001,$" we say instead, "the error is no larger than $0.001.$"

The following formulas give bounds on the errors for the rules we have been using.

The Errors in the Trapezoid Rule and Simpson's Rule

If $f"(x)$ is continuous in $[a,\ b],$ then the error in the trapezoid rule is no larger than

 $\frac{(b - a)^3}{12n^2} \|f"(M)\|,$

where $\|f"(M)\|$ is the largest value of $\|f" (x)\|$ in $[a,\ b].$

If $f^{(4)}(x)$ is continuous in $[a,\ b],$ then the error in Simpson's rule is no larger than

 $\frac{(b - a)^5}{180n^4} \|f^{(4)}(M)\|,$

where $\|f^{(4)}(M)\|$ is the largest value of $\|f^{(4)}(x)\|$ in $[a,\ b].$ Example 6 Estimating the Error in the Trapezoid Sum

How accurate is the calculation in Example 3?

Solution

In that example we used 4 intervals in the trapezoid rule to estimate $\int_{1}^{2}\ e^{-x^2} dx.$

In order to estimate the error, we need to find the largest value of $\|f" (x)\|$ in the interval $[1,\ 2].$ for $f(x) = e^{-x^2}.$

Calculating,

$f'(x) = -2xe^{-x^2},$ y
$f"(x) = 2(2x^2-1)e^{-x^2}.$

Since we want to find the extreme values of $f",$ we calculate its derivative,

$f^{(3)}(x) = 4x(3-2x^2) e^{-x^2}.$

Now $f^{(3)} (x) = 0$ only when $x = 0$ or $3-2x^2 = 0,$ so $x = 0$ or $±(3/2)^{1/2} \approx ±1.225.$ Checking values in the interval $[1,\ 2],$ we get the following (where we have rounded up the values of $f"(x)$ rather than simply rounding to two decimal places (why?).

 $x$ $1$ $1.225$ $2$ $\color{blue}{f" (x)}$ $0.74$ $0.90$ $0.26$

The largest value of $\|f"(x)\|$ is therefore $0.90.$ This tells us that the error is no larger than

$\frac{(2 - 1)^3}{12\times 4^2}\times 0.90 \approx 0.0047$ Example 7 Estimating the Error in the Simpson Sum

How large would n have to be to obtain a Simpson rule approximation of $\int_{-1}^{2}\ (x^3 + e^{-x})\ dx$ accurate to five decimal places?

Solution

"Accurate to $5$ decimal places" means an error of less than $0.000 005.$ In this problem, we don't know the value of $n,$ but we do know an upper bound for the error.

Our formula for the error in Simpson's rule says that

$\|Error\| ≤ \frac{(b - a)^5}{180n^4} \|f^{(4)}(M)\|$

A quick calculation shows that the $4th$ derivative of $f$ is

$f^{(4)}(x) = e^{-x},$

so that it is positive, and its larest value in the interval $[-1,\ 2]$ occurs when $x = -1:$

$\|f^{(4)}(M)\| = e \approx 3$

As before, we have overestimated rather than underestimated the quantity e (never underestimate an error!). This gives

 $\|Error\| ≤ \frac{(b - a)^5}{180n^4} \|f^{(4)}(M)|$ $< \frac{3^5}{180n^4}3 = \frac{81}{20n^4}.$

We would like this quantity to be at most $0.000 005$ for $a$ suitable $n.$ To find $n,$ we can set $81/20n^4$ equal to $0.000 005$ and solve for $n,$ and then round up to the nearest integer. We obtain:.

 $n$ must be at least .   Last Updated: September, 1999