Multivariable Mathematics With Maple - Linear Algebra, Vector Calculus and Differential - Carlson & Johnson
Multivariable Mathematics With Maple - Linear Algebra, Vector Calculus and Differential - Carlson & Johnson
c
1996
Prentice-Hall
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1. Introduction to Maple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1. A Quick Tour of the Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2. Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3. Graphing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4. Solving Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6. Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
7. Vector and Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
8. Programming in Maple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
9. Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2. Lines and Planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1. Lines in the Plane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2. Lines in 3-space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3. Planes in 3-space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4. More about Planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
36
36
39
41
43
49
49
52
58
61
64
67
67
71
74
77
82
86
87
89
91
93
95
99
101
101
102
104
109
113
118
124
125
132
134
8. Fourier Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1. Periodic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2. Computing Fourier Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3. Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4. Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5. Approximations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6. Appendix: Almost Periodic Functions . . . . . . . . . . . . . . . . . . . . . . .
137
137
143
147
149
150
151
156
156
160
160
162
168
168
171
172
174
176
181
181
184
184
186
190
ii
201
202
205
207
210
212
214
216
217
219
219
220
222
224
228
229
235
235
239
243
246
247
247
251
253
254
255
258
261
261
265
268
270
iii
iv
Introduction
Introduction
Chapter 1
Introduction to Maple
The purpose of this first chapter is to give a rapid overview of how one
can use Maple to do algebra, plot graphs, solve equations, etc. Maple can also
compute derivatives and integrals, solve differential equations, and manipulate
vectors and matrices. Much can be done with one-line computations. For example,
> expand((a + b)^3);
expands (a + b)3 to a3 + 3a2 b + 3ab2 + b3 , while
> plot( cos(x) + cos(2*x) + cos(3*x), x = -Pi..Pi );
constructs the graph of the function f (x) = cos x+cos 2x+cos 3x on the interval
[, ], and
> solve( x^2 + 2*x - 5 = 0 );
solves the quadratic equation x2 + 2x 5 = 0.
The best way to learn Maple is by using it. Begin by trying the three
examples above. The symbol > is the prompt, which Maple displays to signal
you that it awaits your command. Commands normally end with a semicolon.
Computers are much fussier about rules of punctuation, grammar, and
spelling than are humans. If something is not working right, check to see if
you are following the rules. For example, Maple will get confused if you say
solve( x^2 + 2x - 5 = 0 ) instead of solve( x^2 + 2*x - 5 = 0 ). Check
for things like misspelled names, extra or missing parentheses. If further thought
doesnt clear things up, ask a human for help. You will soon become an expert
troubleshooter.
Technical details on how to open the Maple program and how to save or
print a Maple file depend heavily on your local system. Thus no information is
Introduction to Maple
given here on these important aspects of getting started. Consult your manual
or local support staff if you need help.
While learning Maple, you will often have questions about how a particular
command or function is used. Fortunately, Maple can help you. To ask about
a command whose name you know, just type a question mark, followed by the
name of the command. Thus
> ?solve
gives information on the solve command. You will probably find the examples
at the end more useful than the technical information at the beginning of the
help file. Here are other things to try:
> ?
> ?intro
1.1
1.1
might expect. The second command gives a decimal or floating point answer
because we used floating point forms in our question.
To force Maple to give results in floating point form, use evalf:
> Pi; evalf( " );
> evalf( Pi );
> evalf( exp(1) );
The quote sign " or ditto stands for the most recently computed quantity.
Be aware that Maple distinguishes upper-case letters from lower-case. Thus
evalf(pi) is not the same as evalf(Pi). The function evalf can take a second
(optional) input which determines the precision of the output. (Inputs, called
independent variables in mathematics, are known as arguments in computer
jargon.)
> evalf( Pi, 100 );
For the most part, spacing is unimportant in Maple. In the commands
above, spaces could be omitted without causing any problems. However, thoughtful use of spacing makes Maple code easier to read, and so easier to understand
and, if necessary, to correct.
Standard mathematical functions can be used in Maple so long as we know
their names. To compute the quantities
2 + 14 8 + | 14| sin(1)
and
esin(1.6) +
2 + tan1 (3) :
use
> evalf( 2^(1/3) + 14*8 + abs(-14) - sin(1) );
> exp( sin(1.6*Pi) ) + sqrt(2) + arctan(3) ;
> evalf( " );
This example illustrates another important point. The correct form of a Maple
expression can often be found by intelligent guessing. Thus tan(1) does indeed
compute the tangent, and 20! computes a factorial. If your first guess does not
work, use ? together with your guess to get more information. For instance,
> arctangent( 1.0 );
produces only an echo, but
> ?arctangent
leads to the desired command and examples of its use. The query ?library
gives a listing of all the functions available.
Introduction to Maple
In addition to the functions in the Maple library, there are specialized packages of functions that can be read into the working memory as needed, using
the command with. Try
> ?packages
to see what is available. For example, to work with matrices and vectors, you
must load the linear algebra package. Do this by typing
> with(linalg);
This command produces a list of all the functions in this package and gives you
access to them in your current Maple session. If you close Maple and reopen it
later, you must reload any special packages you want to use.
Once you become familiar with a package, you will prefer to load it using a
colon
> with(linalg):
rather than a semicolon. Again, this gives you access to the linear algebra
commands, but without listing all their names. In general, end Maple commands
with a colon to prevent printing the results on screen. Other packages of interest
are
> ?plots
> ?DEtools
> ?student
1.2
Algebra
Maple knows about variables and algebra. Consider, for example, the expression (a + b)2 . The commands
> ( a + b )^2;
> expand( " );
give the expanded form, and
> factor( " );
brings us back to our starting point. To make long computations easier and
more intelligible, we can assign values to variables:
> p := ( a + b )^2;
b := 1;
p;
1.2
Algebra
# define a function
# check definition
but on other machines Maple ignores everything that appears between the first
comment symbol # and the next command prompt >. On those machines Maple
will not see the second line of commands f(2) and f(3).
Variables can be returned to their original symbolic (unassigned) state. The
commands
> b := b;
> p;
first remove the value 1 assigned to the variable b above and then display the
updated value of p. Similarly,
> unassign(pts, eqn, eqns, tag);
> print( pts, eqn, eqns, tag );
# check
clears the variables assigned above. A more drastic way of clearing Maples
memory is to say
> restart;
This command clears all variables and unloads all packages. Thus, if you need
one later, you must reload it using with.
Introduction to Maple
Pay special attention to the kind of quotes used in examples. The possibilities are the single quote , the backquote , and the double quote ". They all
play different roles.
Here is an extended example of how to use variables and assignment statements.
>
>
>
>
>
>
>
F :=
m :=
F;
a :=
F;
a :=
F;
m*a;
2.1; a := 5;
21.9;
a;
#
#
#
#
#
#
#
1.3
Graphing
Exercise 5. Maple has a built-in function for computing partial fraction decompositions. Start with ?partialfractions and follow the clues Maple gives
to find out how to use this function. Use it to rewrite
x2 2x + 5
x4 + 8x3 10x2 104x + 105
as a sum of simpler fractions.
1.3
Graphing
Maple can construct many kinds of graphs, a feature we will frequently use
to visualize mathematical objects and processes. The command
> plot( sin( 3*x ), x = -Pi..Pi );
produces a plot window containing the curve y = sin 3x for x in the interval
from to . No space is allowed between the dots in a plot command. Notice
that the scale on the x-axis is not the same as on the y-axis. To fix this we say
> plot( sin( 3*x ), x = -Pi..Pi, scaling = constrained );
Sometimes it is useful to restrict the range over which y varies. We get a misleading graph from
> plot( tan(x), x = -5..5 );
It does not accurately represent the vertical asymptotes of y = tan x. Better
results are obtained with
> plot( tan(x), x = -5..5, y = -5..5 );
As always: Think about your results. Are they reasonable?
We can plot several curves at once. To plot y = sin x and y = sin 3x together
we say
> plot( { sin(x), sin(3*x) }, x = -Pi..Pi );
Now the first argument of plot is a set of expressions to be graphed. Sets are
enclosed in curly braces and individual items are separated by commas. This
graph can be used to solve the equation sin x = sin 3x. Click with the mouse on
a point where these graphs cross. Maple gives the coordinates of the intersection
point and the x-value is a solution to the equation.
10
Introduction to Maple
Parametric Equations
A curve inthe plane can be described as the graph of a function, as in the
graph of y = 12 4 x2 for x [1, 1], or it can be given parametrically as in
(x(t), y(t)) = (2 cos t, sin t)
for t [0, ].
Often we interpret such a curve as the path traced by a moving particle in the
plane, with (x(t), y(t)) denoting the position of the particle at time t. Use the
plot command to draw a curve from this parametric description:
> plot( [ 2*cos(t), sin(t), t = 0..Pi ] );
The resulting graph is somewhat distorted, because Maple did not use the same
vertical and horizontal scales. There are two ways to fix this. Use
> plot( [ 2*cos(t), sin(t), t = 0..Pi ] , -2..2, -2..2 );
or use the option scaling = constrained.
Polar Coordinates
Polar plots are a special kind of parametric plot. The polar coordinates
(r, ) of points on a curve can be given as a function of some parameter t. In
many cases the parameter is just the angle . Consider the ellipse defined in
standard coordinates by x2 + 4y 2 = 4. To find an equation relating the polar
coordinates r and of a typical point on this ellipse, we make the substitution
x = r cos t and y = r sin t, where t = .
> subs( x = r*cos(t), y = r*sin(t), x^2 + 4*y^2 = 4 );
simplify( " );
solve( ", r );
We find that the ellipse is the collection of points whose polar coordinates (r, )
satisfy
4
r2 =
4 3 cos2
and the following commands draw the right half of the ellipse:
> r := 2/sqrt( 4 - 3*cos(t)^2 );
> plot( [ r, t, t = -Pi/2..Pi/2 ], coords = polar );
Try the examples below. However, before asking Maple to do the plot, try
to predict what the result will be.
> plot( [ 1, t, t = 0..2*Pi], coords = polar );
> plot( [ t, t, t = 0..2*Pi], coords = polar );
> plot( [ sin(4*t), t, t = 0..2*Pi ], coords = polar );
As usual, a more realistic picture is obtained with scaling = constrained.
1.3
Graphing
Plotting Data
Maple can plot data consisting of pairs of x and y values. For example, if
we say
> data := [ [0, 0.53], [1, 1.14], [2, 1.84], [3, 4.12] ];
then data refers to a sequence of five points, (x, y) = (0, 0.53), etc. The result is a
list: something enclosed in square brackets, with elements separated by commas.
Lists are used for collections of objects where the order matters. Individual items
are accessed this way:
> data[1]; data[2]; data[3]; data[4];
In our case, the list items are themselves lists very short ones made up of the
coordinates of a point. To access the second coordinate of the third data point
we say
> data[3][2];
To plot the points in our list we use commands like
> plot( data, style = point );
> plot( data, style = point, symbol = diamond );
> plot( data, style = line, view = [ 0..4, 0..5 ] );
You could also try
> plot( data, style = line, title = Experiment 1 );
Here the backquote is used to specify a plot title. See ?plot[options] for
more information, e.g., about symbols and line styles available. See ?readdata
or ?stats to find out how to read a file of data points into a Maple session.
(You could discover these commands on your own by typing ?data or ?reading
data.)
Exercise 1. Graph y =
x3
6x2
1
on the interval 1 x 4.
+ 11x 6
1
Exercise 2. Graph y =
on the interval 27 x 27. Do your results
3
x
make sense?
11
12
Introduction to Maple
Exercise 4. Graph
y = cos x
cos 3x
for x .
3
Then graph
y = cos x
cos 3x cos 5x
+
for x .
3
5
What happens if we continue this pattern that is, what does the graph of
y = cos x
cos((2k + 1)x)
cos 3x cos 5x
+
+ + (1)k
3
5
2k + 1
Exercise 5. Graph the curve, defined by the polar equation r = 2(1 + cos ).
(This curve is called a cardioid.)
Exercise 6. The path of a point P on a circle of radius 2 that rolls along the
outside of a larger circle of radius 5 is given by
7t
2
and
7t
2
Draw the path of this particle for t in [0, 5]. Repeat for t in [0, 20]. (This curve
is called an epicycloid.)
1.4
1.4
Solving Equations
Solving Equations
13
14
Introduction to Maple
How did the sum and product relate to the coefficients of the equation?
We can also solve systems of equations:
> solve( { 2*x + 3*y = 1, 5*x + 7*y = 2 } );
> x; y;
A system of equations is given as a set a bunch of items enclosed in curly
brackets and separated by commas. Sets are often used when the order of the
objects is unimportant.
In reply to the solve command above, Maple tells us how to choose x and
y to solve the system, but it does not give x and y these particular values. To
force it to assign these values, we use the assign function:
> s := solve( { 2*x + 3*y = 1, 5*x + 7*y = 2 } );
> assign( s );
> x; y;
# Check that it worked.
Warning: You may have trouble later if you leave numerical values assigned
to the variables x, y, and r. Maple will not forget these assigned values, even
though you have gone on to a new problem where x means something different.
It is a good idea to return these variables to their unassigned state when you
finish your problem. Recall how to do this:
> x := x;
y := y;
r := r;
1.4
Solving Equations
solve( x^5 + x = 32 );
solve( x^5 + x = 32.0 );
fsolve( x^5 + x = 32, x );
fsolve( x^5 + x = 32, x, complex );
Exercise 3. Using
> e1 := 7*x + 3*y + 8*z = 1;
solve({e1, e2, e3});
e2 := ... ;
e3 := ... ;
7x + 3y + 8z = 1 (b)
2x 4y + 5z = 2
3x 7y + 9z = 1
7x + 3y + 8z = 1
2x 4y + 5z = 2
Exercise 4. Use solve to find the intersection points of the two ellipses defined
by
4x2 + 9y 2 = 1 and 25x2 + 4y 2 = 1.
Try using fsolve instead of solve. Finally, change the constant term in the
first equation to 1.0 and try solve again. Do the results agree? Draw a picture
to clarify the situation. You can do this by hand, or in Maple. The first ellipse
can be described parametrically by the expression ( 12 cos t, 13 sin t) where t runs
over the interval [0, 2]. The second ellipse has a similar parametric description.
By filling in the missing information below, you can plot both curves together:
> ellipse1 := [ cos(t)/2, sin(t)/3, t = 0..2*Pi ];
ellipse2 := [ ??? ];
plot({ ellipse1, ellipse2 }, scaling = constrained );
Exercise 5. Find all solutions to sin x + x = sec x for x in [3, 3]. Use plot to
get information about how many solutions exist and their approximate location.
Then use fsolve to locate the solutions to 9 decimal places accuracy.
15
16
Introduction to Maple
1.5
Functions
p( -2 );
p( 1/2 );
p( 0.4 );
p( a + b );
simplify( " );
p;
p(x);
p(y);
p(3);
#
#
#
#
function
expression
expression
expression
factor( p );
factor( p(x) );
plot( p, x = -2..2 );
plot( p, -2..2);
plot( p(x), x = -2..2 );
Which of these worked? Does the factor command work on functions or expressions? What about plot?
1.5
Functions
17
18
Introduction to Maple
Exercise 5. Define f (x) = sin( x) using each of the three styles of function
definition. Use each definition to compute f (2) in decimal form.
1.6
Calculus
Let us now explore Maples tools for working with the notions of calculus.
1.6
Calculus
Derivatives
To compute the derivative of the expression x3 2x + 9, say
> diff( x^3 - 2*x + 9, x );
To compute the second derivative, say
> diff( x^3 - 2*x + 9, x, x );
or alternatively,
> diff( x^3 - 2*x + 9, x$2 );
This works because Maple translates the expression x$2 into the sequence x, x.
By analogy, x$3 would give the third derivative. Thus one can easily compute
derivatives of any order.
Now suppose that we are given a function g defined by
> g := x -> x^3 - 2*x + 9;
It seems natural to use
> diff( g, x );
to get the derivative g 0 . However, Maple expects an expression in x and so
interprets g as a constant, giving the wrong result. The command
> diff( g(x), x );
which uses the expression g(x), works correctly. The subtlety here is an important one: diff operates on expressions, not on functions g is a function while
g(x) is an expression.
To define the derivative of a function, use Maples D operator:
> dg := D(g);
The result is a function. You can work with it just as you worked with g. Thus
you can compute function values and make plots:
> dg( 1 );
> plot( { g(x), dg(x) }, x = -5..5,
title = g(x) = x^3 - 2x + 9 and its derivative );
19
20
Introduction to Maple
Partial derivatives
Maple can compute partial derivatives:
> q := sin( x*y );
> diff( q, x );
> diff( q, x, y );
D[1](k);
D[2](k);
D[1,1](k);
D[1,2](k);
D[1](D[2](k));
#
#
#
#
#
int(
int(
int(
int(
1/x, x );
x*sin(x), x );
sin( 3*x + 1 ), x );
x / (x^2 - 5*x + 4), x );
x3 dx
0
1.6
Calculus
we say int( x^3, x = 0..1 ). Note that the only difference is that we give an
interval of integration.
Let us return to the integral
Z
p
sin( 1 x3 ) dx
which Maple could not evaluate. We can still find an approximate numerical
answer using the definition of the integral: approximate the figure under the
graph by little rectangles or trapezoids and add up their areas. You could write
a loop to do this (see Section 8) or try:
> int( sin( sqrt(1 - x^3) ), x = 0..1 );
> evalf( " );
The second command forces Maple to apply a numerical method to evaluate the
integral. Of course, you could also put everything on one line:
> evalf( int( sin( sqrt(1 - x^3) ), x = 0..1 ) );
Numerical Integration
Another approach to numerical integration is to use the student package.
>
>
>
>
with( student );
j := sin( sqrt(1 - x^3) );
trapezoid( j, x = 0..1 );
evalf( " );
#
#
#
#
load package
define integrand
apply trapezoidal rule
put in decimal form
By default
the trapezoid command approximates the area under the graph of
sin( 1 x3 ) with four equal trapezoids. For greater accuracy use more trapezoids, i.e., a finer subdivision of the interval of integration:
> evalf( trapezoid( j, x = 0..1, 10 ) );
Better yet, use a more sophisticated numerical method like Simpsons rule:
> simpson( j, x = 0..1 );
> evalf( " );
Only an even number of subdivisions is allowed, as in simpson( j, x = 0..1,
10 ).
The student package is well worth exploring. Among other things it has
tools for displaying figures which explain the meaning of integration:
> leftbox( j, x = 0..1, 10 );
21
22
Introduction to Maple
(x2 + y 2 ) dx dy.
0
(x2 + y 2 ) dy dx
1
The integrand is the same, but the order of integration is different. How do the
intermediate and final results compare with the previous computation?
Maple can compute double integrals over more complicated regions, such as
the triangle T defined by 0 x 1 and x y 1. We have
Z 1 Z 1
Z
2
2
2
2
(x + y ) dxdy =
(x + y ) dy dx
T
1.6
Calculus
Exercise 2. Compute
Z
1
1
|x3 y 5 | dy dx
#
#
#
#
integrand
y integral
x integral
numerical result
Note the use of colons to suppress output. Repeat the computation with 16
subintervals. How accurate do you think the result is? Now reverse the order of
integration and compare answers.
|x3 y 5 | dy dx.
# y integral
# x integral
# numerical result
g := x
limit(
limit(
limit(
taylor( exp(x), x = 0, 4 );
sum( i^2, i = 1..100 );
# Be sure i is clear
sum( x^n, n = 5..10 );
# needs n to be clear
sum( 1/j^5, j= 1..infinity ); evalf( " );
23
24
Introduction to Maple
Differential equations:
> deq := diff( y(x), x$2 ) + y(x) = 0;
> dsolve( deq, y(x) );
> ?dsolve
We can specify initial conditions and experiment with parameters.
>
>
>
>
k := k;
de := diff( y(x), x ) = k*y(x)*(1 - y(x));
sol := dsolve( {de, y(0) = 0.1}, y(x) );
k := 1; sol;
To graph the solution, we need only the right-hand side of sol, which we can
get by
> plot( rhs(sol), x = 0..10, title = k = 1 );
> k := 2; plot( rhs(sol), x = 0..10, title = k = 2 );
Exercise 4. Find the derivative of x5 + 7x2 + x + 1. Find the critical points
of f (x) = x5 + 7x2 + x + 1, and determine if they give maxima, minima, or
inflection points.
1 + 7x2 . Compute
R1
0
1 + 7x2 in
1
1
+ ... + 3 ...
3
2
n
that you believe accurate to three decimal places. How many terms do you need
to compute to get this accuracy?
(b) Repeat for
1
1
1
+ . . . + (1)n+1 . . . .
2
n
1.7
1.7
To work with vectors and matrices in Maple, first load the linear algebra
package:
> with(linalg);
Define and display a vector like this:
> v := vector( [1, -1] );
> v[1];
v[2];
v;
> print( v );
Note that Maple treats vectors as columns, even though it displays them as rows.
Now define another vector w and do some simple computations:
> w := vector( [1, 1] );
> add( v, w );
> dotprod( v, w );
What does this tell you about the geometry of v and w?
Next we define two matrices:
> A := matrix([ [2, 3], [1, 2] ]);
> B := matrix([ [1, 1], [0, 1] ]);
It is easy to make new matrices (or vectors) from old ones as in
> stack( A, B );
> augment( A, v, w );
Next, we do some computations with our matrices A and B and our vectors v
and w:
>
>
>
>
add(A, B);
scalarmul(A, 2);
multiply(A, B);
multiply(A, v + w);
#
#
#
#
or
or
or
or
use
use
use
use
evalm(A + B);
evalm(2*A);
evalm(A &* B);
evalm(A &* (v+w));
25
26
Introduction to Maple
# check
vw
= arccos
|v||w|
where |v| stands for the length of v. Define a function Length that computes
the length of a vector v and a function Angle that computes the angle between
vectors v and w. Use it to find the angle between v = (1, 2, 3) and w = (1, 4, 1).
You may want to use evalf to get results in decimal form. Is your result in
radians or degrees? How would you modify your function definitions to get the
opposite?
1.8
Exercise 3. Let
A= 4
7
2
5
8
3
2
6
and B = 1
9
0
1
2
1
Programming in Maple
0
1.
2
Compute AB and BA. Are they the same? Compute A + B and B + A. Are
they the same? Compute 7A. Define C to be the sum of A and B. Then
compute C 2 and compare to A2 + 2AB + B 2 . Compute the determinants of
A and of B. Where possible, compute the inverses of A and B. Compute Av
where v = (1, 2, 3) as above. Is it possible to compute vA? Let x be an unknown
vector and solve Bx = v.
Exercise 4. Let A be the same matrix as in the previous exercise. Use augment
and gaussjord to solve Ax = v, where v = (1, 2, 3). Repeat for Ax = w, where
w = (1, 4, 1).
1.8
Programming in Maple
1
1 1
+ + ... + 2.
4 9
n
(8.1)
1
1 1
+ + ... + 2 +
4 9
n
One way to compute them is to write out the defining expression for s(n) whenever we need to compute it:
>
>
>
>
>
1;
1 + 1/4;
1 + 1/4 + 1/9;
1 + 1/4 + 1/9 + 1/16;
etc.
27
28
Introduction to Maple
This works, but is not a good solution when n is large. We can cut down
somewhat on the amount of typing needed by judicious use of the ditto symbol,
which remembers the result of the last computation:
>
>
>
>
1;
" + 1/4;
" + 1/9;
" + 1/16;
:=
:=
:=
:=
:=
:=
4;
1;
2;
3;
4;
5;
total
total
total
total
total
:=
:=
:=
:=
:=
0;
total
total
total
total
+
+
+
+
1.0/i^2;
1.0/i^2;
1.0/i^2;
1.0/i^2;
Consequently, the counter i has the value 5 when the loop is complete. This
may cause trouble later, so you might want to clear the variable i with i :=
i when you have finished the problem.
1.8
Programming in Maple
Our loop as written prints out all the intermediate sums. For large n this is
not a good idea, so we rewrite our computation with colons to suppress display
of all but the final results:
> n := 10;
total := 0.0:
for i from 1 to n do
total := total + 1.0/i^2:
od:
total;
Warning: Only one Maple prompt (>) appears in the loop. This is the safest
way to type in a loop because it ensures that every time you start a loop, you
also end it. A command like
works, but can easily lead to disaster. For example, you might start typing a loop
and realize that you forgot to do something else first, say define a function that
you need inside the loop. Then you move back in your Maple file to take care
of this oversight. You have abandoned the loop, but Maple has not. Everything
you enter after a line like for ... do gets incorporated into the body of your
loop until you enter a line containing od to end the loop or until Maple crashes
and you lose all your unsaved work because Maple is irredeemably confused by
the nonsensical instructions it has been given. To avoid this problem type your
loops and procedures on a single Maple command line. To continue a command
on a new line without getting a new Maple prompt, you use Enter on a Unix
machine and Return on a Macintosh.
Conditionals
Next, consider the problem of finding partial sums of the series
1
1
1 1
+
+ ... 2 +
4 9 25
n
(8.2)
Depending on whether n is odd or even, we add or subtract the term 1/n2 . This
29
30
Introduction to Maple
1.8
Programming in Maple
sums. One important difference is that we declared the variables i and total
to be local. This means that they cannot be seen outside the function S, and,
moreover, that nothing that happens outside S can affect them. This kind of
protection is useful, since we cant keep in mind the names of all the internal
variables used by functions. The value of S(n) is the value of the last expression
in the definition, namely, total. One can also use the RETURN statement to
define the function value:
> S := proc( n )
local i, total;
total := 0.0;
for i from 1 to n do
total := total + 1.0/i^2;
od;
RETURN ( total );
end;
Warning: As with loops, it is much safer to type the entire definition with a
single prompt (>).
With a small bit of extra work, we can devise a procedure which computes
the nth partial sum
Sn = f (1) + f (2) + . . . + f (n)
where f is an arbitrary function. Here is the definition:
> S := proc( f, n )
local i, total;
total := 0.0;
for i from 1 to n do
total := total + evalf( f(i) );
od;
[ n, total ];
end;
The arguments of S are a function and a number. The function value S(f,n) is
a list consisting of two items: the number of terms in the sum, and the value of
the partial sum.
Warning: There is no punctuation after the phrase proc( f, n ). Also, with
procedures as with loops, it is much safer to type the entire definition on a single
Maple command line.
31
32
Introduction to Maple
1
1 1
+ + ... + + ...
2 3
n
Lists
Another way to think about the behavior of an infinite sum is to plot the
partial sums Sn versus n. This gives a picture of how the sum grows as n
increases. We can modify our procedure S to produce a list of points (n, Sn ) and
then use plot to graph them.
To define our list we use Maples sequence-building command. For instance,
to generate a list of squares we can type
> i := i;
# Make sure i is unassigned
> [ i^2 $ i = 1..10 ];
Whenever you use $ be sure that the indexing variable is unassigned or you will
get an error message. Alternatively, type
> [ i^2 $ i = 1..10 ];
to tell Maple to ignore any values previously assigned to the variable i when it
makes this list of squares. The quotation marks here are single quotes.
Let us call our modified procedure Spts. We define it like this:
> Spts := proc( f, n )
local i, j, total, pts;
total := 0.0;
for i from 1 to n do
total := total + evalf( f(i) );
pts[i] := [i, total]:
od;
[ pts[j] $ j = 1..n ];
end;
1.8
Programming in Maple
Spts( f, 10 );
Spts( f, 10 )[10];
plot( Spts(f, 10), style = line, labels = [n, Sn] );
Since we assigned n the value 10 earlier, this last command did not work. Try
again:
> n := n;
plot( Spts(f, 10), style = line, labels = [n, Sn] );
Now we have the desired plot. Replot using the first 100 terms of the series.
Does it look like the sum approaches a well-defined number as we take more and
more terms?
Exercise 1. Define a function f (x) = sin x/x. Investigate the limit of f (x) as
x 0 by computing the sequence f (1), f (1/2), . . . , f (1/10) with a loop.
Exercise 3. (a) Use the procedures S and Spts to investigate the limit of the
partial sums
1
1
Sn = 1 + + . . . + 2
4
n
as n . Does the limit exist? That is, do the partial sums approach some
well-defined number or do they grow without bound?
(b) Repeat for the harmonic series
1+
1
1
+ ... + + ...
2
n
1
1 1 1
+ + ... + ...
2 3 4
n
Does it converge? If so, does it converge quickly or slowly? Compare with the
series
1
1
1 1
+ ... 2 + ...
1 +
4 9 16
n
33
34
Introduction to Maple
Exercise 5. Write a procedure S3 that sums the first n cubes. Notice that the
first five values, obtained by
> i := i;
> [S3(i) $ i = 1..5];
are perfect squares. Does this pattern continue? Modify your procedure to sum
the first n cubes, then take the square root of this sum, and give
i
h
3, 5,
7, 9, 11,
...
Notice that the first term is 1 , the sum of the next two terms is 8 or 23 , and the
sum of the next three terms is 27 or 33 . The sum of the next four terms is 64 or
43 . Does this pattern continue? Use Maple to check that this pattern continues
up to at least 153 . How could one be sure that it continues forever?
The next exercise contains a new kind of loop, the while-do loop. Its
general form is
while ( <test> )
do
<something>
<something else>
<etc>
od;
Exercise 7. Another variation on the procedures S and Spts is the following:
> G := proc( L )
local s, n;
s := 0.0;
n := 0;
while (s < L)
do
n := n+1;
s := s + 1.0/n;
od;
[s, n];
end;
Compute G(1.0), G(2.0), G(3.0), G(4.0), G(5.0), G(6.0), G(7.0). Try to
figure out what the procedure G is doing. What does it tell you about the
harmonic series 1 + 1/2 + + 1/n + ?
1.9
1.9
Troubleshooting
Troubleshooting
Although errors in Maple can be quite confusing at first, you will quickly
gain the experience needed to understand and fix them. Below is a list of common
errors to consider when troubleshooting.
1. Do statements end with a semicolon or colon?
2. Are parentheses, braces, brackets, etc., balanced? Code like { x, y } is
good but x, y } will cause trouble.
3. Are you trying to use something as a variable to which you have already
assigned a value? To clear a variable x, say unassign(x). You can
clear many variables at once, e.g., unassign(x, y). You will not have
to reload any packages. Remember, a variable that has a value assigned to
it no longer can function as a variable. To display the value of an ordinary
variable, type its name, followed by a semicolon, e.g.,
> x;
4. If things seem hopelessly messed up, issue the command
> restart;
5.
6.
7.
8.
9.
10.
11.
12.
35