Ryerson Crest Ryerson Header

MTH 207 Lab Lesson 21

Numerical Integration


Up to Main Lab Page Next Lesson - Differential Equations Previous Lesson - Integration

[See Stewart Section 7.8]

The idea of numerical integration is to find the value of the integral of f (x) from a to b by estimating the area under the curve from a to b, counting areas below the axis as negative.

The Midpoint Rule

Consider the definition of the integral as a Riemann sum.
P is a partition of [a, b] into n subintervals, [x0, x1 ], [x1, x2], . . . , [xn - 1, xn] each of width x.
xi* is in the interval [xi - 1, xi] for each i.
As || P || -> 0, n -> infinity and x -> 0.

In this definition the integral is taken to be the limit of a sum of areas. If we instead take a finite number, n, of subintervals we will get an approximation to the integral. We choose xi* to be the midpoint of each subinterval. i.e. xi* = ½( xi-1 + xi ), for each i from 1 to n.

The area of each rectangle is f ( xi* ) x.
Since each rectangle has the same width: x = (b - a)/n
xi* = ½( xi-1 + xi ).

So f (x) dx ~ (b - a)/n ( f ( x1* ) + f ( x2* ) + . . . + f ( xn* ) ), where xi* = ½( xi-1 + xi ).

Example

For our first example we will try to calculate the integral of f (x) = exp(x)*sin(x) from 0 to Pi. This is an integral which Maple can do, so we can compare our answer to that reported by Maple. We start by creating a proceedure to evaluate the midpoint rule.
> midp := proc(f, a::realcons, b::realcons, n::integer)
> local dx, i, xi, sm;
> dx := (b - a)/n;
> sm := 0;
> for i from evalf(a) by evalf(dx) to evalf(b) do
> xi := evalf(i + dx/2);
> sm := evalf(sm + f(xi));
> od:
> RETURN(evalf(dx*sm));
> end;

Now we can use the procedure to find the value for various values of n, and compare them to the value reported by Maple.
> f:= x -> exp(x)*sin(x);
> evalf(int(f(x), x = 0..Pi));
> midp(f, 0, Pi, 10);
> midp(f, 0, Pi, 20);
> midp(f, 0, Pi, 50);
> midp(f, 0, Pi, 100);

  1. Use the midpoint method to estimate the following integrals, using n = 5, 10 and 50. Where possible check your answers with the Maple int function.
    1. exp(1/x), from 1 to 2.
    2. -exp(-x)/x, from 1 to 2.
    3. (1/sqrt(2*Pi)) * exp(-(t^2)/2) from -2 to 2.
    4. sin(x^2)*exp(x), from 1 to 2.

Error Analysis

Suppose that | f ''(x) | < K for all x in [a, b]. Then the error in the midpoint rule using n subintervals can be estimated by:
En < K(b - a)3 / (24n3)

The Trapezoidal Rule

We can see that the midpoint rule takes a fairly large number of subintervals (n) to get close to the true value. The reason for this is that the boxes aren't really a very good approximation of the true area, and so we need the width (x) to be very small to get a good approximation.

A better approach is to estimate the area under each subinterval by a trapezoid (a rectangle with a triangle on top). We take the top of the trapezoid to be the secant line from f (xi-1) to f (xi).

The area of each trapezoid is the area of the rectangle with width x and height f (xi-1) plus the area of the triangle with base x and height f (xi) - f (xi-1).

Thus the area of the trapezoid is x f (xi-1) + ½ x ( f (xi) - f (xi-1)) = ½ x ( f (xi-1) + f (xi))

Now consider the sum of these terms;
½ x ( f (x0) + f (x1) ) + ½ x ( f (x1) + f (x2) ) + ½ x ( f (x2) + f (x3) ) + . . . + ½ x ( f (xn-1) + f (xn) )
= ½ x ( (f (x0) + f (x1) ) + (f (x1) + f (x2) ) + (f (x2) + f (x3) ) + . . . f (xn-1) + f (xn) )

Since each term except the first and last appears twice we can simplify this to get the fromula for the Trapezoidal rule:

f (x) dx ~ (b - a)/n ( f (x0) + 2f (x1) + 2f (x2) + . . . + 2 f (xn-1) + f (xn)).

Example

We will again try our formula on the integral of f (x) = exp(x)*sin(x) from 0 to Pi. We will use the fact that x0 = a and xn = b.

> trap := proc(f, a::realcons, b::realcons, n::integer)
> local dx, i, sm;
> dx := (b - a)/n;
> sm := 0;
> for i from evalf(a+dx) by evalf(dx) to evalf(b-dx) do
> sm := evalf(sm + 2*f(i));
> od;
> RETURN(evalf(dx*(sm + f(a) + f(b))/2));
> end;

