next up previous
Next: About this document ... Up: lab_template Previous: lab_template

Subsections


Numerical Integration

Purpose

The purpose of this lab is to give you some experience with using the trapezoidal rule and Simpson's rule to approximate integrals.

Background

In class we have talked about the trapezoidal rule and Simpson's rule for approximating the definite integral

\begin{displaymath}\int_{a}^{b} f(x) \, dx \end{displaymath}

Both methods start by dividing the interval $[a,b]$ into $n$ subintervals of equal length by choosing a partition

\begin{displaymath}a = x_0 < x_1 < x_2 < \ldots < x_n = b \end{displaymath}

satisfying

\begin{displaymath}x_i = a + i h, \mbox{ for $i=0, \ldots, n$} \end{displaymath}

where

\begin{displaymath}h = \frac{b-a}{n} \end{displaymath}

is the length of each subinterval. For the trapezoidal rule, the integral over each subinterval is approximated by the area of a trapezoid. This gives the following approximation to the integral

\begin{displaymath}T_n = \frac{h}{2} \left( f(x_0) + 2 f(x_1) + 2 f(x_2) + \ldots + 2
f(x_{n-1}) + f(x_n) \right) \end{displaymath}

For Simpson's rule, the function is approximated by a parabola over pairs of subintervals. When the areas under the parabolas are computed and summed up, the result is the following approximation.

\begin{displaymath}S_n = \frac{h}{3} \left( f(x_0) + 4 f(x_1) + 2 f(x_2) + \ldots + 4
f(x_{n-1}) + f(x_n) \right) \end{displaymath}

Maple commands

The commands for the trapezoidal rule and Simpson's rule are in the student package.
>with(student);
The following example will use the function
>f:=x->x^2*exp(x);
This computes the integral of the function from 0 to 2.
>int(f(x),x=0..2);
Using the evalf command provides a decimal approximation.
>evalf(int(f(x),x=0..2));
The command for using the trapezoidal rule is trapezoid. The syntax is very similar to that of the int command. the last argument specifies the number of subintervals to use. In the command below, the number of subintervals is set to 10, but you should experiment with increasing or decreasing this number. Note that Maple writes out the sum and doesn't evaluate it to a number.
>trapezoid(f(x),x=0..2,10);
Putting an evalf command on the outside computes the trapezoidal approximation.
>evalf(trapezoid(f(x),x=0..2,10));
The command for Simpson's rule is very similar.
>simpson(f(x),x=0..2,10);
>evalf(simpson(f(x(,x=0..2,10));

Exercises

  1. Consider the function

    \begin{displaymath}
g(x) = \frac{x^3+3x^2-4x+5}{x^2+7x+1}
\end{displaymath}

    Plot the function between 0 and 11. Using the trapezoid rule, find the minimum number of subintervals necessary to approximate the following integrals to within 0.001. (Remember to find error subtract the approximation from the integral).

    \begin{displaymath}
\int_{1}^{10} g(x)dx
\end{displaymath}


    \begin{displaymath}
\int_{1}^{11} g(x)dx
\end{displaymath}


    \begin{displaymath}
\int_{0}^{10} g(x)dx
\end{displaymath}

    Looking at the plot can you say why the final approxiamation takes so many more subintervals?
  2. Consider the following function

    \begin{displaymath}
g(x) = \frac{x^3-7x+11}{x^2+5x-5}
\end{displaymath}

    Plot the function between 1 and 6 (note you may want to include a y-range of -5 to 1). Using Simpson's rule, find the minimum number of subintervals necessary to approximate the following integrals to within 0.001.

    \begin{displaymath}
\int_{2}^{5} g(x)dx
\end{displaymath}


    \begin{displaymath}
\int_{3}^{6} g(x)dx
\end{displaymath}


    \begin{displaymath}
\int_{1}^{4} g(x)dx
\end{displaymath}

    Looking at the plot can you say why the final approximation takes so many more subintervals?
  3. Consider the following function

    1. \begin{displaymath}
f(x) = (4-x^2)^{\frac{5}{2}}
\end{displaymath}

      Plot the function between 0 and 2. Using the trapezoid rule find the minimum number of subintervals necessary to approximate the area to within 0.001. Then do the same with Simpson's rule. Which rule is more accurate? (i.e. which needs less subintervals to approximate the area)

    2. \begin{displaymath}
f(x) = \sin(x)
\end{displaymath}

      Plot the function between 0 and $\pi$. Using the trapezoid rule find the minimum number of subintervals necessary to approximate the area to within 0.001. Then do the same with Simpson's rule. Which rule is more accurate? (i.e. which needs less subintervals to approximate the area)

next up previous
Next: About this document ... Up: lab_template Previous: lab_template
Jane E Bouchard
2004-09-24