Up to Main Lab Page | Next Lesson - Differential Equations | Previous Lesson - Integration |
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.
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 ).
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);
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:
> 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);
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:
Notice the pattern of the coefficients 1, 2, 4, 2, 4, . . . , 4, 2, 4, 1.
> 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.
Up to Main Lab Page | Next Lesson - Differential Equations | Previous Lesson - Integration | Top of this Lesson |