A Little Bit of Help With Maple
A Little Bit of Help With Maple
A Little Bit of Help With Maple
Welcome!
The aim of this manual is to provide you with a little bit of help with Maple. Inside you will
find brief descriptions on a lot of useful commands and packages that are commonly needed
when using Maple. Full worked examples are presented to show you how to use the Maple
commands, with different options given, where the aim is to teach you Maple by example –
not by showing the full technical detail. If you want the full detail, you are encouraged to
look at the Maple Help menu.
To use this manual, a basic understanding of mathematics and how to use a computer is
assumed. While this manual is based on Version 10.01 of Maple on the PC platform on Win-
dows XP, most of it should be applicable to other versions, platforms and operating systems.
This handbook is built upon the original version of this handbook by Maureen Edwards and
Alex Antic. Further information has been gained from the Help menu in Maple itself.
If you have any suggestions or comments, please email them to Grant Cox (gcox@uow.edu.au).
1 Introduction 4
1.1 What is Maple? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3.1 The Ditto Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 Assign and unassign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5 Special constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.6 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.6.1 Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.6.2 The absolute value and the sign of a number . . . . . . . . . . . . . . . 13
1.7 Automatic simplifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.8 Sets and lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.9 Names, symbols and strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3 Calculus 52
3.1 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.2 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4 Solving equations 57
4.1 Algebraic equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.2 Differential equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.3 Recurrence equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2
A little bit of help with Maple
6 Programming in Maple 73
6.1 Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.2 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.3 If statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.4 For statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Introduction
In this chapter, we will look at what Maple is, and how to start using it to solve many
problems in mathematics.
While this handbook aims to provide you with everything you need to learn the basics of
Maple, further information about Maple can be found either in numerous books in the library
(library.uow.edu.au) or at Maple’s web site: www.maplesoft.com
Note:
Always save your Maple worksheet as a classic Maple worksheet - it should have the file type
extension ‘.mws’, while the visually enhanced worksheet has the file type extension ‘.ms’.
4
A little bit of help with Maple
When you type Maple commands into the classic worksheet, the color of the text should be
red. Thus, all Maple commands and inputs in this handbook will also be in red text. Maple
commands must be typed after the prompt: [ >
Almost every statement entered into Maple must end with either a semicolon (i.e., ;) or a
colon (i.e., :) . If a semicolon is at the end of a statement, then the output will be displayed
to the screen, while if a colon is used, then the output is suppressed - although the statement
is still executed. To execute a command, press the Enter or Return key, while the blinking
cursor is anywhere in the statement. The cursor does not need to be at the end of the state-
ment in order to execute it. Once a command is executed, a new prompt is displayed, ready
for your next command.
Example 1.1:
For example, if a semicolon, ;, is used, then the result is printed to the screen:
> 2;
2
However, if a colon, :, is used, then the statement is executed, but the result is
not printed to the screen:
> 2:
>
Note:
If you have executed a statement, which has a semicolon at the end of the statement, but no
output is given by Maple, then either:
• your statement has no output - indeed some Maple commands have no output, or
Alternatively, you can execute a command by moving the blinking cursor to the appropriate
command line and pressing the icon with a single exclamation mark, i.e., ! . Further, you
can execute the entire worksheet by pressing the icon with three exclamation marks, i.e., !!! .
If you want a new prompt above where the cursor is currently located, press Ctrl k, while
if you want a new prompt below, press Ctrl j. In regards to the prompt, the [ shows the
size of the execution group. If you have a multi-line input statement, then the [ will grow
to encompass the entire statement. You can also add extra lines to an execution group, i.e,
input statement, by pressing Shift Enter. All statements in the same execution group are
executed at the same time - but in order of appearance.
When typing commands, please keep in mind that Maple is case sensitive. For example, pi
and Pi are different commands.
mathematical relational
+ addition < less than
− subtraction > greater than
∗ multiplication <= less than or equal to
/ division >= greater than or equal to
ˆ or ∗∗ exponentiation <> not equal
= equal = equal
logical set
and intersect
or union
xor exclusive or minus
implies subset
not member
Table 1.1: The basic operators in Maple.
Finally, Maple has an in-built help support. You can either go to the Help menu and search
for a desired topic or command, or if you already know the command, i.e., command name,
then you can instead type ? command name to bring up the relevant help page. Notice
that this is one of the very few situations where you do not need to have a colon or semicolon
at the end of the statement. These help pages provide a detailed explanation on the com-
mands and available options, usually with some basic examples.
1.3 Operators
The basic operators in Maple, including mathematical, relational, logical and set, are listed
in Table 1.1. For more information about these operators, type ? operators.
Note:
When typing decimal numbers you need to be a little bit careful with blank spaces, because
. is an operator in Maple that performs non-commutative, or dot product, multiplication on
its arguments. Some examples of using the operator . in Maple are given below:
Example 1.2:
> 2 . 5;
10
> 2 .5;
10
> 2. 5;
Error, unexpected number
> 2.5;
2.5
We will see examples of how to use the relational, logical and set operators later. However,
some examples of how to use the mathematical operators are presented in the following ex-
ample:
Example 1.3:
> 2 + 3;
5
> 2 − 3;
−1
> 2 * 3;
6
"
> 2 / 3;
2
3
> 2 ˆ 12;
4096
The blank spaces between the numbers and the operators in the above example can be ne-
glected. For example, we could instead have written:
Example 1.4:
> 2+3;
5
> 2 +3;
5
> 2+ 3;
5
> 2+3;
5
> 2+ 3 ;
5
> 2−3;
−1
> 2*3;
6
"
> 2/3;
2
3
> 2ˆ12;
4096
In other words, Maple ignores spaces, in general. There are a small number of exceptions,
where one is demonstrated in Example 1.2.
Note:
The standard order of operations holds true in Maple.
Example 1.5:
> 2 + 6/2;
5
> (2 + 6)/2;
4
Example 1.6:
> 3;
3
> x;
x
> hello;
hello
> %;
hello
> %%%;
x
Note:
The ditto operator, %, refers to the most recent statement executed – this need not be located
directly above.
Example 1.7:
To assign the value of 2 to the variable x, use
> x := 2;
x := 2
Thus, if x is used elsewhere, Maple knows its value to be 2, i.e.,
> y := 3*x;
y := 6
To unassign a value to a previously assigned variable, use the unassign command. For ex-
ample:
Example 1.8:
Assuming x is defined as above in Example 1.7, then to unassign x, use
> x;
2
> unassign(’x’):
> x;
x
Note:
The argument of the unassign command must have the single quotes, ’ ’ around the vari-
able to be unassigned. The single quotes have the effect of delaying the evaluation of the
variable, so that the command unassign(’x’) unassign’s the variable, x, and not the value
of the variable.
To clear all stored information in a Maple worksheet, use the command restart.
Example 1.9:
> x := -2;
x := −2
> x;
−2
> restart:
> x;
x
Note:
pi is also used in Maple, but it is not a protected word. Instead, it is only used as a symbol
to represent the constant value of π. Similarly, the characters e an E are not protected in
Maple. To use the value of the exponential, e, consider the following example:
Example 1.10:
> x := exp(1);
x := e
> ln(x);
1
where exp is the exponential function and ln is the logarithmic function. See
? exp and ? ln for more information.
Note:
Any symbol or variable name beginning with an underscore, i.e., , is reserved for use by
Maple library code and as such should not be used by you for a variable name. Also, vari-
ables names should not begin or end with a tilde, i.e., ∼.
1.6 Evaluation
When using special constants, such as Pi, Maple will by default represent them by their
symbol, π, and not by their numerical value, 3.141592654. To obtain the value of a constant,
you need to ask Maple to evaluate it using the function evalf, which converts an expression
from it’s symbolic form into it’s numeric floating-point form.
Example 1.11:
> Pi;
π
> evalf(Pi);
3.141592654
As pi is used to denote the symbol of the constant π, then you cannot obtain
the numerical value of π via the evalf function. If you do try, then you will
only obtain the symbol back, i.e.,
> evalf(pi);
π
Maple represents fractions in rational form. To obtain the numerical value of a fraction, use
the evalf function. For example:
Example 1.12:
"
> 10/7;
10
7
> evalf(10/7);
1.428571429
Maple automatically converts an expression to it’s numerical decimal value if
the expression contains a decimal number, i.e.,
> 10./7;
1.428571429
> 10/7.0;
1.428571429
> 10.0/7.;
1.428571429
where Maple treats 10. to be the same as 10.0.
Further, evalf can be used to also find the decimal value of an irrational number, i.e., see Pi
above.
Maple has other evaluation functions. The three most common other evaluation functions
are:
• evalb: determines the boolean value of an expression, i.e., true or false. See ? evalb for
more information.
• evalc: determines the complex value of an expression. See ? evalc for more information.
Example 1.13:
> eval(2x+1,x=2);
5
> evalb(2>3);
f alse
> evalc(sqrt(1 + I));
√ √
q q
1 1
2 + 2 2 + I −2 + 2 2
2 2
where sqrt is the square root function.
Note:
The function eval also has other uses, which we will see later!
1.6.1 Digits
In floating-point calculations, the default number of digits that Maple returns is 10. To change
this value to some number n, where n is a natural number from 1 to kernelopts(maxdigits),
we need to change the predefined Maple variable Digits. For example:
Example 1.14:
To check the number of digits being used, simply check the value of Digits,
i.e.,
> Digits;
10
To alter the number of digits to, say 5, use
> Digits := 5;
Digits := 5
Thus, if now we evaluate Pi using evalf, we find
> evalf(Pi);
3.1416
Example 1.15:
> abs(3);
3
> abs(-3);
3
> abs(cos(3));
− cos(3)
> a:=abs(2*x-3);
a := |2x − 3|
> abs(1 - 2*I); √
5
Example 1.16:
> sign(0);
1
> sign(-2/3);
−1
> sign(2 - 3*I);
1
> sign(-2 - 3*I);
−1
For more information, see ? sign. To determine the sign of a real or complex expression, use
the signum command.
Example 1.17:
> signum(0);
0
> signum(-2/3);
−1
> signum(1 - 2*I);
√
1 2
− I 5
5 5
Note that signum(x) = x/abs(x) for x 6= 0.
Example 1.18:
> x - x;
0
> x + x;
2x
> x + 0;
x
> x ∗ x;
x2
> x/x;
1
> x ∗ 1;
x
> xˆ0;
1
> xˆ1;
x
Calculating infinity/infinity, or infinity - infinity, returns the value
> infinity/infinity;
undef ined
while trying to calculate 0/0 returns the error message
> 0/0;
Error, numeric exception: division by zero
A list is an ordered sequence of expressions of the form [e1, e2, . . . ], where e1, e2, . . . are
expressions. In this case, the two lists [a,b] and [b,a] are not equal.
A set is an unorder sequence of expressions of the form {e1, e2, . . . }. In this case, the two
sets {a,b} and {b,a} are equal. When using a set to denote a sequence of expressions, do
not assume that Maple will preserve the same order.
Thus, if you want an ordered sequence of expressions, then you should use a list, with the
square brackets [ ]. Otherwise, you can use a set, with curly brackets { }.
Two very useful commands that are often used in conjunction with sets and lists, are the nops
and op commands. The nops command returns the number of operands in an expression.
For example:
Example 1.19:
> L := [1,2,3,4];
L := [1, 2, 3, 4]
> S := {5,6,7};
S := {5, 6, 7}
> nops(L);
4
> nops(S);
3
The nops command can be used with more than just sets and lists. For
example, it can be used to tell you how many terms in an equation, i.e.,
> eqn := 1 + x + y + x*y - yˆ2;
eqn := 1 + x + y + xy − y 2
> nops(eqn);
5
Example 1.20:
> S := {5,6,7};
S := {5, 6, 7}
> op(S);
5, 6, 7
The op command can also be used to select individual particular operands, i.e.,
> op(2,S);
6
and similarly for other expressions, i.e.,
> eqn := 1 + x + y + x*y - yˆ2;
eqn := 1 + x + y + xy − y 2
> op(5,eqn);
−y 2
For more help on nops and op, see ? nops and ? op. There are many operations that you
can do on both lists and sets. Some examples are given below:
Example 1.21:
Sets remove duplication, while lists don’t, i.e.,
> {a,a,b};
{a, b}
> [a,a,b];
[a, a, b]
You can convert from a list to a set, and vice-versa
> convert([a,b],set);
{a, b}
> convert({a,b},list);
[a, b]
For more information on convert, see ? convert. Maple may change the order
of elements in a set, i.e.,
> {b,a,c};
{a, b, c}
To select elements from a list or set, use [#], i.e.,
> L := [1,2,3,4];
L := [1, 2, 3, 4]
> S := {5,6,7};
S := {5, 6, 7}
> L[1];
1
> S[2];
6
> L[2..4];
[2, 3, 4]
If you want to add a new element to a list, use
> L1 := [op(L),12];
L1 := [1, 2, 3, 4, 12]
Similarly for sets, or use union. To remove an element from a set, use
> S minus 6;
{5, 7}
To remove an element from a list, it is slightly harder to do. Instead of minus,
use
> remove(has,L1,3);
[1, 2, 4, 12]
The command remove, and its counterpart select, are very useful commands
that can be used in many situations. See later for more details.
Another common command used in conjunction with sets and lists is the seq command. For
example:
Example 1.22:
If
> L1 := [1,2,3];
L1 := [1, 2, 3]
> L2 := [4,5,6];
L2 := [4, 5, 6]
then to multiply the corresponding elements of L1 and L2 together to form a
new list L3, we can use
> L3 := [seq(L1[i]*L2[i],i=1..nops(L1))];
L3 := [4, 10, 18]
For more help on seq, see ? seq. An alternate way to calculate the product of elements from
two lists, is to use the zip command, i.e.,
Example 1.23:
If
> L1 := [1,2,3];
L1 := [1, 2, 3]
> L2 := [4,5,6];
L2 := [4, 5, 6]
then
> zip((x,y)-> x*y,L1,L2);
[4, 10, 18]
Note that zip cannot be used with sets.
• Any valid Maple name formed without using left single quotes is precisely the same as the
name formed by surrounding the name with left single quotes. Therefore, x and ‘x‘ both
refer to the same name x.
• Names can be formed with the concatenation operator || or with the cat command (see
example below). For more information see ? || and ? cat.
• Names should not start with an underscore, , or a tilde, ∼, and should not contain a slash,
/.
Example 1.24:
> This is a valid name := 2;
T his is a valid name := 2
> ‘I like this number!‘ := 42;
I like this name! := 42
> ‘I like this number!‘;
42
You can join names together using || to form new names, i.e.,
> i := 5;
i := 5
> p || i;
p5
> p || 2*i;
p10
"
If the right-hand expression is a sequence or a range and the operands of the
range are integers or character strings, then Maple returns a sequence of
names.
> a || (a, b, 4, 67);
aa, ab, a4, a67
> ”foo” || (1..3);
”f oo1”, ”f oo2”, ”f oo3”
You can include ‘ in names by using two of them.
> ‘Trees aren“t pink!‘;
T rees aren0 t pink!
There are two types of names: indexed names and symbols (non-indexed names). All of the
above names are symbols because they are not of the form: name[expression]. An indexed
name may appear almost anywhere a name can appear.
Example 1.25:
The following are examples of symbol names.
> a := 2;
a := 2
> distance := 3.5;
distance := 3.5
The following are examples of indexed names.
> a[1] := 2;
a[1] := 2
> a[2] := 3;
a[2] := 3
> a[3] := 5;
a[3] := 5
> distance[0] := 2.7;
distance[0] := 2.7
> distance[1] := 3.5;
distance[1] := 3.5
> A[1,2,3][x,y][2,1] := 0;
A[1, 2, 3][x, y][2, 1] := 0
It is strongly recommended that you don’t use the same name for indexed and symbol names
– otherwise things may go wrong!
Example 1.26:
> a[1] := 2;
a[1] := 2
> a[1];
2
> a;
a
> a := 3;
a := 3
> a[1];
31
> b := 2;
b := 2
> b[1] := 3;
b[1] := 3
> b;
b
A string is a sequence of characters that has no value other than itself. It cannot be assigned
to, and will always evaluate to itself.
A string is written by enclosing any sequence of characters within a pair of double quote
characters, i.e., ”This is a string”. Strings may also be formed with the concatenation
operator ||, or with the cat command.
Example 1.27:
> ”The sky is blue”;
”The sky is blue”
> ”The sea is green”;
”The sea is green”
If you try to assign to a string, the following error occurs:
> ”A dog has how many legs?” := 4;
Error, invalid left hand side of assignment
Using the || operator, we find
> ”This” || ” ” || ”and” || ” ” || ”that”;
”This and that”
Strings are very useful when you want to write messages to the screen. In particular, as we
will see later, when writing procedures it can be useful to output error messages to the screen
using strings.
There are a number of very useful commands that can be used in conjunction with names,
symbols and strings, including:
• convert: Can be used to convert from one Maple expression into another. This is one of
the most useful and powerful Maple commands to know! Here, in particular, it can be used
to convert a name into a string, and vice-versa. See ? convert for more information.
• cat: Can be used to combine strings and names together. This is useful when you want to
write informed messages to the screen. See ? cat for more information.
• parse: Can be used to evaluate a Maple expression contained within a string (see example
below). Note that the string must consist of exactly one Maple expression. By default,
the expression is parsed, and returned unevaluated. If the option ’statement’ is specified,
the string is parsed and evaluated, and the result is returned. See ? parse for more
information.
• assign: Can be used to assign one value to another. See ? assign for more information.
• type: Can be used to check they type of a Maple expression. This is particularly useful
when you have assigned a value to a name and you want to check that the type is correct.
See ? type for more information.
• whattype: Can be used to see what Maple type an expression is. If you do not know
what type something this, this command comes in handy. See ? whattype for more
information.
Example 1.28:
> convert(a,string);
”a”
> convert(‘I want to be a string¡,string);
”I want to be a string!”
> convert(”I don’t like being a name”,name);
I don0 t like being a name
To assign the value 6 to the name converted from the string ”2 times 3
equals”, you need to use the assign command:
> assign(convert(”2 times 3 equals”,name),6);
> ”2 times 3 equals”;
”2 times 3 equals”
> ‘2 times 3 equals‘;
6
Note that the assign command above produces no output. The parse
command can be used to evaluate the expression within a string.
> a := 3;
a := 3
> parse(”a”);
a
> parse(”a”,statement);
3
The type command can used to check the type of a Maple expression.
> type(b,name);
true
> b := 2;
b := 2
> type(b,name);
f alse
> whattype(b);
integer
In this chapter we will go through some of the basic commands that you may need to use
Maple.
Example 2.1:
> sin(Pi/2);
1
> exp(x);
ex
> exp(ln(x));
x
> sqrt(4*x); √
2 x
In Maple, the two most commonly used functions for calculating summations are sum and
add. The important difference between these two commands is that sum tries to calculate
a formula for the sum, while add calculates the sum by simply adding the finite sequence of
terms. Thus, it is strongly recommended that you use the add command if an explicit sum is
needed, particularly when summing over all elements of a list, array, matrix, or similar data
structure. See ? sum for a more detailed comparison between sum and add.
23
A little bit of help with Maple
Similarly, there are two common functions used for calculating products, namely product
and mul. The command product is used when you want to determine a formula that is
equal to the product, whereas the mul command is used when you want to calculate the
finite product of terms. Thus, mul is similar to add, in that it is strongly recommended that
you use mul if an explicit product of terms is needed.
Example 2.2:
The sum of the first 10 integers is calculated by
> add(i,i=1..10);
55
while the formula for the sum of the first n integers is given by
> sum(i,i=1..n);
(n+1)2 n 1
2
− 2
− 2
Similarly, the product of the first 10 integers is found by
> mul(i,i=1..10);
3628800
while the formula for the product of the first n integers is determined by
> product(i,i=1..n);
Γ(n + 1)
where Γ denotes the GAMMA function. See ? GAMMA for more
information.
For all of the summation and product commands, the first argument in the command denotes
the terms to be summed/multiplied, while the second argument denotes the index and the
range of the index to be summed/multiplied over. For more information, see ? sum, ? add,
? product and ? mul.
For an example of how to add a sequence of numbers from a list (or set), consider:
Example 2.3:
> L := [1,3,5,7];
L := [1, 3, 5, 7]
> add(L[i],i=1..4);
16
Note:
If you are using either sum or product to determine a formula for the summation or product,
and Maple is unable to calculate the answer, then the function call is returned.
For example:
Example 2.4:
> sum(nˆk/(n-1),n=2..k);
k
X nk
n=2
n−1
which shows that Maple is unable to calculate a closed form formula for the
answer.
Example 2.5:
> sum(1/xˆ2,x=1..infinity);
π2
6
> product(xˆn,x=1..infinity);
∞
Y
xn
x=1
If you want the sum of an expression, but you do not want Maple to calculate it, then you
can use what is called the inert form of the sum command, i.e., Sum. For example:
Example 2.6:
> S := Sum(xˆk,k=0..n);
n
X
S := xk
k=0
> X := 2 + 3S - Sˆ3; ! !3
n
X n
X
X := 2 + 3 xk − xk
k=0 k=0
If you want to determine the value of an inert Sum, use the value command,
i.e.,
> value(S);
x(n+1) 1
−
x−1 x−1
Note:
The inert form of product is Product
2.3 Limits
Maple can calculate directional and bidirectional limits. To attempt to calculate the limiting
value of an expression, say f (x), as x approaches a, i.e., lim f (x), then we use the limit
x→a
command.
The limit command has three arguments. The first argument is the expression that you want
sin(x − 2)
to take the limit of, say . The second argument defines the limit you want to take,
x−2
say as x approaches 2. And finally, the third argument, which is optional, tells Maple if you
only want a directional limit from either the left or the right, or a real or complex limit.
If no third argument is specified, then Maple assumes that you want the bidirectional real
limit.
Example 2.7:
> limit(sin(x-2)/(x-2),x=2);
1
If the bidirectional limit does not exist, then undefined is returned:
> limit(1/x,x=0);
undef ined
To calculate a directional limit, specify the direction as the third argument,
i.e.,
> limit(1/x,x=0,left);
−∞
> limit(1/x,x=0,right);
∞
You can take the limit at infinity, i.e.,
> limit(exp(-x),x=infinity);
0
The inert form of limit is Limit. If the inert form of the limit is used, then Maple will not
calculate the limit – until forced to do so using the value command.
Example 2.8:
"
> L := Limit(xˆ2,x=3);
L := lim x2
x→3
> value(L);
9
See ? limit for more information on the limit and Limit commands.
Example 2.9:
> plot(sin(x),x=0..2*Pi);
The plot command can have extra (optional) arguments. Some common options include:
• A vertical range – if this is specified then it must be the third argument. Unlike the hori-
zontal range, when specifying the vertical range, you don’t specify a variable (see example
below).
• axes: Specifies the type of axes to be drawn on the plot. axes can be set to the following:
boxed, frame, none or normal, which can be specified in either upper or lower case
letters. The default value is normal.
• color: Specifies the color of the lines. All lines in the plot will change to the color specified.
See ? plot,color for more information of available colors.
• labels: Specifies the labels of the axes. By default, Maple will put the dependant variable
(which from the specification of the horizontal range) on the horizontal axis, and leave
the vertical axis with no label. To change this, you need to specify a string each for the
horizontal and vertical axes (see example below).
• labeldirections: Specifies the directions of the axes labels. By default all labels will be
written horizontally on the graphs. To change the direction, you need to set labeldirec-
tions equal to a list of two elements, where the first corresponds to the direction of the
horizontal axis while the second element of the list corresponds to the direction of the ver-
tical axis. These elements must only be equal to either horizontal or vertical, where case
of letters does not matter (see example below).
• linestyle: Specifies the style of the lines. linestyle must be set equal to either be 1, 2, 3
or 4, or one of the following names: SOLID, DOT, DASH, or DASHDOT. The name
must be in uppercase letters.
• numpoints: Specifies the minimum number of points used to generate the plot. If the
lines on the graphs are looking jagged, you can increase the minimum number of points
that Maple uses to draw the graphs by setting numpoints to a positive integer, where the
default value is 50. By default, Maple uses adaptive techniques to draw graphs, so Maple
may use more than the specified number of points.
• thickness: Specifies the thickness of the lines. thickness must be set equal to a non-
negative integer, where by default it is equal to zero. The higher the number, the thicker
the lines.
Example 2.10:
> plot(sin(x),x=0..2*Pi,-0.8..0.8,color=green,thickness=5,
linestyle=4,labels=[”x”,”sin(x)”],
labeldirections=[horizontal,vertical]);
If we change the axes to boxed, we find:
> plot(sin(x),x=0..2*Pi,-0.8..0.8,color=green,thickness=5,
linestyle=4,labels=[”x”,”sin(x)”],
labeldirections=[horizontal,vertical],axes=boxed);
You can also use the plot command to plot a list of points.
Example 2.11:
If you want to plot the points: [0,0], [1,1], and [1,0.5], then you need to create
a list of the points, i.e.,
> L := [[0,0], [1,1], [1,0.5]];
L := [[0, 0], [1, 1], [1, 0.5]]
> plot(L,thickness=3);
To plot a closed shape, you need to add the first point to the end of the list:
> plot([op(L),L[1]],thickness=3);
To fill in the closed shape with a color, set the option filled to true:
> plot([op(L),L[1]],thickness=3,filled=true,color=blue);
To graph a parametric function, you can use the plot command as well.
Example 2.12:
If x = sin t and y = cos t, for t ∈ [−π, π], then consider:
> plot([sin(t), cos(t), t=-Pi..Pi],color=blue,thickness=4);
It is possible to plot multiple functions on the same graph using the plot command.
Example 2.13:
If you want to plot both y = sin x and y = cos x, over the same range of say,
x ∈ [0, 2π], then consider:
> plot([sin(x),cos(x)],x=0..2*Pi, thickness=[4,2], color=[blue, red],
linestyle=[1,3]);
Note that the options for each line can be defined separately.
Note:
To plot multiple functions on the same plot, but with different ranges, you need to use the
plots[display] command. This will be covered in more detail later.
To plot polar functions, you can use the plot command with the option coords=polar, and
where function is defined for r and θ parametrically.
Example 2.14:
To plot r = 3 sin 3θ, consider:
three leaf rose
> plot([3*sin(3*theta),theta,theta=0..2*Pi],coords=polar,
thickness=4);
Some other interesting polar plots are:
limacon
> plot([2-4*cos(theta),theta,theta=0..2*Pi],coords=polar,
thickness=3,color=green);
spiral of Archimedes
> plot([exp(theta/3),theta,theta=0..4*Pi],coords=polar,
thickness=3,color=blue);
See ? plot,polar for more information on polar plotting, and see ? plot,coords for more
information on plots in other coordinate systems.
Example 2.15:
> plot3d(cos(x*y), x=-Pi..Pi, y=-Pi..Pi);
Note that by default no axes are drawn in three dimensional plots! To have
them drawn, you need to use the axes option.
> plot3d(cos(x*y), x=-Pi..Pi, y=-Pi..Pi,axes=frame,labels=[x,y,z]);
Note: The options of axes in plot3d are the same as for plot.
See ? plot3d,options for more information about the possible options for plot3d.
Basically, all of the possibilities of what you can do in two dimensions with plot can be
extended to do in three dimensions with plot3d, but where you have surfaces instead of
lines. This raises the question of how do you draw three dimensional curves? This will be
answered later, when we look at the plots[spacecurve] command.
Example 2.16:
You need to specify the number of dimensions of the Array, where there can
be from 0 to 63 different dimensions. The range of each dimension must be
defined. For a 1-dimensional Array:
> A := Array(1..10);
A := [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
while for a 2-dimensional Array:
> B := Array(1..2,1..3);
0 0 0
B :=
0 0 0
For higher order dimensional Array’s, Maple returns a summary of the Array:
> C := Array(1..2,1..3,0..512);
1..2 x 1..3 x 0..512 3-D Array
Data Type: anything
C :=
Storage: rectangular
Order: Fortran order
See ? Array for more information on the Data Type, Storage, Order and
other options of an Array. You can refer to each element of an Array in the
following way:
> B[1,2] := 3;
B[1, 2] := 3
> B[2,3] := -1;
B[2, 3] := −1
> B;
0 3 0
0 0 −1
You can assign initial values to Array’s as follows:
> E := Array(1..2,1..3,2);
2 2 2
E :=
2 2 2
> F := Array(1..3,1..4,[[1,2,1,2],[3,4,3,4],[5,6,5,6]]);
1 2 1 2
F := 3 4 3 4
5 6 5 6
You can perform simple arithmetic operations on Array’s provided they the Array’s are of
same size, where the result is a new Array (of the same size) where the arithmetic operation
has been applied to the corresponding elements of the Array’s.
Example 2.17:
Assume the Array’s B, E and F are as defined in the previous example, i.e.,
> B;
0 3 0
0 0 −1
> E;
2 2 2
2 2 2
> F;
1 2 1 2
3 4 3 4
5 6 5 6
Then we can calculate:
> B - 3*E;
−6 −3 −6
−6 −6 −1
> B*E;
0 6 0
0 0 −2
> %/%%;
0 −2 0
0 0 27
Note that normal matrix multiplication does not work:
> B*F;
Error, (in rtable/Product) invalid arguments
Recall, that we also have the non-commutative multiplication operator ., i.e.,
> B . E;
0 6 0
0 0 −2
so in terms of Array’s, * and . are the same.
Array’s are useful when you want to store, manipulate and manage multi-dimensional data.
However, when you have one or two dimensional data (i.e., something which can be expressed
as a vector or a matrix), then it is better to use the Vector and Matrix data structures,
as there exists many useful commands in the LinearAlgebra package that can be used to
manipulate them.
The Vector data structure is useful when you want to perform matrix calculations on one-
dimensional array’s. Vector’s differ to Array’s in that they are only one-dimensional and
operations on Vector’s must obey the usual linear algebra rules for matrices.
Example 2.18:
The are a number of ways to define Vector’s, including:
> A := Vector(3);
0
0
0
> B := Vector(1..3,5);
5
B := 5
5
> C := Vector[row]([1,2,3]);
C := [1, 2, 3]
> E := Vector[column]([4,5,6]);
4
5
6
> F := 2*B;
10
E := 10
10
> B + F;
15
15
15
> B + C;
Error, (in rtable/Sum) invalid arguments
Referring to an element of a Vector is similar to Array’s:
> C[2];
2
To convert a row vector into a column vector, or vice-versa, use the convert
command:
> G := convert(C,Vector[column]);
1
2
3
> type(G,Vector[column]);
true
See ? Vector for some other ways to define a Vector and it’s elements. When specifying a
Vector, you can add the option of either [row] or [column], as shown in the above example.
In these cases, the Vector is outputted and treated as either a row or column vector. By
default, Maple assumes the Vector is a column vector.
You can perform operations on Vector’s in the same way as Array’s – provided they are of
the same dimension and type – except for multiplication.
Example 2.19:
> B := Vector(1..3,5);
5
B := 5
5
> C := Vector[row]([1,2,3]);
C := [1, 2, 3]
> E := Vector[column]([4,5,6]);
4
5
6
The * multiplicative operator does not work for Vector’s, i.e.,
> B * E;
Error, (in rtable/Product) invalid arguments
> E * C;
Error, (in rtable/Product) invalid arguments
However, the . non-commutative multiplicative operator works are follows:
If you multiply two Vector’s of the same orientation, then the dot product of
the two Vector’s is returned, i.e.,
> B.E;
75
> C . C;
14
If the two Vector’s are of different orientation, then the two vector’s are
multiplied according to the standard rules of matrix multiplication, i.e.,
> E .C;
4 8 12
5 10 15
6 12 18
> C. E;
32
Note that the blank spaces around the . operator above do not matter, as
Maple will ignore them when dealing with non-numbers.
To perform more complicated operations on Vector’s, you need to use commands from the
LinearAlgebra package – we will see more about this later.
Note:
An alternate way to define a Vector is to use the < > notation.
Example 2.20:
> A := <1,0,1>;
1
A := 0
1
> type(A,Vector);
true
Example 2.21:
> M := Matrix(2);
0 0
M :=
0 0
> N := Matrix(2,3);
0 0 0
N :=
0 0 0
> P := Matrix(1..2,1..3,5);
5 5 5
P :=
5 5 5
> Q := Matrix([[1,2,3],[4,5,6]]);
1 2 3
Q :=
4 5 6
> R := Matrix(2,3,symbol=m);
m1,1 m1,2 m1,3
R :=
m2,1 m2,2 m2,3
Note:
To refer to an element of a Matrix, you need to specify both the row and the column of the
element in the Matrix – even when the Matrix is one-dimensional.
Example 2.22:
> Q := Matrix([[1,2,3],[4,5,6]]);
1 2 3
Q :=
4 5 6
> Q[2,3];
6
> T := Matrix([3,5,7]);
T := [3 5 7]
> T[1,2];
5
You can perform simple arithmetic operations on Matrix’s in a similar manner to Array’s
and Vector’s.
Example 2.23:
> P := Matrix(1..2,1..3,5);
5 5 5
P :=
5 5 5
> Q := Matrix([[1,2,3],[4,5,6]]);
1 2 3
Q :=
4 5 6
> P - 2*Q;
3 1 −1
−3 −5 −7
To perform matrix multiplication, you need to use the . operator:
> S := Matrix(3,2,[[1,-1],[2,1],[3,1]]);
1 −1
S := 2 1
3 1
> Q . S;
14 4
32 7
> Q . P;
Error, (in LinearAlgebra:-MatrixMatrixMultiply) first matrix column
dimension (3) <> second matrix row dimension (2)
To perform more complicated operations on Matrix’s, you need to use commands from the
LinearAlgebra package – we will see more about this later.
Note:
An alternate way to define a Matrix is to use the << >|< >> notation, where each
< > defines a column of the Matrix.
Example 2.24:
> A := <<1,0,1> | <2,1,3>>;
1 2
A := 0 1
1 3
> type(A,Matrix);
true
The Matrix command has many useful options, see ? Matrix for more information. A
particularly useful option is the ability to define identity matrices, as given below:
Example 2.25:
> M:=Matrix(3,3,shape=identity);
1 0 0
M := 0 1 0
0 0 1
Because we have specified that M has shape=indentity, then we are unable
to change the elements of M, i.e.,
> M[1,1] := 2;
Error, invalid assignment to identity diagonal
> M[1,2] := 3;
Error, invalid assignment of non-zero to identity off-diagonal
Example 2.26:
The series command computes a truncated series expansion of an expression
about a specified point for the specified order.
> S := series(exp(x),x=0,8 );
S := 1 + x + 12 x2 + 61 x3 + 24
1 4
x + 1201
x5 + 7201 1
x6 + 5040 x7 + O(x8 )
> series(cos(x),x=1,3);
cos(1) − sin(1)(x − 1) − 21 cos(1)(x − 1)2 + O((x − 1)3 )
The O() term denotes the order of the series expansion. To remove the order
term, say in order to plot the expansion, use the convert command, i.e.,
> T := convert(S,polynom);
1 + x + 12 x2 + 16 x3 + 241 4
x + 120 1
x5 + 7201 1
x6 + 5040 x7
Note that if the series is expanded about infinity, then an asymptotic
expansion is generated.
> series(xˆ3/(xˆ4+4*x-5),x=infinity);
1 4 5 1
x
− x 4 + x 5 + O x 7
The taylor command computes the Taylor series of an expression, and is a
restriction of the more general series command
> taylor(cos(x),x=1,3);
cos(1) − sin(1)(x − 1) − 21 cos(1)(x − 1)2 + O((x − 1)3 )
> taylor(sin(x),x);
1
x − 16 x3 + 120 x5 + O(x6 )
Example 2.27:
Expressions can be factorized using the factor command.
> factor(6*xˆ2+18*x-24);
6(x + 4)(x − 1)
> factor((xˆ3-yˆ3)/(xˆ4-yˆ4));
x2 + xy + y 2
(y + x)(x2 + y 2 )
You can factorize an equation in the same manner:
> eqn := yˆ2 - 1 = xˆ2 + 4*x + 4;
y 2 − 1 = x2 + 4x + 4
> factor(eqn);
(y − 1)(y + 1) = (x + 2)2
Alternatively, you can factorize the left hand or the right hand side of an
equation separately, by using the lhs and rhs commands.
> lhs(eqn);
y2 − 1
> rhs(eqn);
x2 + 2x + 4
> factor(lhs(eqn)) = factor(rhs(eqn));
(y − 1)(y + 1) = (x + 2)2
Note:
The factor command cannot be used to factorize expressions that cannot be perfectly fac-
torized.
Example 2.28:
The factor command will not recognize that x2 + 2x + 1 + y can be factored
as (x + 1)2 + y.
> eq := xˆ2 + 2*x + 1 + y;
x2 + 2x + 1 + y
> factor(eq);
x2 + 2x + 1 + y
To factor the equation, we need to use the student[completesquare]
command. We will see more on this later.
There are times when you want to factorize the denominator or numerator of an expression.
In this case, the commands denom and numer come in handy.
Example 2.29:
The opposite of factorizing expressions is, of course, expanding expressions. To do this, use
the expand command.
Example 2.30:
> expand((x - 1)*(x + 1));
x2 − 1
"
> expand((x+1)/(x+2));
x 1
+
x+2 x+2
For quotients of polynomials, only sums in the numerator are expanded –
products and powers are left alone. See the normal command for dealing with
quotients of polynomials.
> expand(1/(x+1)/x);
1
(x + 1)x
The expand command can be used for more than just polynomials:
> expand(sin(x+y));
sin(x) cos(y) + cos(x) sin(y)
> expand(exp(a+ln(b)));
ea b
If you have an expression that can be simplified using well-known simplification rules, then
you can use the simplify command to do this. The simplify searches the expression for
function calls, square roots, radicals, and powers, and then invokes the appropriate simplifi-
cation procedures.
Example 2.31:
> simplify(4ˆ(1/2)+3);
5
> te1 := exp(a+ln(b*exp(c)));
te1 := e(a+ln(be ))
c
> simplify(te1);
bea+c
There are times when using the simplify command causes unwanted and unnecessary diffi-
culties, because Maple considers the analytical issue of branches for multi-valued functions.
In situations where such branches are not of interest, then various options can be applied.
Example 2.32:
> g:=sqrt(xˆ2); √
g := x2
> simplify(g);
csgn(x)x
where csgn is the sign function for real and complex functions – see ? csgn
for more information. However, if we are only interested in the symbolic
simplification, then:
> simplify(g,symbolic);
x
Alternatively, we could have done:
> simplify(g,assume=real);
x
> simplify(g,assume=positive);
x
To combine terms in sums, products, and powers into a single term, the combine command
is used. For many functions, the transformations applied by the combine command are the
inverse of the transformations that are applied by the expand command.
Example 2.33:
> expand(sin(a+b));
sin(a) cos(b) + cos(a) cos(b)
> combine(sin(a)*cos(b) + cos(a)*sin(b));
sin(a + b)
> E := exp(x)ˆ2*exp(y);
E := (ex )2 ey
> combine(E);
e2x+y
There may be times when you only want certain terms to be combined. To do this, you need
to specify the appropriate options in the combine command.
Example 2.34:
> eqn := 4*sin(x)ˆ3+exp(y)*exp(x);
eqn := 4 sin(x)3 + ey ex
> combine(eqn);
− sin(3x) + 3 sin(x) + ex+y
> combine(eqn,trig;
− sin(3x) + 3 sin(x) + ex ey
> combine(eqn,exp);
4 sin(x)3 + ex+y
When you want to collect together the coefficients of a variable or function in an equation,
then you should use the collect command.
Example 2.35:
> f := a*ln(x)-ln(x)*x-x;
a ln(x) − ln(x)x − x
> collect(f,ln(x));
(a − x) ln(x) − x
When collecting coefficients, Maple does not automatically sort them into any
particular order.
> g := 3*(x - 2) + x*(2 - 3*xˆ2);
g := 3x − 6 + x(2 − 3x2 )
> collect(g,x);
−6 − 3x3 + 5x
To do so, you need to use the sort command.
> sort(collect(g,x));
−3x3 + 5x − 6
See ? sort for more information. When you want to collect more than one variable or func-
tion in an expression, you do so by specifying a list of objects to be collected.
Example 2.36:
> p := x*y+a*x*y+y*xˆ2-a*y*xˆ2+x+a*x;
p := xy + axy + yx2 − ayx2 + x + ax
> collect(p, x);
(y − ay)x2 + (y + ay + 1 + a)x
> collect(p, [x,y]);
(1 − a)yx2 + ((1 + a)y + 1 + a)x
> collect(p, [y,x]);
((1 − a)x2 + (1 + a)x)y + (1 + a)x
Note that the order the objects are to be collected in will alter the output. If
this is not desired, then use the distributed option.
> collect(p, [x,y], distributed);
(1 + a)x + (1 + a)xy + (1 − a)yx2
See ? collect for more information. Additionally, if you have a polynomial, then you can ask
Maple to pick out particular coefficients using the coeff command.
Example 2.37:
> p := 4*xˆ2 + 3*yˆ3 - 5;
4x2 + 3y 3 − 5
There are two ways to pick out the coefficient of x2 , i.e.,
> coeff(p,x,2);
4
> coeff(p,xˆ2);
4
> coeff(p,x,0);
3y 3 − 5
If you want all of the coefficients, then you can either use the coeff command manually for
each coefficient, or you can use the coeffs command.
Example 2.38:
> s := 3*vˆ2*yˆ2+2*v*yˆ3;
s := 3v 2 y 2 + 2vy 3
The coefficients of v can be picked out by:
> coeffs(s,v,’vt’);
2y 3 , 3y 2
where vt specifies what the corresponding power of v is, i.e.,
> vt;
v, v 2
We can pick out the coefficients of y in a similar manner:
> coeffs(s,y,’yt’);
2v, 3v 2
> yt;
y3, y2
Alternatively, you may only want to select, or remove, certain terms from an expression. To
do so, use the select, remove and selectremove commands.
Example 2.39:
> f := 2*exp(a*x)*sin(x)*ln(y);
f := 2eax sin(x) ln(y)
> select(has, f, x);
eax sin(x)
> remove(has, f, x);
2 ln(y)
> selectremove(has, f, x);
eax sin(x), 2 ln(y)
> g := 2*exp(a*x)*sin(x)*ln(y) + cos(x)*exp(b*y) + 3;
g := 2eax sin(x) ln(y) + cos(x)eby + 3
> select(has,g,cos);
cos(x)eby
> remove(has,g,[cos,ln]);
3
We have seen previously that we can use the op and nops commands to manipulate elements
in a list. We can also use these commands in other expressions.
Example 2.40:
To know the number of terms in an equation, use the nops command, i.e.,
> f := expand(((x-4)*(xˆ2 + x - 3) + xˆ2*(x - 3))ˆ2);
f := 4x6 − 24x5 + 8x4 + 132x3 − 95x2 − 168x + 144
> nops(f );
7
To create a sequence of all the terms, use the op command, i.e.,
> op(f );
4x6 , −24x5 , 8x4 , 132x3 , −95x2 , −168x, 144
To select the nth term of the expression, use:
> op(1,f );
4x6
> op(2,f );
−24x5
> op(3,f );
8x4
and so forth. Further, you can use the op command to find the next level of
terms, i.e.,
> op(4,f );
123x3
> op(op(4,f );
123, x3
If you want to substitute values, or expressions, into an equation or expression, use the subs
command.
Example 2.41:
> w := a*b + b - 3*c;
w := ab + b − 3c
> subs(a=2,w);
3b − 3c
> subs([a=0],w);
b − 3c
> subs([a=1],[b=2],w);
4 − 3c
> subs([a=1, b=3],[b=2],w);
6 − 3c
Example 2.42:
There is a difference between the eval and subs commands:
> z := 6*u*vˆ2 - 3*v + 3*exp(v - u);
z := 6uv 2 − 3v + 3ev−u
> subs(u=2,v=2,z);
42 + 3e0
> eval(z,[u=2,v=2]);
45
The print command displays the values of the arguments of the command to the screen, and
returns NULL as the function value. The arguments are printed separated by a comma and
a blank space.
Example 2.43:
> print(I,Love,Maths);
I, Love, M aths
> print(”This is a string”);
”This is a string”
> x := 42;
x := 42
> print(x);
42
> print(seq(i,i=1..10));
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
The print commands returns no value.
> y := print(4*xˆ2);
16
y :=
> y;
The print command is fairly limited in the format of how the output appears. One alter-
native is to use the printf command. To do so, you need to specify the format string that
defines the format of the output, where for full details see ? printf. Some examples of the
printf command are given below.
Example 2.44:
To output strings, use the %s format:
> printf(”%s\n”,”This is cool!”);
This is cool!
> printf(”%s %s %s\n”,This, is, ”cool!”);
This is cool!
> x := 42;
x := 42
> printf(”The answer is x = %s”, convert(x,string));
The answer is x = 42
Alternatively, to print the number x = 42 without converting to a string, use
the %g format, i.e.,
> printf(”The answer is x = %g”, x);
The answer is x = 42
For full details on all the different types of formats, see ? printf. There a number of different
ways to write data to a file. One of these is the fprintf command, which is related to the
printf command.
Example 2.45:
> x := 23; y := -1/x;
x := 23
−1
y :=
23
You first need to use the fopen command to open the file you want to print
to, i.e.,
> fd := fopen(”temp file”, WRITE):
fprintf(fd, ”x = %d, y = %g”,x,y):
fclose(fd):
which will have written: x = 23, y = −0.0434783 to the file: temp file. The
location of this file will depends on a number of factors. To be certain of it’s
location, use:
> fd := fopen(”C:/temp file”, WRITE):
fprintf(fd, ”x = %d, y = %g”,x,y):
fclose(fd):
where the file is located on the C: drive. Note that you must close the file
using the fclose command – otherwise the file will appear empty.
For more information, see ? fprintf, fopen and fclose. Some other useful commands to
write data to a file include the writeline, writedata and writestat commands.
Example 2.46:
The writeline command writes a string to either the screen or to a file:
> writeline(terminal,”This is a test”,”So is this”));
This is a test
So is this
26
The 26 is the number of characters returned from the writeline command. To
suppress this, use the colon : instead. To write the strings to a file, use:
> writeline(”C:/whatever.txt”,”This is a test”,”So is this”):
fclose(”C:/whatever.txt”):
The writedata command can be used to numerical data to a file.
> A := [[0,1,3.4,2,5.3],[-3,1,5.6,2,4.5]];
A := [[0, 1, 3.4, 2, 5.3], [−3, 1, 5.6, 2, 4.5]]
> writedata(”C:/A data.txt”,A,float):
which writes
0 1 3.4 2 5.3
−3 1 5.6 2 4.5
For more information, see ? writedata, ? writeline and writestat. Another alternative
on how to write data to a file, is to use the writeto and appendto commands. Simply put,
you use the writeto command to specify what file to want to start writing to, and then from
this point onwards any commands executed in the Maple worksheet will have their output
written to the specified file. If you only want to append to a file that already has something
in it, then use the appendto command, which will start writing at the end of the specified file.
Example 2.47:
> writeto(”C:/C data.txt”);
> y := xˆ2 - 2*x + 1;
> 3*4;
> writeto(terminal);
> y;
x2 − 2 ∗ x + 1
where the command writeto(terminal) tells Maple to write back to the
screen again.
The opposite of writing is, of course, reading. To read data from a file, you can use readline,
readdata or readstat, in a similar manner to how the write versions work.
Example 2.48:
then to read in all the data, you need to specify the type of each column, i.e.,
> readdata(”C:/SomeData.txt”,[string,integer,float,integer,float]);
[[”x”, 2, 3., 4, 5.], [”y”, 7, 8., 9, 0.]]
Finally, if you have a file that contains Maple commands, then you can tell Maple to execute
Example 2.49:
Calculus
3.1 Differentiation
To calculate the partial derivative of an expression, use the diff command.
Example 3.1:
> diff(sin(x),x);
cos(x)
> diff(sin(x),y);
0
You can calculate higher order derivatives in a number of ways, i.e.,
> diff(diff(xˆ2 - 3*x + 4,x),x);
2
or,
> diff(xˆ2 - 3*x + 4,x,x);
2
or,
> diff(xˆ2 - 3*x + 4,[x,x]);
2
or,
> diff(xˆ2 - 3*x + 4,x$2);
2
where $ is operator for forming an expression sequence, i.e.,
> x$4;
x, x, x, x
52
A little bit of help with Maple
For more information, see ? $ and ? diff. The $ operator is particularly useful when dealing
with higher order derivatives.
Example 3.2:
> z := sin(x)*cos(y);
z := sin(x) cos(y)
> diff(z,x$42,y$76);
− sin(x) cos(y)
Example 3.3:
> Diff(f(x,y),[x,y]) = diff(sin(x*y),[x,y]);
∂2
f (x, y) = − sin(xy)xy + cos(xy)
∂y∂x
Example 3.4:
> D(sin);
cos
> D(cos);
− sin
To calculate higher order derivatives, use the @@ operator, i.e.,
> (D@@2)(sin);
− sin
The D operator is particularly useful when you want to specify the derivative
of an expression evaluated at a point, i.e., when you need to specify initial or
boundary conditions:
> D(f )(0) = 0;
D(f )(0) = 0
or, in other words,
> convert(D(f )(0) = 0,diff );
d
f (t1) =0
dt1 t1=0
The D operator can also be used for partial derivatives, where you need to specify which
independent variable you want to differentiate with respect to.
Example 3.5:
> D[1](F)(x,y)
convert(%,diff );
D1 (F )(x, y)
∂
F (x, y)
∂x
> D[2](F)(x,y)
convert(%,diff );
D2 (F )(x, y)
∂
F (x, y)
∂y
To calculate higher order partial derivative operators, consider
> D[1,1,2](F)(x,y)
convert(%,diff );
D1,1,2 (F )(x, y)
∂3
F (x, y)
∂y∂x2
3.2 Integration
The opposite of differentiation is, of course, integration. To integrate an expression, use the
int command.
Example 3.6:
To calculate an indefinite integral:
> int(sin(x),x);
− cos(x)
Note that no constant of integration appears in the result. To calculate a
definite integral:
> int(sin(x),x=0..Pi);
2
> int(sin(x),x=0..pi);
1 − cos(π)
To calculate a double integral, you need to repeat the int command, i.e.,
> int(int(sin(x),x),x);
− sin(x)
Note:
There is no shorthand way to represent double, triple and higher order integrals – you must
repeat the int command for each integral.
Example 3.7:
> int(x,x);
x2
2
If we try a shorthand way, similar to higher order derivatives, then Maple
either ignores the extra input or returns an error, i.e.,
> int(x,x,x);
x2
2
> int(x,x$3);
x2
2
> int(x,[x,x,x,x]);
Error, (in int) wrong number (or type) of arguments
Example 3.8:
> Int(f(x),x=a..b);
Z b
f (x)dx
a
> eqn := Int(Int(x*y,x),y); ZZ
eqn := xy dxdy
To get Maple to evaluate the Int command, use the value command, i.e.,
> value(eqn);
x2 y 2
4
Note:
The value command can be used to force Maple to evaluate inert commands, such as Int,
Diff, Limit, Product and Sum, by applying the lowercase versions, i.e., int, diff, limit,
product and sum.
Sometimes Maple cannot evaluate an integral analytically. In this case, Maple will simply
return your call. However, in such situations, if there are no non-numeric values then you can
ask Maple to determine a numerical approximation to the integral using the evalf command.
Example 3.9:
> MyInt := int(sin(cos(sin(x))),x=0..Pi);
Z π
M yInt := sin(cos(sin(x))) dx
0
To determine a numerical approximation to the above integral, use the evalf
command, i.e.,
> evalf(MyInt);
2.147394008
Solving equations
In this chapter, we look at how to use Maple to solve algebraic, differential and recurrence
equations.
Example 4.1:
> eqn := f = m*a;
eqn := f = m a
To solve eqn for a:
"
> solve(eqn,a);
f
m
Note that Maple returns what a is equal to – it does not return an equation. If
there is more than one solution, Maple will return a sequence of solutions, i.e.,
> eq := xˆ4-5*xˆ2+6*x=2;
eq := x4 − 5x2 + 6x = 2
> sols := solve(eq,x); √ √
sols := 1, 1, 3 − 1, −1 − 3
> sols[3]; √
3−1
You can use the solve command to solve a system of equations, i..e,
> e1 := 4*x - 5*y = 3;
e2 := x + 7*y = 0;
e1 := 4x - 5y = 3
e2 := x + 7y = 0
"
> solve({e1,e2},{x,y});
−1 7
{y = ,x = }
11 11
57
A little bit of help with Maple
Note:
If you specify the variables to be solved for using a set, as in the above example, then Maple
will return the answer in any order. In the above example, the order of the variables were
specified by {x,y}, but Maple returned the answer with y first, and then x. However, if you
execute the solve command again, then Maple may return the answers in a different order.
This is particularly true for large systems of equations. If you want Maple to always return
the solution in a desired order, then specify the order of the variables using a list (see example
below).
Example 4.2:
> eqn1 := 3.2*x + 1.3*y + 4.2*z = 5;
eqn2 := 8.7*x + 19*y + 11.2*z = 94;
eqn3 := x + y/4 + z = 1;
eqn1 := 3.2*x + 1.3*y + 4.2*z = 5
eqn2 := 8.7*x + 19*y + 11.2*z = 94
eqn3 := x + y/4 + z = 1
> solve({eqn1, eqn2, eqn3}, [x, y, z]);
[[x = 0.4969502408, y = 5.187800963, z = −0.7939004815]]
> solve({eqn1, eqn2, eqn3}, [x, z, y]);
[[x = 0.4969502408, z = −0.7939004815, y = 5.187800963]]
> solve({eqn1, eqn2, eqn3}, [y, x, z]);
[[y = 5.187800963, x = 0.4969502408, z = −0.7939004815]]
Note that because some of the coefficients of the variables in the above
equations are floating point numbers, then Maple will automatically convert
the answers to floating point numbers.
See ? solve for more information. If you have used the solve command to find the analytical
solutions to an equation, but you want to know the floating point value of the solutions, then
consider the following two methods.
Example 4.3:
> eq := xˆ4-5*xˆ2+6*x-2;
eq := x4 − 5x2 + 6x − 2
> sols := solve(eq,x); √ √
sols := 1, 1, 3 − 1, −1 − 3
If you want a floating point approximation of the solution, then you can either
use evalf, i.e.,
> evalf(solve(eq,x));
1., 1., 0.732050808, −2.732050808
or you can use the fsolve command.
> fsolve(eq,x);
−2.732050808, 0.732050808, 1., 1.
The fsolve command works in pretty much the same way as the solve command. If the
equation is a polynomial, then Maple will return all real roots, by default. If the equation
is not a polynomial, then Maple will return a single root of the equation. There are some
options to try to get Maple to solve for more roots.
Example 4.4:
> fsolve(sin(x) = 1,x);
1.570796327
To get Maple to solve for a different root, either specify a range, i.e.,
> fsolve(sin(x) = 1,x=-6..1);
−4.712388980
or use the avoid option and specify a starting point, i.e.,
> fsolve( sin(x) = 1,x=8,avoid={x=1.570796327, x=-4.712388980});
7.853981636
If you want all the roots of a polynomial, where some of the roots are complex,
then you need to specify the complex option, i.e.,
> polynomial := 23*xˆ5 + 105*xˆ4 - 10*xˆ2 + 17*x;
polynomial := 23x5 + 105x4 − 10x2 + 17x
> fsolve(polynomial);
−4.536168981, −0.6371813185, 0.
> fsolve(polynomial, x, complex);
-4.536168981, -0.6371813185, 0., 0.3040664543 - 0.4040619058 I,
0.3040664543 + 0.4040619058 I
Note that if Maple cannot find a solution, then NULL is returned, i.e.,
> fsolve(xˆ2 + 1 = 0,x);
For more information, see ? fsolve. An alternate to using the solve command to determine
the analytical solution of a system of equations, is the eliminate command.
Example 4.5:
> eqn1 := xˆ2+yˆ2-1;
eqn2 := xˆ3-yˆ2*x+x*y-3;
eqn1 := x2 + y 2 − 1
eqn2 := x3 − y 2 x + xy − 3
> eliminate({
eqn1, eqn2 },x);
3 4 6 3 5 2
{x = 2+y+1
}, {−7y + 4y + 6y − 4y + 4y − 2y + 8}
−2y
where Maple returns one possible expression for the desired variable and
whatever equations are left when the elimination is made.
Example 4.6:
> isolve(xˆ2 - x - 6);
{x = −2}, {x = 3}
> isolve(3*x-4*y=7);
{x = 5 + 4 Z1, y = 2 + 3 Z1}
where Z1 is an arbitrary constant. If isolve cannot determine an
integer-valued solution, then NULL is returned, i.e.,
> isolve(x2̂=3);
For more information, see ? isolve. Additionally, if you want to find integer-valued solutions
(mod n), then use the msolve command.
Example 4.7:
> msolve(3*x-4*y=1,7*x+y=2,19);
{y = 11, x = 15}
> msolve(2ˆi=3,19);
{i = 13 + 18 Z1}
Example 4.8:
> ode := diff(y(x),x,x) = 2*y(x) + 1;
d2
ode := 2 y(x) = 2y(x) + 1
" dx
> dsolve(ode);
√ √ 1
y(x) = e( 2x)
C2 + e(− 2x)
C1 −
2
where C1 and C2 are arbitrary constants of integration. You can specify
conditions to determine the constants of integration, i.e.,
> ics := y(0)=1, D(y)(0)=0;
ics := y(0) = 1, D(y)(0) = 0
"
> sol := dsolve({ode, ics});
3 √ 3 √ 1
sol := y(x) = e( 2x) + e(− 2x) −
4 4 2
To test to see if the solution satisfies the ode, use the odetest command, i.e.,
> odetest(sol,ode);
0
where if zero is returned, then the solution satisfies the ode. If you also want
to check that the solution satisfies the initial conditions, then specify a list of
equations that you want to check whether the solution satisfies, i.e.,
> odetest(sol,[ode, ics]);
[0, 0, 0]
where the first zero corresponds to the ode, the second zero to the first initial
condition and the third zero to the second initial condition, respectively.
Note:
In the above example, to specify initial conditions that contain a derivative, the D operator
is used.
For more information, see ? dsolve. Maple can also determine numerical solutions to odes
using the numeric option in dsolve, i.e.,
Example 4.9:
> deq1 := diff(y(x),x) + sin(x)*y(x)
= 0;
d
deq1 := y(x) + sin(x)y(x) = 0
dx
> ics := y(0) = 1;
ics := y(0) = 1
The analytical solution is given by
> dsol := dsolve(deq1, ics);
ecos(x)
dsol := y(x) =
e
while the numerical solution is given by
> dsol N := dsolve(deq1, ics,numeric);
dsol N := proc(x rkf 45)...end proc
where x rkf 45 denotes the numerical solution is using a Fehlberg fourth-fifth
order Runge-Kutta method with degree four interpolant, and the
proc ... end proc defines a procedure (which we will see more about later).
To see the numerical solution:
> dsol N(0);
[x = 0., y(x) = 1.]
> dsol N(0.1);
[x = 0.1, y(x) = 0.995016612435748794]
> dsol N(2);
[x = 2., y(x) = 0.242647315785959495]
Alternatively, you can use the output=listprocedure option, i.e.,
> dsol N2 := dsolve(deq1, ics,numeric, output=listprocedure);
dsol N 2 := [x = (proc(x)...end proc), y(x) = (proc(x)...end proc)]
> f := eval(y(x),dsol N2);
f := proc(x)...end proc
> f(2);
0.242647315785959495
Note:
To plot the numerical solution, Maple provides the odeplot command within the plots pack-
age (see next chapter).
Example 4.10:
> PDE := x*diff(f(x,y),y)-y*diff(f(x,y),x)
= 0;
∂ ∂
P DE := x f (x, y) − y f (x, y) = 0
∂y ∂x
> pdsolve(PDE);
f (x, y) = F 1(x2 + y 2 )
where F 1(x2 + y 2 ) is an arbitrary function of x2 + y 2 .
The pdsolve command has many options to try to help Maple solve the pde. For example,
you can suggest the form of the solution.
Example 4.11:
> PDE := S(x,y)*diff(S(x,y),y,x)
2 + diff(S(x,y),x)*diff(S(x,y),y)
= 1;
∂ ∂ ∂
P DE := S(x, y) S(x, y) + S(x, y) S(x, y) = 1
∂y∂x ∂x ∂y
If we use pdsolve without any hints, then we get:
> pdsolve(PDE);
d c1 d 1 1
(S(x, y) = F 1(x) F 2(y))&where { F 1(x) = , F 2(y) = }
dx F 1(x) dy 2 F 2(y) c1
However, if we suggest a form of the solution, then
> pdsolve(PDE,HINT=P(x,y)ˆ(1/2)); p
S(x, y) = F 2(x) + F 1(y) + 2xy
where F 1 and F 2 are arbitrary functions of their arguments.
Note:
You can also use pdsolve to try to solve a system of partial differential equations.
Maple can also be used to find a numerical solution to a partial differential equation.
Example 4.12:
Consider the pde:
> PDE := diff(u(x,t),t)=-diff(u(x,t),x);
∂ ∂
P DE := u(x, t) = − u(x, t)
∂t ∂x
subject to the initial-boundary conditions:
> IBC := u(x,0)=sin(2*Pi*x),u(0,t)=-sin(2*Pi*t);
IBC := {u(x, 0) = sin(2πx), u(0, t) = − sin(2πt)}
Then the numerical solution is given by
> pds := pdsolve(PDE,IBC,numeric,time=t,range=0..1);
pds := module()export plot, plot3d, animate, value, settings; ...end module
"
Note that the numerical solution of a pde returns a module, not a proc like
the numerical solution of an ode. Please see later for more information on
module’s.
To examine the numerical values of the solution, use the values option, i.e.,
> pds:-value(t=0)(0);
pds:-value(t=0)(1);
[x = 0., t = 0., u(x, t) = 0.]
[x = 1., t = 0., u(x, t) = −0.244929359829470494 10−15 ]
The above values correspond to t = 0 with x = 0 and x = 1. To plot the
numerical solution, you don’t need the odeplot command, but instead you
can use the plot option, i.e.,
> p1:=pds:-plot(t=0,numpoints=50):
p2:=pds:-plot(t=1/8,numpoints=50,color=blue):
p3:=pds:-plot(t=1/4,numpoints=50,color=green):
plots[display](p1,p2,p3);
For more information, please see ? pdsolve, ? pdsolve, system and ? pdsolve, numeric.
Example 4.13:
> rsolve(f(n) = -3*f(n-1) - 2*f(n-2), f(k));
(2f (0) + f (1))(−1)k + (−f (0) − f (1))(−2)k
You can apply initial conditions, i.e.,
> rsolve(f(n) = -3*f(n-1) - 2*f(n-2), f(1..2)=1, f);
f (n) = −3(−1)n + (−2)n
You can make a procedure of the solution, i.e.,
> rr := rsolve(s(n) = 2*s(n-1), s(0)=1, s, ’makeproc’);
rr := proc (n)if 1 < nargs then (0 procname0 )(args) else expand(2n )
end if end proc
> rr(0), rr(1), rr(2), rr(3);
1, 2, 4, 8
In this chapter we will look at some packages that contain some commands that you may find
useful in Maple.
To include a package into Maple, you need to use the with command.
Example 5.1:
To include the plots package, use
> with(plots);
Warning, the name changecoords has been redefined
[Interactive, animate, animate3d, animatecurve, arrow, changecoords,
complexplot, complexplot3d, conformal, conformal3d, contourplot,
contourplot3d, coordplot, coordplot3d, cylinderplot, densityplot, display,
display3d, fieldplot, fieldplot3d, gradplot, gradplot3d, graphplot3d,
implicitplot, implicitplot3d, inequal, interactive, interactiveparams,
listcontplot, listcontplot3d, listdensityplot, listplot, listplot3d, loglogplot,
logplot, matrixplot, multiple, odeplot, pareto, plotcompare, pointplot,
pointplot3d, polarplot, polygonplot, polygonplot3d, polyhedra supported,
polyhedraplot, replot, rootlocus, semilogplot, setoptions, setoptions3d,
spacecurve, sparsematrixplot, sphereplot, surfdata, textplot, textplot3d,
tubeplot]
Note that if you use the semi colon ;, then you will see a list of all the
commands available in the package. Also, in this case, a warning message is
given saying that this package has redefined a command that already exists in
Maple – this is something to be kept in mind if you need to use either the
standard or the new definition of this command.
To include only a particular command, say the listplot command, use
> with(plots, listplot);
[listplot]
Alternative, if you only want to use a command once or twice, then you can
use the [ ] notation, i.e.,
> plots[listplot]([seq([i,sin(i)],i=0..10)]):
65
A little bit of help with Maple
5.1 plots
The plots package contains over fifty commands that can be used to plot various Maple ex-
pressions and data structures. A brief description of some of the more useful and commonly
needed commands are presented below.
The display and display3d commands can be used to plot multiple plots in the same plot.
Example 4.12 above provides an example of this for display.
Example 5.2:
> with(plots):
> F:=plot3d(sin(x*y), x=-Pi..Pi, y=-Pi..Pi):
G:=plot3d(x + y, x=-Pi..Pi, y=-Pi..Pi):
H:=plot3d([2*sin(t)*cos(s), 2*cos(t)*cos(s), 2*sin(s)], s=0..Pi, t=-
Pi..Pi):
display([F, G, H]);
For more information, see ? plots[display]. To plot a list of points, or an array of data, the
listplot command is very useful.
Example 5.3:
> t := Array([seq(i,i=1..10)]);
t := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> plots[listplot]([seq([t[i],sin(t[i])],i=1..10)],color=pink,thickness=3);
See ? plots[listplot] for more information. The command dsolve can be used to determine
the numerical solution to and ordinary differential equation. To plot the resulting solution,
you can use the odeplot command.
Example 5.4:
> with(plots, odeplot);
[odeplot]
> p:= dsolve(D(y)(x) = y(x), y(0)=1, type=numeric, range=-5..2):
> odeplot(p,thickness=3);
If the range is not specified in the dsolve command, then you need to do so in
the odeplot command, i.e.,
> p2 := dsolve( diff(y(x),x) = sin(x*y(x)),y(0)=2,y(x),type=numeric):
> odeplot(p2,[x,y(x)],0..6,thickness=3);
See ? plots[odeplot] for more information. Another way to plot a polar curve, is to use the
polarplot command.
Example 5.5:
To plot a circle of radius 1:
> polarplot(1);
To plot x = cos θ and y = sin θ, use the similar format to the plot command
with coords=polar option, i.e.,
> polarplot([cos(t),sin(t),t=0..2*Pi],color=gold,thickness=2);
For more information, see ? plots[polarplot]. To plot a list of data in order to create a
closed shaped, you can use the polygonplot and and polygonplot3d.
Example 5.6:
> onepoly := [[0,0],[0,1],[1,1] ,[1,0]];
polygonplot(onepoly,thickness=3);
onepoly := [[0, 0], [0, 1], [1, 1], [1, 0]]
To create a polygon, consider:
> ngon := n − > [seq([cos(2*Pi*i/n), sin(2*Pi*i/n)], i = 1..n)]:
polygonplot(ngon(5),color=BLUE);
Similar closed shaped can be created in three-dimensions using
polygonplot3d.
See ? polygonplot and ? polygonplot3d for more information. To write words, symbols
and sentences onto plots, Maple provides the textplot command.
Example 5.7:
> p := plot(sin(x),x=-Pi..Pi):
delta := 0.05:
t1 := textplot([Pi/2,1+d,‘Local Maxima (Pi/2, 1)‘],align=ABOVE):
t2 := textplot([-Pi/2,-1,‘Local Minima (-Pi/2, -1)‘],align=BELOW):
display(p,t1,t2,thickness=3);
5.2 LinearAlgebra
We have seen how to perform simple operations on Vector’s and Matrix’s. Maple provides
the LinearAlgebra package with over one hundred commands to manipulate Vector’s and
Matrix’s.
Example 5.8:
> with(LinearAlgebra):
> P := Matrix(1..2,1..3,5);
5 5 5
P :=
5 5 5
> Q := Matrix([[1,2,3],[4,5,6]]);
1 2 3
Q :=
4 5 6
To transpose a Matrix, use the Transpose command, i.e.,
> T := Transpose(Q);
1 4
T := 2 5
3 6
To multiply to Matrix’s together, use the Multiply command, i.e.,
> Multiply(Q,T);
14 32
32 77
Example 5.9:
> with(LinearAlgebra):
A := << −1, −3, −6 > | < 3, 5, 6> | < −3, −3, −4 >>;
−1 −3 −6
A := 3 5 6
−3 −3 −4
> Eigenvalues(A);
−4
2
2
> (v, e) := Eigenvectors(A); 1
−4 2
−1 1
v, e := 2 , 12 0 1
2 1 1 0
so the Eigenvectors command returns a Vector containing the eigenvalues
and a Matrix containing the corresponding eigenvectors as the columns of the
Matrix. Thus, checking that Ax = λx, i.e.,
> Multiply(A,e[1..3,1]) = Multiply(v[1],e[1..3,1]);
−2 −2
−2 = −2
4 4
where here λ is
> v[1];
−4
and x is the corresponding eigenvector, i.e.,
> e[1..3,1]; 1
2
1
2
1
Note:
The Multiply command can also be used to multiply Matrix’s with Vector’s and scalars,
as demonstrated above.
5.3 PDEtools
The PDEtools package is a collection of commands and routines to help find analytical so-
lutions to partial differential equations. However, here we present a couple of the commands
that can be utilized elsewhere in Maple.
The dchange command can be used to change the variable in mathematical expressions.
Example 5.10:
> with(PDEtools):
> PDE := diff(f(x,y),x)
+ g(x,y)+ diff(f(x,y),y)=0;
∂ ∂
P DE := f (x, y) + g(x, y) + f (x, y) = 0
∂x ∂y
> tr := x = r + s, y = r - s:
dchange(tr,PDE);
∂
f (r, s) + g(r, s) = 0
∂r
> II := Int(xˆ2,x=0..1);
Z 1
II := x2 dx
0
> dchange(x=sin(y),II);
Z π
2 sin(y)2 cos(y)dy
0
Example 5.11:
It can be easier to read the Maple output if looks more compact, i.e.,
> with(PDEtools):
declare(y(x), prime=x);
y(x) will now be displayed as y
derivatives with respect to x of functions of one variable will now be
displayed with ’
d
so that y(x) now looks like
dx
> diff(y(x),x);
y0
> ode := diff(diff(y(x),x),x)*diff(y(x),x)*y(x)*f(x)-2*diff(y(x),x)ˆ3*xˆ6
+ 2*diff(y(x),x)ˆ2*y(x)*diff(g(x),x) + y(x)ˆ5;
ode := y 00 y 0 yf (x) − 2y 03 x6 + 2y 02 yg 0 + y 5
You can tell Maple to toggle between using the compact form and the full
form, using the OFF and ON options, i.e.,
> OFF;
ode; 3 2
d2
d d 6 d d
2
y(x) y(x) y(x)f (x)−2 y(x) x +2 y(x) y(x) g(x) +y(x)5
dx dx dx dx dx
> ON;
ode;
y 00 y 0 yf (x) − 2y 03 x6 + 2y 02 yg 0 + y 5
Programming in Maple
In this chapter, we look at how to use Maple to create functions and programs. The simplest
way to create a function is by using the −> operator.
Example 6.1:
> f := x −> 3*x + 5;
f := x−> 3x + 5
> f(2);
11
> g := (x,y) −> sin(x)*cos(y) + x*y;
g := (x, y)−> sin(x) cos(y) + xy
"
> g(Pi/2, Pi);
1
−1 + π 2
2
6.1 Procedures
More advanced functions can be created using the proc command. This command can have
numerous arguments, but returns only one value (which may be assigned to some name).
Example 6.2:
> lc := proc( s, u, t, v )
description ”linear combination of the arguments”;
local lincom;
lincom := s * u + t * v;
return lincom
end proc;
lc := proc (s, u, t, v) local lincom; description ”linear combination of
the arguments”; lincom := s*u + t*v; return lincom end proc
Notice that the last statement before the end proc does not need to have a
colon : or a semi-colon ;.
73
A little bit of help with Maple
Note:
When writing programs and procedures, it is good programming practice to indent your code
to make it easier to read. Further, you can use blank lines to enhance the readability of your
code. Finally, you should include comments in your code telling the reader what you are
doing. Comments can be inserted using the # command.
Example 6.3:
> DiffProd := proc(func1, func2, var)
description ”Calculates the derivative of the product of the
two functions, func1 and func2, with respect to ’var’.”:
local Dfunc1, Dfunc2, answer:
Dfunc1 := diff(func1,var): # derivative of func1 w.r.t var
Dfunc2 := diff(func2,var): # derivative of func2 w.r.t var
# calculate derivative of product using product rule
answer := Dfunc1*func2 + func1*Dfunc2:
return answer
end proc;
DiffProd := proc (func1, func2, var)
local Dfunc1, Dfunc2, answer;
description ”Calculates the derivative of the product of th e two func-
tions, func1 and func2, with respect to ’var’.”;
Dfunc1 := diff(func1, var); Dfunc2 := diff(func2, var); answer :=
Dfunc1*func2 + func1*Dfunc2; return answer
end proc
> ans := DiffProd(sin(x),cos(x),x);
ans := cos(x)2 − sin(x)2
> diff(sin(x)*cos(x),x);
cos(x)2 − sin(x)2
> combine(ans);
cos(2x)
Note:
If Dfunc1, Dfunc2 and answer had not been defined as local (or global) variables, then
Maple would print a warning message to the screen saying that they have implicitly been de-
fined as local variables. It is good programming practice to create code that does not produce
any error or warning messages.
6.2 Modules
Procedures can generally only be used to return one result. If you want multiple results re-
turned, then use the module command.
Example 6.4:
If you have two numbers, a and b, and you want to know a + b mod 5 and
a × b mod 5, then consider:
> z5 := module()
export plus, times;
plus := (a,b) −> a + b mod 5;
times := (a,b) −> a * b mod 5;
end module;
z5 := module () export plus, times; end module
where plus and times have been defined as the results to be exported. To
access these results, you need to use the :- operator, i.e.,
> z5:-plus(2,4);
1
> z5:-times(2,4);
3
Note:
We saw an example of a module, when we looked at the pdsolve command.
6.3 If statements
When writing programs, it is common to want to test to see if an expression is true, and then
execute some other statements depending on the value of the first expression. Maple provides
the if statement for this purpose. There is more than one representation of the if statement,
due to optional arguments.
Example 6.5:
> a := 3;
b := 5;
a := 3
b := 5
> if a > b then a else b end if;
5
> if b - a <> 0 then
c := b - a
else
c := a - b
end if;
c := 2
> if c < a and c < b then
a - c;
b-c
elif c < a and c > b then
a - c;
c-b
else
a+b-c
end if;
1
3
The elif command above stands for ‘else if’.
Note:
The last statement before either an else, elif or end if statement does not have to have a
colon : or a semi-colon ;.
Example 6.6:
"
> a := 3; b := 5;
a := 3
b := 5
> 5*(Pi + ‘if‘(a > b,a,b));
5π + 25
Example 6.7:
> summ := 0;
for i from 1 to 10 do
summ := summ + i
end do;
summ := 0
summ := 1
summ := 3
summ := 6
summ := 10
summ := 15
summ := 21
summ := 28
summ := 36
summ := 45
summ := 55
If you don’t want to see each step, then use a colon : at the end of the end do
command.
Note:
If you nest for statements, then Maple will not show the output for each step – even if you
use a semi-colon ;. If you want to see the output, you need to use commands like print,
writeline, etc.
Example 6.8:
> for i from 1 by 2 while i <= 100 do
for j in [1,24,3,-6,0,2] do
i*j
end do;
end do;
However, if you replace i*j by print(i*j), then each step will be printed to the
screen (not shown here as there are 300 lines of output!).
! ,6 combine, 42
!!! , 6 command, 65
+, 7 commands
−, 7 case sensitive, 6
/, 7 executing, 6
<, 7 complex number, see I
<=, 7 convert, 21
<>, 7 list, 17
>, 7 set, 17
>=, 7 cos, 23
Γ, see GAMMA csgn, 42
∗, 7
D
∗∗, 7
differential operator, 53
∞, 11
initial conditions, 61
||, 19
dchange, 71
’, 10
declare, 71
->, 73
delay evaluation, see ’
:-, 75
denom, 41
:=, 9
Diff
#, 74
evaluating Diff, 55
$, 52
inert form of diff, 53
%, see ditto
diff, 52
%%, see ditto
Digits, 13
%%%, see ditto
display, 66
ˆ,7
ditto, see operators
‘, 18
dsolve, 61
abs, 13 numeric, 61
add, 23 plotting numerical solution, 67
and, 7
Eigenvalues, 70
appendto, 49
Eigenvectors, 70
Array, 32
eliminate, 59
Data Type, 33
end of statement
Order, 33
colon :, 6
Storage, 33
seimcolon ;, 6
assign, see :=, 21
eval, 12
cat, 19, 21 evalb, 12
coeff, 44 evalc, 12
coeffs, 44 evalf, 11
collect, 43 evaluating an integral numerically, 55
distributed, 44 numerical value of solve, 58
78
A little bit of help with Maple
evaluation Eigenvalues, 70
at a point, 12 Eigenvectors, 70
boolean, 12 Multiply, 69
complex, 12 Transpose, 69
floating point, see evalf list, 15
executing a statement ln, 11, 23
how to, 6
no output, 6 maple functons, see initial functions and con-
execution group, 6 stants
exp, 11, 23 Matrix, 32, 37
expand, 41 << > | < >>, 38
indentity, 38
factor, 40 member, 7
fclose, 48 minus, 7
fopen, 48 module, 63, 75
for, 76 msolve, 60
fprintf, 48 mul, 24
fractions, 12 Multiply, 69
fsolve, 59
avoid, 59 name, 18
complex, 59 indexed, 19
symbol, 19
GAMMA, 24 nops, 16, 45
normal, 41
help
not, 7
?, 7
number of digits, see Digits
in-built support pages, 7
numer, 41
I, 11
odeplot, 67
if, 75
odetest, 61
implies, 7
op, 16, 45
index, 32
operators
indexed, see name
ditto, 9
infinity, see ∞
logical, 7
initial functions and constants, 11
mathematical, 7
inserting new input prompts, 6
relational, 7
Int
set, 7
evaluating Int, 55
or, 7
inert form of int, 55
int, 54 parse, 21
intersect, 7 PDEtools, 71
isolve, 60 dchange, 71
declare, 71
lhs, 40
pdsolve, 62
Limit
Pi, see pi
evaluating Limit, 55
pi
inert form of limit, 26
difference between pi and Pi, 11
limit, 26
plot, 27
linalg
axes, 27
matrix, 32
boxed, 27
LinearAlgebra, 34, 69
frame, 27 sin, 23
none, 27 solve, 57
normal, 27 sort, 43
color, 27 sqrt, 23
coords, 31 the square root function, 13
labeldirections, 27 starting Maple, 4
labels, 27 string, 21
linestyle, 28 student[completesquare], 40
list of points, 28 subs, 46
multiple functions, 30 subset, 7
numpoints, 28 Sum
options, 28 evaluating Sum, 55
parametric plots, 29 inert form of sum, 25
polar, 30 sum, 23
thickness, 28 symbol, see name
plot3d, 32
plots, 62, 66 tan, 23
display, 30, 66 taylor, 39
display3d, 66 textplot, 68
odeplot, 62, 67 Transpose, 69
polygonplot, 68 type, 21
polygonplot3d, 68 unassign, 10
spacecurve, 32 union, 7
textplot, 68
polygonplot, 68 value, 25, 55
polygonplot3d, 68 Vector, 32, 34
print, 47 < >, 36
printf, 48 column, 35
proc, 73 dot prouct, 36
Product row, 35
evaluating Product, 55
inert form of product, 25 whattype, 22
product, 24 worksheet
classic, 4
read, 51 visually enhanced, 4
readline, 50 writedata, 49
remove, 17, 45 writeline, 49
restart, 10 writestat, 49
not executed, 51 writeto, 49
rhs, 40
rsolve, 64 xor, 7