1 Introduction To Maple Fall 2003: Getting Started
1 Introduction To Maple Fall 2003: Getting Started
In this course we will use only a tiny fraction of Maple's capability, but you will get a
good taste of the great power that Maple offers. This introduction provides brief samples
of these capabilities. More details about specific applications will be given in
supplementary notes as they are needed.
In the problem assignments and solutions, and in all the supplementary notes (including
these), Maple input statements will always be written in sans serif type, such as “Arial”
or “Univers,” like this. I’ll use a similar type face for the projected class demonstrations
of Maple. (In most of the cluster computers, the default type face for Maple input is
Courier.)
Getting Started
Carnegie Mellon has a campus-wide site license for Maple, and it is installed on all the
cluster computers. The current version of Maple is Maple 8; all the clusters have this
version. You can buy your own copy at the Computer Store for about $130. It's very
handy to have Maple on your own computer. If you own or have access to an older
version, Maple 4.0 or any later version is adequate for this course.
To start Maple on a UNIX machine: At the command prompt %, type "xmaple&" and
Maple will open and run in a new window. If you have problems, ask a CCon.
When you are learning Maple, I recommend setting the "Input Display" under the
"Options" menu to "Maple Notation," and setting the "Output Display" to "Typeset
Notation." On most machines these will be the default settings, and you won't need to
change anything.
1-2 1 Introduction to Maple
Maple is interactive; you type in a Maple statement, then type <enter>; Maple
executes the statement and displays the result immediately. We'll often refer to the
Maple statement as "input" and the result as "output." When Maple is waiting for input,
the prompt ">" appears at the left margin. The default settings are to display the input in
red, usually in "Courier" or "Arial" typeface, and the output in blue in Times Roman, in
usual mathematical notation.
Every Maple input statement or command must end with a semicolon or a colon. If you
end a statement with a semicolon and then type <enter> the result is displayed
immediately. If you end the statement with a colon, the statement is still executed, but
the result isn't displayed. This is sometimes handy if the output has many lines that you
don't need to see.
If there is room, you can put two or more input statements on the same line, provided
each statement ends with a semicolon or colon. When you type <enter>, all the
statements on the line are entered and executed. But it is usually better, for the sake of
clarity, to put each statement on a separate line. Occasionally you will want to enter
several lines but delay execution until you've entered them all. In that case, type
<Shift> + <enter> for all but the final line. Such a group of commands is called an
execution group, and it is denoted by a bracket in the left margin.
If you forget and enter a command without a semicolon, Maple will give you an error
message; you can then move the cursor back to the end of the input line, add the
semicolon, and type <enter> again. Alternatively, you can enter the semicolon alone on
the next line (; <enter>). Exception: No semicolon is needed for the “help” command.
To get help on a topic, type ?topic <enter>, no semicolon needed.
Maple generally ignores spaces in algebraic expressions, so feel free to add spaces to
improve legibility. x + 3 is usually more readable than x+3.
Maple is case sensitive; you can’t interchange capital and lowercase letters. Example:
the cosine of x is cos(x), not COS(x). Most functions are lower case; an exception is
the gamma function, usually written as Γ(x). Maple's notation is GAMMA(x). The
constant π MUST be spelled Pi, and the imaginary unit (the square root of −1) is I ,
not i. The base of natural logs is exp(1), not e or E.
1 Introduction to Maple 1-3
Here are Maple notations for a few common functions, to illustrate the general pattern.
sine of x: sin(x) (x in radians, not degrees)
inverse sine of x: arcsin(x)
tangent of x: tan(x)
square root of x: sqrt(x)
exponential of x: exp(x)
natural logarithm of x: ln(x) or log(x)
The various delimiters such as ( ), [ ], and { } are not interchangeable in Maple.
Usually functions and algebraic expressions require ( ) parentheses. [ ] denotes a list or
a sequence (with a definite order); { } denotes a set. These can be lists or sets of
constants, expressions, functions, equations, plot structures, matrices, or other entities.
A range of values of a variable is denoted with two dots. If x ranges from a to b, then
type x = a..b, not x = a, b
When you are editing Maple code, you may sometimes want to insert a line of Maple
input between two existing lines. To insert a blank line after the line the cursor is on,
type <ctrl> + j; to insert a blank line before the cursor, type <ctrl> + k.
Often you'll want to clear Maple's memory of all previous input and output. This is done
easily with the command restart; <enter> You will use this command frequently; get
in the habit of using it first, before you begin a new calculation.
In the following sections, don't just read through the material. Sit down at a computer,
log on, get into Maple, try all the input statements, and note carefully what happens.
While you are learning Maple, make up your own manual. Write down things you have
trouble remembering or that cause confusion. And don't be afraid to ask questions; don't
get hung up on trivial but basic things.
Arithmetic
Let's begin by using Maple as a simple calculator. Maple can do both integer and
floating-point arithmetic: If the input numbers are all integers or rational fractions, the
output will be also. Try these examples:
restart;
3 + 4;
3/4 + 2/7;
2^5;
2^100;
(2/5)^15;
1-4 1 Introduction to Maple
Now try entering some of these without the semicolon at the end; you will get an error
message because Maple is waiting for you to end the statement. You can either type
;<enter> alone on the next line, or you can move the cursor back to where the missing
semicolon belongs, put it in, and type <enter>.
Thus there are two ways to force floating-point evaluation: using floating-point numbers
or using the evalf command. Compare the following:
(3/2)^4;
(1.5)^4;
evalf((3/2)^4);
In floating-point calculations, the default number of digits is 10. If you want a different
number, you can specify it in the evalf command by adding a comma followed by the
number of digits you want (inside the final parenthesis):
evalf((2/5)^15, 5);
Pi;
evalf(Pi, 100);
There's an alternative: Enter the command Digits := 20; (Note the capital D.) Then all
succeeding floating-point calculations will be done and displayed with 20-digit precision
until you enter restart; or another Digits command.
1 Introduction to Maple 1-5
Assignment Statements
Maple remembers all such assignments indefinitely. If you want to erase the record and
start from the beginning, use the command restart; Or if you want to unassign an
individual variable x, type x := 'x';
A variable name can have any combination of letters (capital or lower-case) and numbers,
but it must start with a letter. Punctuation marks aren't allowed, but underscores are OK.
Some combinations of letters, such as cos, exp, and diff, have special meanings as part
of Maple commands. These names are protected and thus aren't available for general use.
If you try to assign to one of these, Maple gives you an error message. Usually it's a
good idea to choose variable names that suggest the physical significance of the
quantities they represent.
The percent sign % is used to refer to the previous result. For example, f := cos(%);
means that we give the name f to the cosine of whatever appeared as output on the
preceding line. %% refers to the second-to-last line. And so on, up to %%%.
However, I strongly recommend that when you’re first learning Maple, you avoid the use
of the % completely and instead give everything a name if there's any chance you might
want to refer to it later.
In Maple, expressions, functions, and equations are three distinct kinds of entities; the
distinction will emerge as you use Maple. For example, f := a*x^2 + b*x + c; defines an
expression called f; g := x −> a*x^2 + b*x + c; defines a function called g;
h := a*x^2 + b*x + c = 0; defines an equation called h. The arrow (made with a
hyphen and a “greater than”) shows explicitly that a function is a mapping, not simply an
expression containing the particular variable x. We'll discuss this distinction further on
pages 1-8 and 2-1.
1-6 1 Introduction to Maple
Algebra
Three other commands that are useful for algebraic manipulation are simplify, collect,
and isolate. For explanation of these commands, see the Maple Help files.
Sometimes the roots of an equation can't be expressed in terms of familiar functions but
have to be obtained by numerical approximation. An example is tan(x) = x + 1. The
Maple commands eq := tan(x) = x + 1; and solve(eq, x); just give you back a
restatement of the problem. Instead, use the command fsolve(eq, x); or fsolve(tan(x)
= x + 1, x); The command fsolve tells Maple to obtain a floating-point numerical
approximation of the root. In this particular case, the equation has infinitely many roots;
to find a root in a particular range, say x = 0 to 2, use the command fsolve(tan(x) = x +
1, x = 0..2);
1 Introduction to Maple 1-7
Maple can solve sets of simultaneous equations. Consider the simple pair of equations
x + y = 5, x − 2 y = −6. We give each equation a name:
eq1 := x + y = 5; eq2 := x - 2*y = −6;
Maple requires a set to be enclosed in curly brackets. Here the pair of equations form one
set, and the unknowns x and y form another. Thus the Maple command is:
solve({eq1, eq2}, {x, y});
There are many other useful commands that do algebraic manipulations. A few are listed
in the summary of Maple commands, starting on page 1-9.
Calculus
Maple can do symbolic derivatives and integrals. Try these: For an expression
expr := x^6 − a*x^2; the derivative with respect to x is given by
diff(expr, x); or diff(x^6 − a*x^2, x);
Note that you have to tell Maple what to differentiate (in this case, expr) and the
variable (in this case, x) with respect to which the derivative is to be calculated.
(Otherwise Maple doesn't know whether the variable is a or x.) Try omitting the x in
the above command diff(expr,x), and see what happens.
For higher derivatives, use a dollar sign. For the fourth derivative of expr with respect
to x,
diff(expr, x$4); or diff( x^6 − a*x^2, x$4);
For the derivative of a function, the command is different. If fcn := x −> x^6 − a*x^2,
Plots
Suppose we want to plot a graph of the expression sin x from x = 0 to 2π. Easy!
plot(sin(x), x = 0..2*Pi);
Note the spelling of Pi and the parentheses in the sine function. Also note that you have
to specify the range of values of the independent variable x. Try omitting this range, and
see the rather mysterious error message Maple gives you. An alternative method is:
expr := sin(x);
plot(expr, x = 0..2*Pi);
To plot a function (as distinguished from an expression), the commands are similar but
don't include a variable name. For example, if fcn := x -> sin(x), the correct plot
command is
plot(fcn, 0..2); or plot(sin, 0..2);
As another example, suppose we want to plot x 2 + 3x. Then
expr := x^2 + 3*x; fcn := x -> x^2 + 3*x;
The following plot commands are OK:
plot(expr, x = 0..2); plot(x^2 + 3*x, x = 0..2);
plot(fcn, 0..2); plot(x -> x^2 + 3*x, 0..2); plot(fcn(x), x = 0..2);
But these don't work:
plot(expr, 0..2); plot(fcn, x = 0..2);
Be sure you understand crucial distinction between functions and expressions. Also see
the discussion on page 2-1.
Inspirational Message
Like all computer languages, Maple is fussy about details of syntax. If you don't speak
Maple's language, it can't help you. Unmatched or omitted delimiters ( ), [ ], or { }, are
a very common error. Note the comment at the bottom of page 1-7 concerning finding
unmatched delimiters. If you try to get Maple to do something, and it won't do what you
want because of some trivial but crucial syntax error that you can't find, don't sit and spin
your wheels for hours -- ask somebody for help! The more you learn about Maple, the
more you will appreciate its enormous power and versatility. You may feel like you're
driving a Ferrari; I hope so.
During this course, Maple will save us from enormous amounts of dogwork, and it will
enable us to solve a lot of problems (such as numerical solutions of nonlinear differential
equations) that would be hopeless without it. Don't ignore it in the hope that it will go
away; it won't. And it will become one of your best friends if you let it!
1-10 1 Introduction to Maple
The following three pages list several Maple commands that we'll find useful during the
course. Don't sit down and memorize them all at once; learn them a few at a time as we
come to applications during the course. And remember that you can always get help for
any command from Maple's excellent help files. To get help for the command fiddle,
simply type ?fiddle (no semicolon needed).
Algebra
Series
series(expr, x = 3, n);
taylor(expr, x = 3, n);
convert(series, polynom);
Miscellaneous
restart;
list: L := [a, b, c]; L[1] = a set: S := {a, b, c}; S[2] = b
Pi exp(1) I
1 Introduction to Maple 1-11
Calculus
Plots
Animate
Matrix Algebra
with(linalg);
A := matrix(3, 3, [1, 2, 3, 3, 4, 5, 5, 6, 9]);
A + B; A − B; A &* B; multiply(A, B);
evalm(matrixexpr); inverse(A); det(A);
transpose(A); htranspose(A);
eigenvalues(A); eigenvectors(A); normalize(vectorlist);