Welcome To Maple 8: "Command The Brilliance of A Thousand Mathematicians."
Welcome To Maple 8: "Command The Brilliance of A Thousand Mathematicians."
Welcome to Maple 8
"Command the Brilliance of a Thousand Mathematicians."
[sic]
Care of Adam Bowers, just one mediocre mind.
>
Basic Commands
All Maple commands must end with either a semicolon ( ; ) or a colon ( : ). The difference between the two
is that the colon suppresses output.
> a:=5;
a := 5
> b:=-6:
When assigning values to a name, you must use a
:=
Otherwise, no assignment is made.
> c=13;
c = 13
> b;
-6
> c;
c
The basic algebraic operators are what you might expect: +, -, *, and /.
> a+b; a-b; a*b; a/b;
-1
11
-30
-5
6
If you want to use a new value for a name, just go ahead and assign something new:
> a:=14570;
a := 14570
> a;b;c;
14570
-6
c
>
> b:=evaln(b);
b := b
>
You can also assign functional values.
> f:=x^2-1;
2
f := x - 1
> g:=x+1;
g := x + 1
> h:=1/x;
1
h :=
x
> f+g;
2
x +x
> F;
2
x +x
x
> f;
2
x -1
> F/f;
2
x +x
2
x (x - 1)
> expand(F/f);
x 1
+
2 2
x -1 x -1
> simplify(F/f);
1
x-1
> Mobius:=(a*z+b)/(c*z+d);
az+b
Mobius :=
cz+d
Well, that's not going to work, we have to tell Maple very explicitly what to do.
> subs(x=3,f);
8
> subs(z=1-I,Mobius);
(1 - I) a + b
(1 - I) c + d
Oh wait ... what was that "I" thing? You probably can guess that I = -1 . That brings up a good point,
let's talk about some ...
>
>
> evalf(Pi);
3.141592654
Wait a minute? What command was that? If you don't know a command, you open the help
documentation by typing a question mark and then the command.
> ? evalf
>
>
>
> x+I*y;
x+yI
> sqrt(-4*x);
2 -x
Why didn't it pull out the I? Maybe x<0, Maple can't distinguish, unless we tell it in advance.
> assume(x>0);
> sqrt(-4*x);
2I x~
The ~ that appears after the x is Maple's way of letting us know it's assuming something about x. Let's
make it forget that assumption.
> unassign('x');
> x;
x
Good, there is no ~ after the x, so Maple doesn't think it's positive anymore.
>
Maple can also tell us the real and imaginary parts of a complex number.
> w:=5-I*3;
w := 5 - 3 I
> Re(w);
5
> Im(w);
-3
> Re((3-4*1)/(2+3*I));
-2
13
> Im((3-4*1)/(2+3*I));
3
13
>
Let's get back to our Mobius function.
> Mobius;
az+b
cz+d
> Mo1:=simplify(subs(z=1-I,Mobius));
-a + a I - b
Mo1 :=
-c + c I - d
> Re(Mo1);
æ -a + a I - b ö
Âç ÷
è -c + c I - d ø
Maple cannot tell if a,b,c,d are real or imaginary, so it cannot give us the real part.
> assume(a,real);assume(b,real),assume(c,real),assume(d,real);
> Re(Mo1);
2 a~ c~ + a~ d~ + b~ c~ + b~ d~
2 2
2 c~ + 2 c~ d~ + d~
> Im(Mo1);
-b~ c~ + a~ d~
-
2 2
2 c~ + 2 c~ d~ + d~
> is(b,integer);
false
Maple said b was not an integer, because we never made that assumption.
> additionally(b,integer);
> is(b,integer);
true
> is(a,real);
false
> is(b,integer);
false
>
>
> cos(x);
cos(x)
> tan(x);
tan (x)
> cos(Pi);
-1
> sin(Pi/4);
2
2
> tan(Pi/2);
Error, (in tan) numeric exception: division by zero
Oops, I just tried to divide by zero. Whenever I try to do something Maple can't do, it'll let me know.
Although the error message might be unintelligible.
>
And all of our other favorites are here, too.
> sin(x)*csc(x);
sin(x) csc(x)
Oops, that didn't work. Let's simplify, but I don't want to type the whole thing again, so I'll use
%
which will input whatever was last output.
> simplify(%);
1
> simplify(1/sec(x));
cos(x)
> simplify((sin(x)/cos(x)*cot(x));
Error, `;` unexpected
>
The inverse functions just have "arc" written in front of them:
> sin(arcsin(5));
5
> arcsin(sin(5));
5-2π
> cos(arccos(x));
x
> arccos(cos(x));
arccos(cos(x))
> arccos(cos(Pi));
π
>
Maple is pretty good with trig identities, too.
> (sin(x))^2+(cos(x)^2);
2 2
sin(x) + cos(x)
> simplify(%);
1
Note that the ^2 was after the sin(x) and cos(x), not in the middle.
>
> tf:=sin(x+y);
tf := sin(x + y)
> expand(tf);
sin(x) cos(y) + cos(x) sin(y)
> combine(%,trig);
sin(x + y)
> exp(-1);
(-1)
e
> exp(Pi*I)+1;
0
> exp(ln(2));
2
The command ln is the natural log command, but you can use log, too.
> exp(log(2));
2
>
>
>
>
>
Solving Equations
To solve equations for unknowns, simply use the solve command.
> solve(x^2-1=3,x);
2, -2
To put the solutions into a set, put the equations or unknowns in a set.
> solve({x^2-1=3},x);solve(x^2-1=3,{x});
{x = 2}, {x = -2}
{x = 2}, {x = -2}
If you want to solve for 0, you don't need the equals sign:
> solve(x^2-4=0,x); solve(x^2-4,x);
2, -2
2, -2
You can also solve a system of equations for several unknowns, but be sure to put all of the equations into a
set, as well as the unknowns.
> solve({x^2-4,x-2},{x});
{x = 2}
> solve({x+y+z,3*x-2*y},{x,y,z});
ì 5x 3 xü
í x = x, z = - ,y= ý
î 2 2 þ
In this system, one of the unknowns is a free parameter, Maple chose it to be x. You can see that y and z are
solved in terms of x.
In the above family of solutions, if you would rather have the unknowns in terms of z, you can just leave it
out of the list of unknowns.
> solve({x+y+z,3*x-2*y},{x,y});
ì 2z 3 zü
í x=- ,y=- ý
î 5 5 þ
Matrix Manipulation
One of the things I use Maple for most often is to solve linear algebra problems.
There are several ways to define a matrix:
> M:=matrix([[-2,0],[3,1]]);
é -2 0ù
M := ê ú
ê 3 1úû
ë
> N:=matrix(2,3,[1,2,3,4,5,6]);
é 1 2 3ù
N := ê ú
ê 4 5 6úû
ë
Okay, so what happened there? The command evalm tells Maple I am about to evaluate something with
matrices, and whenever that operation is matrix multiplication, I do &* so Maple knows a matrix is being
multiplied by another matrix and not a scalar.
>
> A:=matrix(2,2,1);
é 1 1ù
A := ê ú
ê 1 1úû
ë
> C:=evalm(3*A-B);
é 3 - cos(x) 3 - sin(x) ù
C := ê ú
ê 3 + sin(x) 3 - cos(x)úû
ë
I have just decided I am only interested in the case x=Pi/2, so why don't I substitute in the value? I have to
remember that Maple has a hard time knowing matrix commands from scalar commands, so I need to use the
map2 command. This command will map the command subs into each matrix entry.
> BB:=map2(subs,x=Pi/2,B);
é æ πö æ πö ù
ê cosç ÷ sinç ÷ ú
ê è 2ø è 2ø ú
BB := ê ú
ê ú
ê -sinæ π ö æ πö ú
cosç ÷ ú
ê ç ÷
ë è 2ø è 2ø û
> simplify(BB);
é 0 1ù
ê ú
ê -1 0úû
ë
> C:=simplify(evalm(3*A-BB));
é 3 2ù
C := ê ú
ê 4 3úû
ë
So now I have a matrix. Let's say I really like looking at entries in the 2nd row 1st column. I can get it all by
itself using the following command:
> C[2,1];
4
I put the name of the matrix, followed by a list containing the row and column numbers:
MatrixName[RowNumber,ColumnNumber].
> C[1,2];
2
> C[1,1]-C[2,2];
0
> 1002*C[2,1]+999*C[1,2];
6006
> C[3,2];
Error, 1st index, 3, larger than upper array bound 2
The command with tells Maple to open up one of the predefined packages, in this case the linalg package. If
you choose not to suppress the output with a colon, it lists all commands in the package. (For now, you can
just ignore any warnings of "redefined and unprotected".)
>
Now we can take an inverse, or find a transpose.
> C;
C
> op(C);
é 3 2ù
ê ú
ê 4 3úû
ë
The op command, among other things, shows the matrix instead of just the name.
> det(C);
1
> Cinv:=inverse(C);
é 3 -2ù
Cinv := ê ú
ê -4 3úû
ë
> Ctrans:=transpose(C);
é 3 4ù
Ctrans := ê ú
ê 2 3úû
ë
> A:=diag(a,b,c);
é a 0 0ù
ê ú
A := ê 0 b 0ú
ê ú
ê ú
ë 0 0 cû
> X:=diag(x,y,z);
é x 0 0ù
ê ú
X := ê 0 y 0ú
ê ú
ê ú
ë 0 0 zû
You can do some nifty things with linalg. For example, using the linsolve command, you can find which
matrix will get you from one to another.
> phil:=randmatrix(3,3);
é -85 -55 -37ù
ê ú
phil := ê -35 97 50ú
ê ú
ê ú
ë 79 56 49û
> george:=randmatrix(3,3);
é 63 57 -59ù
ê ú
george := ê 45 -8 -93ú
ê ú
ê ú
ë 92 43 -62û
If you didn't trust your matrix reading ability, then you could check by subtraction.
> evalm(phil &* martha - george);
é 0 0 0ù
ê ú
ê 0 0 0ú
ê ú
ê ú
ë 0 0 0û
> N:=matrix([[a,b],[c,d]]);
é a bù
N := ê ú
ê c dúû
ë
Now we solve.
> sols:=solve(eqs,{a,b,c,d});
sols := {c = c, d = d, b = -4 d, a = -4 c}
So any matrix of the form a=-4c and b=-4d will suffice. We can substitute these values into out matrix, but
remember to use the map2 command, otherwise Maple might get confused.
> newN:=map2(subs,sols,N);
é -4 c -4 dù
newN := ê ú
ê c d úû
ë
This is just the tip of the linalg iceberg. For more information, just type ?linalg
>
>
>
>
>
... but you'll have to enter a domain. Be sure to separate the endpoints of the domain with two periods (as in
"..").
> plot(1/x,x=-5..5);
500
400
300
200
100
0
-4 -2 0 2 4
x
-100
-200
y
2
0
-4 -2 0 2 4
x
-2
-4
Yeah, that's much better. Let's try something in three dimensions, but now we have to use plot3d.
> plot3d(sin(x*y)/(x*y),x=-2..2,y=-2..2);
If you want, you can move it around by clicking on it and dragging your pointer around. If you right click,
you can change colors, export as an eps file, and many other things.
For more advanced plotting, you should try the plots package. The plots command inequal will allow you
graph sets of inequalities. For more info, just type ?plots
> ? plots
>
>
>
>
>
Segregation is Bad, but Differentiation is Good.
Differentiation is quite straightforward. Just use the diff command on your function, remembering to tell
Maple which variable with which to differentiate.
Let's differentiate the function
> f:=x^3+2*x^2-4*x+1;
3 2
f := x + 2 x - 4 x + 1
> diff(f,x);
2
3x +4x-4
> diff(%,x);
6
> diff(g,x);
2
2 sin(x) cos(x) + 1 + tan (x)
> diff(g,y);
y
e
> diff(diff(g,x),x);
2 2 2
2 cos(x) - 2 sin(x) + 2 tan (x) (1 + tan (x) )
> diff(diff(g,y),y);
y
e
> diff(diff(g,x),y);
0
> diff(log(x)/x,x);
1 ln(x)
-
2 2
x x
>
>
>
>
Integration
Integration is done much like differentiation, but the command is int.
> f:=x^3+x^2+x+1;
3 2
f := x + x + x + 1
> int(f,x);
1 4 1 3 1 2
x + x + x +x
4 3 2
> int(diff(f,x),x);
3 2
x +x +x
> int(sin(x),x=0..Pi);
2
A function might be discontinuous, but you can tell Maple to overlook that problem.
> int(1/x,x=-10..10);
undefined
> int(1/x,x=-10..10,'continuous');
-I π
>
>
Some functions might be hard to integrate, so Maple might give you something like this:
> h:=exp(-x^2)*log(x);
(-x 2)
h := e ln(x)
> int(h,x);
ó 2
ô e (-x ) ln(x)
ô dx
ô
õ
> P1:=int(P,phi=0..Pi/2);
P1 := r cos(θ)
> P2:=int(P1,theta=0..Pi/4);
2 r
P2 :=
2
> P3:=int(P2,r=0..2);
P3 := 2
In this example, I used more than one line, but that's okay, as long as I remember to put semi-colons (or
colons) in all but the first line.
In the previous example, I used shift-enter to make a blank line below the prompt.
>
> if lantern=1 then print(`By land`) else
print(`By sea`);
fi;
By land
> lantern:=2;
lantern := 2
This might not be exactly what you want, because any value for lantern other than 1 will give `By sea`. How
do we solve this problem? By making our program longer, of course.
> lantern:=3;
lantern := 3
>
Usually, if-than commands are not used on there own. Usually, they are used inside of other structures, like
a for-do command.
> for i from 1 to 3 do print(i) od;
1
2
3
>
The for-do commands make it much easier to do repeated calculations.
> for i from 1 to 3 do i^5 od;
1
32
243
Now I have made sure that the name "lantern" has no value assigned to it.
> for lantern from 1 to 3 do
if lantern=1 then print(lantern,`By land`) fi;
if lantern=2 then print(lantern,`By sea`) fi;
if lantern>2 then print(lantern,`Are you smoking on the job, Henry?`)
fi;
od;
1, By land
2, By sea
3, Are you smoking on the job, Henry?
> for m to 6 do
if is(x||m,even) then print(`even`) else print(`odd`) fi;
od;
odd
even
odd
even
odd
even
> M2:=matrix([[2,2,-2],[0,-4,4],[2,1,4]]);
é 2 2 -2ù
ê ú
M2 := ê 0 -4 4ú
ê ú
ê ú
ë 2 1 4û
> M3:=matrix([[1,1,1],[2,2,2],[3,3,Pi]]);
é 1 1 1ù
ê ú
M3 := ê 2 2 2ú
ê ú
ê ú
ë 3 3 πû
> for i to 3 do
linalg[det](M||i);
od;
-3
-40
0
> for i to 3 do
for j to 3 do
if M3[i,j]=Pi then print(M3[i,j],`huh?`) fi
od;
od;
π, huh?
>
For loops can get really complicated. Usually they are planted in programs, but that is a topic for another
time. For more information just type ?for
> ?for
Go on ... do it ... you know you want to ...
>
>