This document is a publication of the Center for Statistical and
Mathematical Computing, Indiana University, Bloomington, Copyright
1994. Please credit IU, the Stat/Math Center, and the author when
referring to and/or copying this publication for your own purposes.
(c) Indiana University Center for Statistical and Mathematical Computing, 1992-1994.
Prepared by David Hart. Last modification: 8/1/94. This document may be freely copied, but must not be changed.
====================================================================
Getting Started with Maple
Maple is a computer program for people doing mathematics. Using Maple to do your calculations should make the work more interesting, allow you to focus more on the concepts, and help you to avoid silly calculation mistakes.
This document is intended to get you started, and show you how to learn more.
In order to be more easily understood, it doesn't include some things which Maple does quite well (for example, linear algebra). Maple can be used either interactively or in "batch mode," but we will focus on interactive use.
To use any software effectively, some knowledge of the computer's operating system is required. This document will assume that you are already familiar with the rudiments of windows -- things like point, click and drag. It is intendgeted to be usable with either Microsoft Windows, Macintosh, or X windows; Maple is essentially the same on all three.
Maple is available in all IUB Public Computing Facilities. On UNIX workstations, the X windows version is invoked by the command xmaple. On PCs, Maple is among the Windows applications in a folder marked Maple; on Macintoshes, Maple is in the Statistics and Mathematics folder.
EXERCISE: Start Maple.
Computers are picky about details. Some of Maple's points worth noting:
.The Return key (next to the letters) and Enter key (in the corner, next to the numbers) are different. For Microsoft and X windows, the Return key starts the command processor; on Macs, it's the Enter key (the return key makes a new line).
.Every command must end with a semicolon (or a colon if you don't want output displayed).
.Case matters: "pi" is just a Greek letter, but "Pi" is a geometric constant.
We will present some examples of Maple commands (after the > prompt), with output, and some interspersed comments.
EXERCISE: Enter the commands given, or make up similar problems.
. Maple does exact calculation.
> 1/2 + 1/3;
> 1/sqrt(2);
If you enter a command without the semicolon, you get another prompt, but no output; just put ";" on the new line, and enter that, to proceed.
> 2^99
> ;
You may then go back and edit your commands, and reenter them.
If you enter 2^99, with no semicolon, then move the cursor back, add the semicolon and enter the same line again, the "computational engine" will receive 2^99 2^99; and will complain about syntax.
> 2^99;
syntax error:
2^99 ;
^
>
However, if you then enter this again, it will work.
.Maple also does approximate calculation, if it sees a decimal point or if asked to evaluate an expression as a "floating-point" number.
> 1.0/2 + 1/3;
> evalf( 1/sqrt(2) );
. The main feature of Maple is symbolic calculation.
> (x*y - y^2)/(x - y);
. Multiplication must be explicitly indicated: x y is not the same as x*y.
> simplify(");
Note the use of ditto marks to refer to the last computed result. You can also name expressions, for later reference. The next example makes "p" an abbreviation for the expression on the right.
> p := x^2 - 8*x + 15;
. ":=" is read is assigned to, and plain "=" is used for equations.
> solve(p = 3, x);
> factor(p); factor(p - 3);
. You may enter more than one command at a time.
We can use Maple to solve a set of equations. { } indicate sets.
> solve( {2*x + 3*y = 22, x + 2*y = 13}, {x,y} );
> solve( {a*x + b*y = U, c*x + d*y = V}, {x,y} );
We can make a list of the solutions, and refer to any particular one.
[ ] indicate lists, or "operands" (such as entries in lists).
> soln := [ solve(x^3 - x^2 + x - 1, x) ];
> soln[2];
> soln[1] + soln[3], soln[2]^2;
We can also use Maple to do calculus.
> dpdx := diff(x^2 - 8*x + 15, x);
> cp := solve(dpdx = 0, x);
> Int(dpdx, x) = int(dpdx, x) + C;
. Indefinite integration does not add a "constant of integration" term.
. Select Format, Math Style, Character if your monitor doesn't display the Ì symbol.
> int(x^2, x = 0 .. 2);
> Int(sqrt(1 + cos(x)), x=0..Pi) = int(sqrt(1 + cos(x)), x=0..Pi);
WARNING...
In any windowed version of Maple, you may edit and re-enter a command. For example, you might have
> x := 4:
> y := sqrt(x);
and then edit the first line to produce
> x := 9:
> y := sqrt(x);
If the two commands are widely separated, and the answers not obvious, this can be a real issue. There are two problems to watch for:
.Changing x does not change y, nor the output of any other command.
.You cannot determine the state of the computational engine, simply by looking at the worksheet.
Also, if you set x := 4, you won't be able to solve(2*x = 4, x). Maple will interpret this as solve(2*4 = 4, 4). To restore the variable x to an "unassigned" state, use the single-quote, as follows:
> x := 4;
> solve(2*x = 4, x);
Error, (in solve) a constant is invalid as a variable, 4
> x := 'x';
> solve(2*x = 4);
It is often possible to avoid such an assignment by using substitution.
> subs( x = 2, x^2 + 1);
Be careful to distinguish the single-quote (which delays evaluation) from the backquote. The backquote is used to mark character strings.
> print(`Go Big Red!`);
WHAT IF IT WON'T STOP?
On UNIX workstations, there are pause and interrupt [abort] buttons; they work.
In principle, control-c or control-break on a PC, or command-dot on a Mac, will interrupt a process -- the next time the operating system checks to see what you're up to. If it's busily crunching a billion numbers, that might be a while.
Save frequently, to ensure you don't lose your work.
HOW TO SAVE AND RETRIEVE YOUR WORK
Maple for Windows saves files much as any other Windows application: use the File menu, select Save. Select Open to retrieve a worksheet. Maple for the Mac works similarly, but the retrieved worksheet is text only -- the internal state is not restored (you must press enter for each command to be reentered).
EXERCISE: Set a:= 16; , then Save your session, Quit, restart Maple and reload your worksheet. Check to see if assignments are still valid (i.e., if a:=16 before saving, is a:=16 after retrieval?).
PACKAGES
Maple has a "modular" design -- not all of its functions are loaded at startup. The more specialized capabilities must be explicitly loaded. For example, to add the multivarate Taylor series function, mtaylor, first use the command
> readlib(mtaylor);
To simplify this process, most of these functions have been gathered into packages, which may be loaded as a group. For example, to load functions for linear algebra and vector calculus, enter
> with(linalg):
To get a list of them, end the command with a semicolon.
EXERCISE: Enter one of the following commands, and note the added functions (we will tell you how to find out what they do, in detail, in the next section).
with(linalg); with(student);
with(plots); with(stats);
THE MAPLE HELP SYSTEM
For general help, Maple offers a "Help Browser." You can search through categories for something appropriate. There is also a keyword search, which is very useful. Finally, the Maple Library Reference Manual collects all of the help files in one book (available in many Public Computing Facilities).
To get help on a given Maple command or procedure, say simplify, enter ?simplify at the Maple prompt. A window pops up containing:
FUNCTION: simplify - Apply Simplification Rules to an Expression
CALLING SEQUENCE:
simplify(expr)
simplify(expr, n1, n2, ...)
simplify(expr, assume=prop)
PARAMETERS:
expr - any expression
n1, n2,... - (optional) names or sets or lists
prop - any property (optional)
SYNOPSIS:
The simplify function is used to apply simplification rules to an
expression. If only one argument is present, then simplify will search the
expression for function calls, square roots, radicals, and powers. Next it
will invoke the appropriate simplification procedures, which include:
BesselI, BesselJ, BesselK, BesselY, Ei, GAMMA, RootOf, W, dilog, exp, ln,
sqrt, trig (for trig functions), hypergeom (for hypergeometrics), radical
(occurrence of fractional powers), power (occurrence of powers, exp, ln),
and atsign ("@") (for operators).
- Further information on particular simplification procedures is available for
the subtopics simplify[<name>] where <name> is one of: Ei, GAMMA, RootOf,
atsign, hypergeom, ln, polar, power, radical, sqrt, trig
- In the case of two or more arguments where the additional arguments are
names, simplify will only invoke the simplification procedures specified by
the additional arguments.
- A user can make his own simplifications known to the simplify function by
defining a Maple procedure. If the procedure `simplify/f` is defined then
the function call simplify(a,f) will invoke `simplify/f`(a) .
- The case of two or more arguments where the additional arguments are sets or
lists is used for simplification with respect to side relations. See the
subtopic simplify[siderels] for details.
- Whenever the last argument is assume=prop, all the indeterminates in expr
are assumed to have the property prop to compute the simplified expression.
EXAMPLES:
> simplify(4^(1/2)+3);
5
> simplify((x^a)^b+4^(1/2), power);
a b
(x ) + 2
> simplify(exp(a+ln(b*exp(c))));
b exp(a + c)
> simplify(sin(x)^2+cos(x)^2, trig);
1
> e := cos(x)^5 + sin(x)^4 + 2*cos(x)^2 - 2*sin(x)^2 - cos(2*x):
> simplify(e);
5 4
cos(x) + cos(x)
> f := -1/3*x^5*y + x^4*y^2 + 1/3*x*y^3 + 1:
> simplify(f, {x^3 = x*y, y^2 = x+1});
5 4 2 3
1 + y + y - y + y - 2 y
> g:=sqrt(x^2);
2 1/2
g := (x )
> simplify(g);
csgn(x) x
> simplify(g,assume=real);
signum(x) x
> simplify(g,assume=positive);
x
SEE ALSO: collect, combine, convert, expand, factor, normal, radsimp, assume,
simplify[<name>] where <name> is one of: Ei, GAMMA, RootOf, atsign,
hypergeom, ln, polar, power, radical, siderel, sqrt, trig
.Simplification is a tricky subject. The "simplest" form often depends upon context. For example, sometimes you might want a polynomial to be expanded; other times you would prefer it to be factored. The "See Also" section indicates alternatives.
.You can go straight to the examples by entering ???simplify .
THE MAIN EXERCISE
Maple's help facility is a wonderful learning aid! If you highlight an example, then copy and paste it into your worksheet, you can see what it does.
.Look at the Help Browser. Do a Keyword Search on "series."
.Try the following commands; copy examples into your worksheet.
?help ?int
?solve ?fsolve
?E ?inifcns
?interface ?packages
PLOTTING
Maple can produce graphs very easily. We won't include the output here to save paper. Examples of plotting commands include the following:
> plot( x^2, x=-2 .. 2 );
> plot(exp(x), x=-10..10, y=-10..10);
> plot({sin(x), x, x-x^3/6}, x=-2..2);
> plot( [ t*cos(t), t*sin(t), t = 0 .. 2*Pi ] );
> plot3d( 4 - x^2 - y^2, x=-3..3, y=-2..2 );
PLOTTING EXERCISE:
.Enter the examples above.
.Enter the following commands. Each gives examples; copy some of them into your worksheet (and modify them, if you like).
?plot ?plot3d
?plot[options] ?plotdevice
?showtangent ?plots
FUNCTIONS
A mathematical "function" is a mapping from one set of numbers to another. To define a function you must specify the rule for assigning one number to another. Consider the following sets of commands:
> f := x^2;
> f := x -> x^2;
The first line defines f to be (an abbreviation for) an expression; the second line defines f to be a function. After f is defined this way, f(x) works as it should:
> f(3), f(10), f(x), f(y), f(a + b);
Functions are often more useful than expressions. However, many Maple commands (for example, diff) expect an expression as input. If f := x -> x^2, diff(f,x); doesn't work; however, diff(f(x),x); will work, since f(x) produces x^2 as output; diff(f(y),y) works, also (it returns 2y).
EXERCISE: Experiment with the "arrow notation" for creating functions.
PROCEDURES
Functions are a special case of procedures. A procedure is a collection of commands, like a subprogram or subroutine in many other computer languages. The squaring function defined above is the same as
> f := proc(x) x^2 end;
Procedures are useful for more complicated activities, which may stretch over many lines, consider many cases, or carry out many activities. Here's another short example of a procedure:
> sayhello := proc() print(` Hello world!:^) `); end;
This can be understood as shorthand for: "sayhello" is the name of a procedure (which needs no input), that prints something, and then ends. After defining this procedure (in a given Maple session), it can be invoked just like the built-in procedures:
> sayhello();
Here's a more realistic example:
sqroot := proc(x)
# Compute square root by Newton's method
local xstart, xnew, i;
xstart := 1.;
for i from 1 to 100 do
xnew := ( xstart + x/xstart )/2;
print(xnew);
if abs(xnew - xstart)/xnew < 10^(-4) then RETURN() fi;
xstart := xnew;
od;
end:
After creating this procedure, it produces:
> sqroot(2);
Here's a more complicated example, which computes a sequence of polynomial approximations to a scalar function (at x = 0), and plots the result:
tayplot := proc(n)
local polys, k;
if not type(n, integer) or (n < 2) then
ERROR(`n must be an integer greater than 1`);
fi:
polys := NULL:
for k from 2 to 1 + n do
polys := polys, convert(taylor(f(x), x=0, k), polynom):
od:
plot( {f(x)} union {polys}, x=-10..10, y=-10..10 ):
end:
This was not created from scratch! It started with the Taylor series command; series cannot be plotted, so that was converted to a polynomial; and rather than do that repeatedly, a sequence of Taylor polynomials was formed. Then it was turned into a procedure, saved as a file for easy reference, and error-trapping added.
Before you can use it, you must define a function of one variable called f, for example, by entering f := cos; or f:= x -> 1/(1+x^2);. Enter tayplot(10), to try it (if the number is too large, it will take a long time).
BATCH PROCESSING
It is often convenient to make a file of Maple commands, and then read it into Maple. This may be done with any editor (for example TeachText or Write). Save the file as plain text. To use it from within a Maple session:
.For PCs and UNIX workstations, enter (note that both use /, not \):
> read `c:/dev/sqroot.txt`;
Under the File menu, see Open (for PCs) or Include (for UNIX workstations), for a "mouse-driven" alternative.
.For Macintosh, the corresponding notation is:
> read `MACINTOSH:Statistics and Mathematics:MapleV R3:sqroot`;
Under the Edit menu, see Paste Pathname, to avoid typing.
Maple may also be used in batch mode at the operating system level. For more information on Maple and batch processing, see the document "Getting Started with Maple and ULTRIX" (available from UCS).
THE LAST EXERCISE!
.Use a text editor to create a file named sqroot, containing the procedure shown above for computing the square root by Newton's method. Then use read to load it into a Maple session. Compare with sqrt(2).
.Write a procedure "bigger", so that bigger(n,m); prints true if n is bigger than m, and false if n is less than m.
.Write a procedure "squaresum", which calculates the sum of the first 100 squares of integers (i.e., 1 + 4 + 9 + ... + 10000). Compare your squaresum(100) to sum(n^2, n=1 .. 100). Modify your procedure to calculate the sum of the first N squares, where N is any integer.
GETTING HELP
If you want a more extended introduction to Maple, invoke a tutorial by entering tutorial(); at the Maple prompt.
Computing information is available 24 hours a day, electronically, from the UCS and Stat/Math Gopher and WWW servers. You can also search the Knowledge Base for Frequently Asked Questions, or obtain copies of our documents.
There are two other places to get help for Maple. The UCS Support Center is the "first line" for all computing questions; call 855-6789, or send e-mail to ucshelp. You may also get help from the Center for Statistical and Mathematical Computing by sending e-mail to statmath or by phoning 855-4724. Both are open for consulting 9 - 5, Monday through Friday.
REFERENCES
The following books are particularly helpful:
Andre Heck, Introduction to Maple . Springer-Verlag: New York, 1993.
Bruce W. Char et. al., First Leaves: A Tutorial Introduction to Maple . Springer- Verlag: New York, 1992.
Wade Ellis Jr. et. al., Maple V Flight Manual . Brooks-Cole: Pacific Grove, CA, 1992. Tutorials for calculus, linear algebra, and differential equations. Included with the student edition of Maple (also marketed by Brooks-Cole).
Manuals are kept in the public computing facilities, in the document rack, and on three-day reserve at the Swain Hall and Business/SPEA libraries. There are also a great number of manuals at the Stat/Math Center (which you can look at, but not borrow.