Skip to main content

Notes for Math 220, Calculus 2

Section 3.6 Numerical Integration

References.

Definite Integrals cannot always be evaluated using anti-derivatives and the Fundamental Theorem of Calculus.
One reason is that not all elementary functions have anti-derivatives that can be expressed in terms of elementary functions: two important examples are \(\ds \int e^{-x^2} dx\) and \(\ds \int \frac{\sin x}{x} dx\text{.}\)
Another situation is when a function \(f(x)\) is only known through values at a finite collection of \(x\) values, for example when working with data from experiments or computer simulation.
One solution is already familiar: get an approximate numerical value using a Riemann Sum as in Section 1.2, and often the best simple choice or Riemann sum is the Midpoint Rule seen there. It is simplest to use \(N\) intervals of equal width \(h = \Delta x = \ds \frac{b-a}{N}\text{,}\) which gives the \(N\)-point Midpoint Rule Approximation
\begin{equation} \int_a^b f(x) \, dx \approx M_N = h \sum_{n=1}^N f \left( \bar{x}_n \right) = h \sum_{n=1}^N f(a+(n-1/2)h),\tag{3.6.1} \end{equation}
where
  • \(x_n = a+n h\) (\(x_{0}=a, x_1=a+h\text{,}\) etc.) and
  • \(\displaystyle \ds \bar{x}_i = \frac{x_{i-1}+x_i}{2} = a+(i-1/2) h\)
so \(\ds \bar{x}_1 = a + \frac{h}{2}, \bar{x}_2 = a+\frac{3 h}{2}\text{,}\) etc.

The Mid-point Rule on a TI Calculator.

This sum can be evaluated on a TI-84 or similar calculator with
sum(seq(f(x),x,a+h/2,b-h/2,h))*h
or preferably (because it avoids occasional wrong answers with the above!)
sum(seq(f(a+(n-1/2)*h),n,1,N))*h
When using these formulas, \(f\) is replaced by a formula, and \(a\text{,}\) \(b\text{,}\) \(h\) and \(N\) by actual numbers, but \(x\) or \(n\) are still letter names.

The Trapezoid Rule.

Sometimes the Midpoint Rule cannot be used as one does not have values of \(f\) at the needed points; it is common instead with \(\int_a^b f(x)\ dx\) to have measurements at equally spaced values starting at \(a\) and ending at \(b\text{:}\) in other words, as the points \(x_n\) above that divide the intervals on which the rectangles in the Riemann Sum are based.
Then a natural (and usually accurate) approach is to approximate the function on each interval \([x_{n-1},x_n]\) by the average of its values at the two endpoints: \(\ds \frac{f(x_{n-1})+f(x_n)}{2}\text{.}\)
The area of each rectangle is then \(\ds \frac{f(x_{n-1})+f(x_n)}{2} h\) which is also the area of a trapezoid on the interval \([x_{n-1},x_n]\) of height \(f(x_{n-1})\) at \(x=x_{n-1}\) and height \(f(x_n)\) at \(x=x_{n}\text{;}\) in other words, touching the curve \(y=f(x)\) at each \(x_n\text{.}\)
With \(N\) intervals of equal width again, we get the \(N\)-point Trapezoid Rule Approximation
\begin{equation*} \begin{split} \int_a^b \hspace{-1ex} f(x) dx \approx T_N \amp= h \sum_{n=1}^N \frac{f(x_{n-1})+f(x_n)}{2} \\ \amp=\frac{h}{2} \left [ f(x_0) + 2 \sum_{n=1}^{N-1} f(x_n) + f(x_N) \right] \\ \amp= h \left[ \frac{f(a)}{2} + f(x_1) + \cdots + f(x_{N-1}) + \frac{f(b)}{2} \right] \end{split} \end{equation*}
This can be evaluated on a TI-84 or similar calculator with
( f(a) + f(b) + 2*sum(seq(f(x),x,a+h,b-h,h)) )*h/2
or by the safer method as above,
( f(a) + f(b) + 2*sum(seq(f(a+n*h),n,1,N-1)) )*h/2

