Mathematics 134 -- Intensive Calculus for Science 2
Lab 1: Numerical Integration in Maple
February 14, 2001
Goals
Today, we will use Maple to compute some approximate
values for integrals, using the LEFT, RIGHT, MID, and TRAP
methods. We will look for patterns in the approximations
and the errors for all methods.
The Student Package
The commands we will use today are contained in the "student"
package, a collection of routines for basic calculus operations.
To begin, you should enter the command:
with(student);
to load the package.
All the commands we will work with today perform various
integration operations on functions defined by expressions
(formulas).
Detailed Information on Maple Expression Syntax
Here is
some more information about the syntax rules that Maple uses to
decide if what you have typed in is a well-formed expression
for a function.
For a function described by a formula, the formula is entered in something
like usual mathematical notation:
- The symbols for addition, subtraction, multiplication, and division
are +, -, *, / respectively.
- The caret (^) is the Maple symbol for raising to a power.
- The asterisk symbol for multiplication MUST be included whenever you
are performing a product in a formula. Moreover, everything must be entered
in one string of characters, so you will need to use parentheses
to group terms to get the expressions you want. The rule to keep in mind
is: Maple always evaluates expressions by doing powers first, then products
and quotients, then sums and products, left to right, unless parentheses
are used to override these built-in rules. For example, the Maple expression
a + b^2/c + d is the same as the mathematical formula:
a + b2/c + d. If you really wanted a + b2
in the numerator and c + d in the denominator of a fraction, you
will need to enter the expression
(a + b^2)/(c + d). What if you really wanted
(a+b)2 in the numerator?
- Maple ``knows'' all the functions we studied in Chapter 1 of
our text. The
names of the most common ones are sin, cos, tan, exp, ln. To use
one of these functions in a Maple formula, you put the name, followed by
the "argument" (that is the expression you are applying the function
to) in parentheses.
Numerical Integration Commands
The formats for the various commands we will use today are:
leftsum(function,x=a..b,n);
rightsum(function,x=a..b,n);
middlesum(function,x=a..b,n);
trapezoid(function,x=a..b,n);
In each case, you fill in the formula for the function whose
integral you are approximating, you supply the a,b
for the interval, and you supply the value of n,
the number of subdivisions.
The output of each of these commands is a formula for a
summation. To evaluate the sum and get a decimal number,
you can enclose the whole command inside an evalf( ).
For example, to approximate the integral of sin(x^2)
from a = 2 to b = 3 with the trapezoidal
rule and n = 20 subdivisions, giving the value as
a decimal, you would enter:
evalf(trapezoid(sin(x^2),x=2..3,20));
Maple also contains a suite of more sophisticated numerical methods
for definite integrals. To have Maple compute an approximate
value of the integral
of sin(x^2) from a = 2 to b = 3,
for example, you would use:
evalf(Int(sin(x^2),x=2..3));
Note the capital I in the "Int". This is important -- it tells
Maple, in effect, to bypass the process of looking for a
formula for an indefinite integral and the Fundamental Theorem
and to go directly to its numerical methods.
Lab Questions
- For each of the following functions and intervals,
construct a table giving the values of LEFT(n),RIGHT(n),TRAP(n),MID(n)
for n = 4,10,50,100,200. Round Maple's output to 6 decimal places in
all cases.
- A) f(x) = cos(x)/x on [a,b] = [1,2]
- B) f(x) = e-x2 on
[a,b] = [0,1/2]
- C) f(x) = sqrt(x3 + 3x) on [a,b] = [0,1]
- Now, use Maple's sophisticated numerical integration routine
to compute a value for each of the integrals in the problem above.
Treat that answer as the exact value of the integral. (It's
certainly closer than any of the approximations you computed
in the first problem!) Make a second table giving the error
for each approximation you found in the first problem.
- Based on the data you found above and our understanding
of the numerical methods, answer the following questions.
You may also want to plot the functions above to see various
features of their graphs. Recall that you can do that in
Maple using the plot command. To plot y = cos(x)/x
on [a,b] = [1,2], for instance, you could use:
plot(cos(x)/x,x=1..2);
- a) What property of a function on an interval [a,b]
guarantees that TRAP(n) overestimates the actual value of
the integral? What property guarantees that TRAP(n)
underestimates the actual value of the integral?
- b) What property of a function on an interval [a,b]
guarantees that MID(n) overestimates the actual value of
the integral? What property guarantees that MID(n)
underestimates the actual value of the integral? (Hint:
The value of MID(n) also equals the sum of the areas of
a collection of trapezoids. Each one has slanted side
tangent to the graph at (mi,f(mi)).)
- c) As n increases in these examples, how does the number
of correct decimal places change for each of the methods?
Which methods are more accurate in the sense
that you get more correct decimal places for the same n value?
- d) How does the size of the error for TRAP(n) and MID(n)
compare, at least for n large? (There is a definite pattern
here that would be proved, for instance in courses in
Numerical Analysis, the part of mathematics that we are
getting into in considering numerical integration methods.)
Assignment
The lab write-up is due in class Wednesday, February 21.