Now we can use the procedure to find the value for various values of n, and compare them to the value reported by Maple.
> f:= x -> exp(x)*sin(x);
> evalf(int(f(x), x = 0..Pi));
> trap(f, 0, Pi, 10);
> trap(f, 0, Pi, 20);
> trap(f, 0, Pi, 50);
> trap(f, 0, Pi, 100);

  1. Use the Trapezoidal rule to estimate the following integrals, using n = 5, 10 and 50. Where possible check your answers with the Maple int function.
    1. exp(1/x), from 1 to 2.
    2. -exp(-x)/x, from 1 to 2.
    3. (1/sqrt(2*Pi)) * exp(-(t^2)/2) from -2 to 2.
    4. sin(x^2)*exp(x), from 1 to 2));

Error Analysis

Suppose that | f ''(x) | < K for all x in [a, b]. Then the error in the Trapezoidal rule using n subintervals can be estimated by:
En < K(b - a)3 / (12n3)
Thus we can see that the Trapezoidal rule is twice as good as the midpoint rule.

Simpson's Rule

The Trapeziodal rule improved on the midpoint rule by assuming that the curve over each subinterval was a straight line.
In Simpson's rule we use a parabola to estimate the curve over the subinterval.
However we need three points to define a parabola, and so instead of considering points in pairs (as with the trapezoidal rule) we consider them in triples. This means that we effectively use up 2 subintervals a time instead of one, thus we need to have that n is even.

We will adopt the notation that h = x, and yi = f (xi).

So with is notation we will use the area under parabola which goes through the points (x0, y0) (x1, y1) (x2, y2) for our first area.
The area under parabola which goes through the points (x2, y2) (x3, y3) (x4, y4) for our second area and so on.

Suppose first that our points are (-h, y0) (0, y1), (h, y2), and that our parabola has the form A x2 + B x + C. Then the area under it from -h to h is given by:
> P := x -> A*x^2 + B*x + C;
> int(P(x), x = -h..h);

Now, we know that this porabola goes through the points (-h, y0) (0, y1), (h, y2).
Thus y0 = P (-h)
y1 = P (0)
y2 = P (h)
Substituting these values in our polynomial gives
> P(-h);
> P(0);
> P(h);
> P(-h) + 4*P(0) + P(h);
Simpson noticed that h/3 (y0 + 4 y1 + y2) = 2/3 Ah3 + 2Ch = the area under the parabola.

Since moving the parabola left or right does not change the area under it we can use this formula for any three points, whether or not they are centered at 0. Thus in general the area under the parabola through the points (xi-1, yi-1) (xi, yi) (xi+1, yi+1) from xi-1 to xi+1 is given by h/3 (yi-1 + 4 yi + yi+1).

So the integral of f (x) from a to b can be approximated by:
h/3 (y0 + 4 y1 + y2) + h/3 (y2 + 4 y3 + y4) + . . . + h/3 (yn-2 + 4 yn-1 + yn)
Simplifying gives us Simpson's Rule:

f (x) dx ~ (b - a)/(3n) ( y0 + 4 y1 + 2 y2 + 4 y3 + 2 y4 + . . . + 2 yn-2 + 4 yn-1 + yn).

Notice the pattern of the coefficients 1, 2, 4, 2, 4, . . . , 4, 2, 4, 1.

Example

Once again we will again try our formula on the integral of f (x) = exp(x)*sin(x) from 0 to Pi. In this case we must keep track of the i's to assign the correct coefficients.

> simpson := proc(f, a::realcons, b::realcons, n::even)
> local h, i, sm, x;
> h := (b - a)/n;
> sm := 0;
> for i from 1 to n-1 do
> x := a + i * h;
> if type(i, even) then
> sm := sm + 2*f(x);
> else
> sm := sm + 4*f(x);
> fi;
> od;
> RETURN(evalf(h/3*(sm + f(a) + f(b))));
> end;

Now we can use the procedure to find the value for various values of n, and compare them to the value reported by Maple.
> f:= x -> exp(x)*sin(x);
> evalf(int(f(x), x = 0..Pi));
> simpson(f, 0, Pi, 10);
> simpson(f, 0, Pi, 20);
> simpson(f, 0, Pi, 50);
> simpson(f, 0, Pi, 100);

It is clear that Simpson Rule is a vast improvement over either the midpoint or the Trapezoidal rules.

  1. Use Simpson's rule to estimate the following integrals, using n = 5, 10 and 50. Where possible check your answers with the Maple int function. Also compare your results with those obtained by using the Midpoint and Trapezoidal rules.
    1. exp(1/x), from 1 to 2.
    2. -exp(-x)/x, from 1 to 2.
    3. (1/sqrt(2*Pi)) * exp(-(t^2)/2) from -2 to 2.
    4. sin(x^2)*exp(x), from 1 to 2));

Error Analysis

Suppose that | f (4)(x) | < K for all x in [a, b]. Then the error in the Trapezoidal rule using n subintervals can be estimated by:
En < K(b - a)5 / (180n4)


Up to Main Lab Page Next Lesson - Differential Equations Previous Lesson - Integration Top of this Lesson


Maintained by: P. Danziger, March 1998