Errors and Error Bounds.

An approximation is useless without at least some idea of how accurate it is, like the number of significant digits. Thus we want to know something about the error in the above methods, defined as the difference between the exact result and the approximation.
\begin{equation*} E_M = \int_a^b f(x) \, dx - M_N \qquad E_T = \int_a^b f(x) \, dx - T_N \end{equation*}
For a variety of examples where the exact value of the integral is known (not the case where we would usually be using an approximation!) two patterns are seen, at least when \(N\) is reasonably large:
  • Increasing the number \(N\) of intervals 10-fold reduces the error about 100-fold.
  • The error for the Midpoint Rule is about half as large as that for the Trapezoid Rule, and of opposite sign.
These patterns are partially explained by the following theoretical results (not proved here, but in a Numerical Methods course like Math 245.) Suppose that \(|f''(x)| \leq K\) for \(a \leq x \leq b\text{.}\) Then
\begin{equation*} |E_M| \leq \frac{K(b-a)^3}{24 N^2}, \qquad |E_T| \leq \frac{K(b-a)^3}{12 N^2}. \end{equation*}
If we have some idea of a value \(K\text{,}\) we can use these results to put an upper limit on the error in a Midpoint Rule or Trapezoid Rule approximation. Better yet, we can use them to guarantee a needed degree of accuracy by choosing a sufficiently large number of intervals \(N\text{.}\)
  1. If we approximate \(\ds \int_2^4 \frac{dx}{x}\) using the Midpoint Rule with \(100\) intervals, what is the worst that the error can be?
  2. If we wish the value to be accurate to eight decimal places, how many intervals are needed?

Simpson's Rule.

The last result is a bit disappointing when you need high accuracy: four decimal places can be got rather cheaply with the Midpoint Rule (about \(N=100\)), but the full fifteen decimal places that a typical computer's hardware is capable of takes a lot of work (about \(N=10^7\text{.}\))
In situations calling for high accuracy, a far more efficient method is Simpson's Rule. For this, the idea is to replace the rectangle and trapezoid approximations of the curve by a collection of quadratics, and then compute the area under each of these, and sum those areas.
Fortunately this leads to a fairly simple formula in terms of the same function values \(f(x_i)\) used in the Trapezoid Rule, so we do not need to actually find the quadratics or integrate them; The only catch is that there must be an even number \(N\) of intervals, as each quadratic spans two intervals.
With \(n\) intervals of equal width, Simpson's Rule is
\begin{equation*} \int_a^b f(x) \, dx \approx S_N = \frac{\Delta x}{3} \left[ f(x_0) + 4f(x_1) + 2f(x_2) + \cdots 2 f(x_{N-2})+ 4f(x_{N-1}) + f(x_N) \right] \end{equation*}
Perhaps the simplest way to evaluate \(S_{2N}\) is to first evaluate \(M_N\) and \(T_N\) as above and then use the surprising formula
\begin{equation*} S_{2N} = \frac{2M_N + T_N}{3} \end{equation*}

Error Bound For Simpson's Rule.

Suppose that \(|f^{(4)}(x)| \leq K\) for \(a \leq x \leq b\text{.}\) Then the error \(E_S = \int_a^b f(x)\ dx - S_N\) satisfies
\begin{equation*} |E_S| \leq \frac{K(b-a)^5}{180 N^4}. \end{equation*}
The good news here is the trend that a 10-fold increase in the number of intervals reduces error by a factor of about 10,000, instead of 100 as for the previous methods. Thus, for large enough \(N\text{,}\) Simpson's Rule is usually far more accurate than the previous two methods.

Section Study Guide.

  • the formulas in Theorems 3, 4 and 6,
  • Examples 39–42, 45 and 46,
  • Checkpoints 22–24 and 26,
  • and one or several exercises from each of the range 299–306.
openstax.org/books/calculus-volume-2/pages/3-6-numerical-integration
openstax.org/books/calculus-volume-2/pages/3-6-numerical-integration