More on Basic Maple Commands
A. J. Meir
Copyright (C) A. J. Meir. All rights reserved.
This worksheet is for educational use only. No part of this publication may be reproduced or transmitted for profit in any form or by any means, electronic or mechanical, including photocopy, recording, or any information storage and retrieval system without prior written permission from the author. Not for profit distribution of the software is allowed without prior written permission, providing that the worksheet is not modified in any way and full credit to the author is acknowledged.
(This document is largely based on a similar document originally written by P. G. Schmidt.)
To familiarize yourself with the basics of Maple's syntax, carefully type and execute each of the following commands (one at a time, and in the given order). Most are self-explanatory. In each case, try to guess what the command will cause Maple to do, before executing it to see what happens. If you can't figure out what a given command "does", consult the online help manual (see Item 5 on the handout " Introduction to Maple").
> restart;
(Note: You should enter this command whenever you open a new worksheet, so that Maple starts "with a clean slate". From time to time, you may want to re-execute the command, in order to clear Maple's computing engine; this will have no effect on the appearance of your worksheet.)
> 2+3; 2-3; 2*3; 2/3; 2^3;
> 1/3; evalf(1/3); 1.0/3.0;
> Pi; evalf(Pi); evalf(Pi,20); sqrt(2); evalf(sqrt(2)); evalf(sqrt(2),30);
(Note: evalf stands for "evaluate as a floating-point number".)
> factor(x^2-1); expand((x-1)*(x+1)); simplify((x^2-1)/(x-1));
> solve(x^2-1=0,x); solve(a^2-b^2=1,a); solve(a^2-b^2=1,b);
> solve({a+b=1,a-b=2},{a,b}); solve({a+b+c=0,a+2*b+3*c=1,a-b-c=2},{a,b,c});
> solve(cos(x)=x,x); fsolve(cos(x)=x,x);
(Note: The equation
cannot be solved
symbolically
; this explains why
solve
fails to produce a useful answer. However, the equation does have a unique solution, which can be found
numerically
, using
fsolve
.)
> y:=c*exp(x); y;
> c:=1; y; c:=2; y; c:=Pi; y; c:=a; y; c:='c'; y; y:='y'; y;
(Note: The commands c:='c' a nd y:='y' "undefine" the respective variables.)
> y:=x^2-1; plot(y,x=-3..3); subs(x=0,y); subs(x=3,y);
(Note: subs stands for"substitute".)
> solve(y=0,x); limit(y,x=0); limit(y,x=infinity); diff(y,x); int(y,x);
> limit(sin(x)/x,x=0); limit(sin(x)/x,x=infinity);
> diff(exp(x)*sin(x),x); int(exp(x)*sin(x),x);
> int(exp(x)*sin(x),x=0..Pi); int(exp(-x^2),x=0..infinity);
The rest of this handout is concerned with graphing. Several Maple routines related to graphing are not automatically available when you start Maple; they must be loaded separately. To do so, enter
> with(plots);
Warning, the name changecoords has been redefined
upon which Maple should print a list of commands -- the contents of the package of routines that is being loaded. If you just want to load the package, without having its contents displayed, end the command with a colon (:) instead of a semicolon (;). (Generally, using a colon instead of a semicolon at the end of a command prevents Maple's response from being displayed on the screen.)
Now execute the following commands to learn about some useful "options" that are available with the basic plot command.
> y:=cos(x)/x; plot(y,x=-10..10);
(This graph doesn't look "right"! Try to understand why. Hint:
)
> plot(y,x=-10..10,'y'=-20..20); plot(y,x=-10..10,'y'=-3..3);
> plot(y,x=-10..10,'y'=-3..3,title=`y=cos(x)/x`);
(Restricting the
-range helped! Incidentally, what about the right quotes around the
in the range specification? Remember that we defined
to be an abbreviation for
/
. The
in the range specification, however, is just a
label
for the vertical axis. To make that clear to Maple, we need to temporarily "undefine"
. Check out what happens if you omit the quotes.)
Plotting the following function is a tough job, and Maple needs some guidance:
> y:=sin(1/x); plot(y,x=0..1,title=`pretty rough!`);
> plot(y,x=0..1,numpoints=1000,title=`much better, but time-consuming!`);
(Make an effort to understand what's going on here!)
It's easy to graph several functions simultaneously . Try this:
> y:=sin(x); z:=cos(x); plot({y,z},x=0..10);
Next, let's graph the function
for various values of the constant
, say, for
=1,2,3. To begin with, define the function:
> y:=c*exp(x);
Now make
equal to 1, check what
is in this case, and plot it as usual:
> c:=1; y; plot(y,x=-1..3,'y'=0..60);
To obtain the graph for
=2, just move the cursor back, change
to 2, and re-execute the last three commands. You can then change
again and repeat the process. (By the way, you may type several commands on the same line and execute them simultaneously, with only one hit of the Return key. Try it!)
> c:=2; y; plot(y,x=-1..3,'y'=0..60);
> c:=3; y; plot(y,x=-1..3,'y'=0..60);
How can we graph all three of these functions simultaneously ? Of course, we could simply type
> plot({exp(x),2*exp(x),3*exp(x)},x=-1..3,'y'=0..60);
but this method is very cumbersome if you need more than two or three graphs or if the functions to be plotted are complicated. Here's a shortcut:
> s:=seq(c*exp(x),c=1..3); plot({s},x=-1..3,'y'=0..60);
(Note:
seq
, which stands for "sequence", is a Maple command, while
is just a variable that we use as a name for the sequence of functions to be plotted; we could just as well have called it
or something like that.)
What if you want to change
in
non-integer
increments? As an example, try the following:
> s:=seq((c/5)*exp(x),c=-10..10); plot({s},x=-1..3);
So far, so good. But what if you need to plot
for, say,
,
, and
? All it takes is a minor modification of the sequence command:
> s:=seq(c*exp(x),c={-1,sqrt(2),Pi/2}); plot({s},x=-1..3);
Another solution, sometimes more convenient, is to
generate the plots separately
and then
display them simultaneously
. To begin with, specify the first
-value and generate the first plot, naming it
(say):
> c:=-1: p1:=plot(c*exp(x),x=-1..3):
(Use a colon instead of a semicolon at the end, to prevent lots of plot data from being displayed on the screen.) Now re-execute the last two commands, changing
to
(c:=sqrt(2)
) and
to
.
> c:=sqrt(2): p2:=plot(c*exp(x),x=-1..3):
Then repeat the process, changing
to
(
c:=Pi/2
) and
to
.
> c:=Pi/2: p3:=plot(c*exp(x),x=-1..3):
You can now look at the three plots, one at a time, or display them together:
> p1; p2; p3; display({p1,p2,p3});
You can further improve your graphics output by using Maple's style, symbol, and color options. Try, for example,
> p1:=plot(cos(x)/x,x=0..10,color=green):
> p2:=plot(sin(x)/x,x=0..10,color=blue):
> p3:=plot({1/x,-1/x},x=0..10,-3..3,style=point,symbol=cross,color=black):
> display({p1,p2,p3});
(Note: The
display
command will not accept plots that contain a range specification of the form
'y'=-3..3
. That's why we simply specified the range of the vertical axis as
-3..3
, omitting the label. We could get a label by typing
y=-3..3
, but then we would have to "undefine"
before executing the plot command.)
While color graphs look nice on a color monitor, it is usually a good idea to insert the option color=black into the plot command when preparing a graph for printing (unless you have access to a color printer, of course). -- For additional information on plot options, type ?plot,options (followed by a semicolon).
display is one of the commands that Maple would not recognize had we not previously entered with(plots) . Another such command is implicitplot , which plots the graphs of equations (rather than functions). Try the following:
> y:='y';
> implicitplot(x^2+y^2=1,x=-1..1,y=-1..1);
> implicitplot(x^2-y^2=1,x=-5..5,y=-5..5);
Closely related is the command contourplot , which plots the level curves of a function of two variables. Examples:
> contourplot(x^2+y^2,x=-5..5,y=-5..5,color=black,axes=normal);
> contourplot(x^2-y^2,x=-5..5,y=-5..5,color=black,axes=normal);
To go back to the Introduction .
To find out Even more on Maple in particular commends for differential equations.