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;

5

-1

6

2/3

8

> 1/3; evalf(1/3); 1.0/3.0;

1/3

.3333333333

.3333333333

> Pi; evalf(Pi); evalf(Pi,20); sqrt(2); evalf(sqrt(2)); evalf(sqrt(2),30);

Pi

3.141592654

3.1415926535897932385

sqrt(2)

1.414213562

1.41421356237309504880168872421

(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));

(x-1)*(x+1)

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);

1, -1

sqrt(1+b^2), -sqrt(1+b^2)

sqrt(a^2-1), -sqrt(a^2-1)

> 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});

{b = -1/2, a = 3/2}

{a = 1, b = -3, c = 2}

> solve(cos(x)=x,x); fsolve(cos(x)=x,x);

RootOf(_Z-cos(_Z))

.7390851332

(Note: The equation cos(x) = x 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;

y := c*exp(x)

c*exp(x)

> c:=1; y; c:=2; y; c:=Pi; y; c:=a; y; c:='c'; y; y:='y'; y;

c := 1

exp(x)

c := 2

2*exp(x)

c := Pi

Pi*exp(x)

c := a

a*exp(x)

c := 'c'

c*exp(x)

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);

y := x^2-1

[Maple Plot]

-1

8

(Note: subs stands for"substitute".)

> solve(y=0,x); limit(y,x=0); limit(y,x=infinity); diff(y,x); int(y,x);

1, -1

-1

infinity

2*x

1/3*x^3-x

> limit(sin(x)/x,x=0); limit(sin(x)/x,x=infinity);

1

0

> diff(exp(x)*sin(x),x); int(exp(x)*sin(x),x);

exp(x)*sin(x)+exp(x)*cos(x)

-1/2*exp(x)*cos(x)+1/2*exp(x)*sin(x)

> int(exp(x)*sin(x),x=0..Pi); int(exp(-x^2),x=0..infinity);

1/2*exp(Pi)+1/2

1/2*sqrt(Pi)

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

[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...

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);

y := cos(x)/x

[Maple Plot]

(This graph doesn't look "right"! Try to understand why. Hint: limit(cos(x)/x,x = 0) = %? )

> plot(y,x=-10..10,'y'=-20..20); plot(y,x=-10..10,'y'=-3..3);

[Maple Plot]

[Maple Plot]

> plot(y,x=-10..10,'y'=-3..3,title=`y=cos(x)/x`);

[Maple Plot]

(Restricting the y -range helped! Incidentally, what about the right quotes around the y in the range specification? Remember that we defined y to be an abbreviation for cos(x) / x . The y in the range specification, however, is just a label for the vertical axis. To make that clear to Maple, we need to temporarily "undefine" y . 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!`);

y := sin(1/x)

[Maple Plot]

> plot(y,x=0..1,numpoints=1000,title=`much better, but time-consuming!`);

[Maple Plot]

(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);

y := sin(x)

z := cos(x)

[Maple Plot]

Next, let's graph the function c*exp(x) for various values of the constant c , say, for c =1,2,3. To begin with, define the function:

> y:=c*exp(x);

y := c*exp(x)

Now make c equal to 1, check what y is in this case, and plot it as usual:

> c:=1; y; plot(y,x=-1..3,'y'=0..60);

c := 1

exp(x)

[Maple Plot]

To obtain the graph for c =2, just move the cursor back, change c to 2, and re-execute the last three commands. You can then change c 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 := 2

2*exp(x)

[Maple Plot]

> c:=3; y; plot(y,x=-1..3,'y'=0..60);

c := 3

3*exp(x)

[Maple Plot]

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);

[Maple Plot]

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);

s := exp(x), 2*exp(x), 3*exp(x)

[Maple Plot]

(Note: seq , which stands for "sequence", is a Maple command, while s 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 bunch_of_functions or something like that.)

What if you want to change c in non-integer increments? As an example, try the following:

> s:=seq((c/5)*exp(x),c=-10..10); plot({s},x=-1..3);

s := -2*exp(x), -9/5*exp(x), -8/5*exp(x), -7/5*exp(...
s := -2*exp(x), -9/5*exp(x), -8/5*exp(x), -7/5*exp(...

[Maple Plot]

So far, so good. But what if you need to plot c*exp(x) for, say, c = -1 , c = sqrt(2) , and c = Pi/2 ? 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);

s := -exp(x), sqrt(2)*exp(x), 1/2*Pi*exp(x)

[Maple Plot]

Another solution, sometimes more convenient, is to generate the plots separately and then display them simultaneously . To begin with, specify the first c -value and generate the first plot, naming it p1 (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 c to sqrt(2) (c:=sqrt(2) ) and p1 to p2 .

> c:=sqrt(2): p2:=plot(c*exp(x),x=-1..3):

Then repeat the process, changing c to Pi/2 ( c:=Pi/2 ) and p2 to p3 .

> 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});

[Maple Plot]

[Maple Plot]

[Maple Plot]

[Maple Plot]

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});

[Maple Plot]

(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" y 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';

y := 'y'

> implicitplot(x^2+y^2=1,x=-1..1,y=-1..1);

[Maple Plot]

> implicitplot(x^2-y^2=1,x=-5..5,y=-5..5);

[Maple Plot]

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);

[Maple Plot]

> contourplot(x^2-y^2,x=-5..5,y=-5..5,color=black,axes=normal);

[Maple Plot]

To go back to the Introduction .

To find out Even more on Maple in particular commends for differential equations.