Maple Tutorial

Maple Tutorial

Up

Running Maple

To use Maple on one of the sun workstations on the balcony of Prince Lab, log in and start up a command tool to work in. The default settings should bring up a command tool automatically when you first log in. In case you haven't run into one before, a command tool is a window marked "cmdtool" which accepts UNIX commands. If you are missing a command tool, simply click on the background of the screen using the right mouse button to select the "programs" submenu and "command tool" option. Once you have a command tool on the screen, type "xmaple &" at the prompt. The ampersand has the effect of executing Maple in the background, leaving the command tool free for other use. "xmaple" in contrast to plain "maple" will start up the interactive version of Maple which executes in its own window. To exit Maple, simply select the "exit" option under the "file" submenu in the top left corner of the Maple window.

Introduction

You are about to undertake a tutorial that will guide you through portions of the symbolic manipulation package called Maple. Maple is a powerful program that can be used interactively to do a wide variety of things ranging from tedious algebra to plotting three-dimensional graphics. This tutorial is intended to provide you with an overview of some of Maple's capabilities, as well as warn you about a few of the common pitfalls. In the material that follows you will repeatedly encounter text in the form shown below:

> 1 + 1; 2

When text appears like this, the entry in bold following the > sign indicates items that should be entered by you at the Maple cursor. The text that follows is the output produced by Maple in response to your input. In the operation above, for instance, Maple responded to 1 + 1 by performing the addition correctly. Good !

Every Maple command must end with a semicolon as shown in the example above (or a colon if you don't want output displayed). For example, for the sum 1 + 1, enter the following:

> 1 + 1;

to which Maple responds with:

2

The use of a semicolon is crucial. Until you enter a semicolon, Maple assumes that you are still in the process of entering a single command. For example, the command above is equivalent to,

> 1
> + 1;
2

Alternately,

> 1
> +
> 1
> ;
2

In either case, you can recognize that the semicolon behaves as a cue for Maple, indicating that the expression is ready for evaluation. This is a useful feature when dealing with long expressions that require multiple lines of input.

Arithmetic

We have encountered addition within Maple above. Analogously, subtraction follows simply as:

> 5 - 2;

with output,

3

Multiplication requires an asterisk *,

> 5*5;
25

As you might expect, division is signified by a backslash /,

> 25/5;
5

To raise something to a power, proceed as follows:

> 8^2;
64

Note that if we fail to use semicolons appropriately, syntax errors may arise,

> 10^20
> 10^20;
syntax error:
10^20;
^

Here Maple interprets your input as 10^20 10^20, which is meaningless.

> 10^20;

100000000000000000000

Another interesting feature of Maple is the factorial function. You will remember that 5! = 5*4*3*2*1. Try it out.

> 3!;

6

Let's try something that puts Maple through its paces,

> 1000!;

4023872600770937735437024339230039857193748642107146325437999104299385123986290 2059204420848696940480047998861019719605863166687299480855890132382966994459099 7424504087073759918823627727188732519779505950995276120874975462497043601418278 0946464962910563938874378864873371191810458257836478499770124766328898359557354 3251318532395846307555740911426241747434934755342864657661166779739666882029120 7379143853719588249808126867838374559731746136085379534524221586593201928090878 2973084313928444032812315586110369768013573042161687476096758713483120254785893 2076716913244842623613141250878020800026168315102734182797770478463586817016436 5024153691398281264810213092761244896359928705114964975419909342221566832572080 8213331861168115536158365469840467089756029009505376164758477284218896796462449 4516076535340819890138544248798495995331910172335555660213945039973628075013783 7615307127761926849034352625200015888535147331611702103968175921510907788019393 1781141945452572238655414610628921879602238389714760885062768629671466746975629 1123408243920816015378088989396451826324367161676217916890977991190375403127462 2289988005195444414282012187361745992642956581746628302955570299024324153181617 2104658320367869061172601587835207515162842255402651704833042261439742869330616 9089796848259012545832716822645806652676995865268227280707578139185817888965220 8164348344825993266043367660176999612831860788386150279465955131156552036093988 1806121385586003014356945272242063446317974605946825731037900840244324384656572 4501440282188525247093519062092902313649327349756551395872055965422874977401141 3346962715422845862377387538230483865688976461927383814900140767310446640259899 4902222217659043399018860185665264850617997023561938970178600408118897299183110 2117122984590164192106888438712185564612496079872290851929681937238864261483965 7382291123125024186649353143970137428531926649875337218940694281434118520158014 1233448280150513996942901534830776445690990731524332782882698646027898643211390 8350621709500259738986355427719674282224875758676575234422020757363056949882508 7968928162753848863396909959826280956121450994871701244516461260379029309120889 0869420285106401821543994571568059418727489980942547421735824010636774045957417 8516082923013535808184009699637252423056085590370062427124341690900415369010593 3983835777939410970027753472000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000

Ugly ! What happens if you divide the above number by 999! ? The answer is extremely simple...you can do it in your head.

Maple knows about certain special numbers such as E and Pi. For example, if we wish to compute 2*Pi we enter,

> Pi * 2;

2 Pi

Case matters. For example, pi is just a variable, but Pi is the geometric constant we are used to. Make sure that you use Pi rather than pi with Maple. Incidentally, for lots of interesting information on Pi, peek into Pi through the ages ! Note that Maple does not evaluate the expression explicitly. Rather than introduce an approximate numerical value for Pi, it carries the constant along instead. To force Maple to evaluate the expression numerically enter,

> evalf(Pi * 2);

6.283185308

evalf explicitly evaluates expressions as decimal numbers. Let's drive a little harder. How many decimal places do you know Pi to ? Let's request Maple for Pi to a 1000 decimal places. How do you suppose this number is calculated ? How do we know it is right ?

> evalf(Pi, 1000);

3.14159265358979323846264338327950288419716939937510582097494459230781640628620 8998628034825342117067982148086513282306647093844609550582231725359408128481117 4502841027019385211055596446229489549303819644288109756659334461284756482337867 8316527120190914564856692346034861045432664821339360726024914127372458700660631 5588174881520920962829254091715364367892590360011330530548820466521384146951941 5116094330572703657595919530921861173819326117931051185480744623799627495673518 8575272489122793818301194912983367336244065664308602139494639522473719070217986 0943702770539217176293176752384674818467669405132000568127145263560827785771342 7577896091736371787214684409012249534301465495853710507922796892589235420199561 1212902196086403441815981362977477130996051870721134999999837297804995105973173 2816096318595024459455346908302642522308253344685035261931188171010003137838752 8865875332083814206171776691473035982534904287554687311595628638823537875937519
57781857780532171226806613001927876611195909216420199

Make sure you understand the above instruction by determining E to a 100 decimal places.

evalf works in other situations as well. In the following, we will use it to obtain approximate values for the fraction:

> 2 + 1/3;

7/3

To evaluate the fraction numerically, we will use evalf but this time with the symbol " which Maple recognizes as shorthand representing the expression on the previous line.

> evalf(");

2.333333333

Maple permits the definition of variables. To define a variable called emu and assign to it a value of 12 we proceed as follows.

> emu := 12;

emu := 12

Here := signifies assigned to. We can now query Maple for the value of emu,

> emu;

12

or, we can multiply emu by 10,

> emu * 10;

120

A particularly useful feature of Maple is the online help utility. To request help on any Maple command, simply enter ? followed by the command of interest. For help with multiplication, for instance, enter the following:

> ?*

In response, Maple will open a new window containing the following information:

 


HELP FOR: Arithmetic operators +, -, *, /, ^, **

SYNOPSIS:  
- An expression can be composed using the arithmetic operators. 
  Note that the exponentiation operator `^` is equivalent to `**`.

- Such expressions can be one of three types: type `+`, type `*`,
  or type `^`  (equivalently, type `**`).  That is, the expression
  a - b is of type `+` with  operands a and -b.  Similarly, a/b is
  of type `*` with operands a and b^(-1).  Finally, a^b is of type 
 `^` with operands a and b.
  
- The representation used for these algebraic expressions is often
  referred to as sum-of products form.
   
- An expression of type `^` has exactly two operands.  (It is a
  syntax error to express a^b^c without parentheses).  An expression
  of either type `+` or type `*` can have two or more operands.

EXAMPLES: 
> op( x+y-z+w );
                                  x, y, - z, w
   
> op( 2*x^2*y );    
                                        2
                                    2, x , y
   
> op( (x+y)^z );   
                                    x + y, z
   
SEE ALSO: algebraic, convert, type, op

                                             

To get rid of the help window, click and drag down the "file" submenu in the top left corner to select "exit". Help can also be requested by selecting the "help" menu in the top right corner of the Maple window. Try it ! Maple also comes with its own tutorial, containing many interesting examples. To work through it enter,

> tutorial();

As the tutorial is quite advanced, we recommend you try it only when you are comfortable with the material in this one.

Algebra and Plotting

Almost everything we have done so far we could have done just as fast without ever using Maple. To really begin to take advantage of Maple we move on to some more advanced topics. Lets look at some symbolic calculations using Maple, where we tinker with variables without defining their numerical
values,
 

> (x + 2*y)^2;

	         2
	(x + 2 y) 
                    

Alternately, using shorthand " to refer to the last result, we can expand the expression,
 

> expand(");

	 2          2
	x + 4xy + 4y 
                              

Another example:
 

> (x*y - y^2) / (x - y);

	 2
x y - y
--------
	           x - y                      

Maple does not simplify expressions automatically. In the previous example, for instance, as the numerator equals y*(x - y), the expression reduces to just y. For Maple to perform such a simplification, it must be explicitly requested,
 

> simplify(");

y

where the " again refers to the previous result. Having warmed up with some simple algebra, we are in shape to attempt solving algebraic equations. First, consider our old friend the quadratic equation. To make the steps somewhat
clearer, we use the assigned to := operation to define a variable that will be our abbreviation for a quadratic equation,
 

> eland := x^2 + 8*x + 12;

       2
	eland := x + 8 x + 12                      

To solve the equation, eland = 0, enter the following:
 

> solve(eland = 0, x); -6, -2

where x within the solve command specifies the variable to be solved for. Are these in fact solutions to the quadratic equation ? We can use Maple to carry out the check very easily. For example, substituting x = -6 into eland,
 

> subs(x = -6, eland);

0

Therefore, -6 is a solution to eland = 0. Try other choices for x to see if they are solutions.   

Maple can also solve sets of algebraic equations. In the following we'll use this capability to determine the intersection between two lines. As before, to keep things tidy, we will define variables as abbreviation,

> line_a := y = a*x + c;

line_a := ax + c

> line_b := y = b*x + d;

line_b := y = bx + d

These commands should strike you as a little odd. So far we have used  the assignment operation := to introduce variables as abbreviations for expressions. Here, on the other hand, variables line_a and line_b are abbreviations for entire equations. To determine intersections between the two, we need to solve the equations simultaneously. Maple does this really well:
 

> solve({line_a, line_b}, {x, y});

	      c - d       ad - cb
	x = - ----- , y = -------
	     a - b        a - b
	                     

Easy ! Observe that we have exploited Maple's ability to work with sets to use solve on mutliple equations involving multiple unknowns. Within Maple a set of objects is defined by enclosing the objects in a pair of curly brackets { }. With this in mind, the solve command can be seen to instruct Maple to solve the set of equations {line_a, line_b} for the set of unknowns {x, y}.  

Sometimes the equation of interest may not have a closed-form solution. In such cases, Maple can be used to find numerical solutions. For instance, what if we were interested in finding values of x at which cosh(x) = 2*x. To solve this equation numerically:

> fsolve(cosh(x)=2*x, x);

2.126799893

To check whether this is indeed the solution, find the hyperbolic cosine of this number:

 

> cosh(");

4.253599787

Dividing this result by 2 should return the original number,

 

> "/2;

2.126799894

Clearly, the equation is satisfied.  

Alternately, we can check the validity of the solution graphically. Consider the curve y = cosh(x) and the line y = 2*x. The two intersect at solutions to the equation cosh(x) = 2*x. To check that the intersection indeed coincides with the solution determined previously, we will use Maple to plot the two over the interval -3 to 3. First, the function cosh(x):

> plot(cosh(x), x = -3..3);

Here the two dots .. separating the limits of the plot are not accidental. These are required by Maple syntax.

 

Observe that as in the case of the online help, Maple opens new windows to display graphics. You can close these, as before, by selecting "exit" under the "file" submenu of the graphics window. Next, we proceed to plot the functions simultaneously to examine their intersection,

> plot({cosh(x), 2*x}, x = -3..3);

Observe that the solution determined numerically does match an intersection. In order to touch up the plot, you might want to add a title and axes labels. To do that, enter instead:

> plot({cosh(x), 2*x}, x = -3..3, title = `solution to cosh(x)=2 x`, labels = [`x`, `y`]);

You may run into some trouble at this point as Maple syntax requires titles and labels to be enclosed within quotation marks `, the key for which is in the upper left corner of your keyboard. This differs from the quotation mark ' usually used in text.

Maple has extensive plotting capabilities. For a glimpse of these the Maple picture gallery at NCSU is highly recommended. To keep your expectations from running overboard, on the other hand, an example of the sort of graphics Maple is likely to have trouble with. Here we will consider a single example: a three dimensional plot of the function z = sin(x)*sin(y),

> plot3d(sin(x) * sin(y), x = -4*Pi ..4*Pi, y = -4*Pi..4*Pi);

Observe that the grid used is far too coarse to display the function accurately. Moreover, unlike the two dimensional plots we worked through earlier, the command does not add axes to the plot. We can fix both problems by supplementing plot3d with additional instructions, grid[m, n] to add m and n grid lines along the x and y axes respectively, and axes = FRAME to switch on axes,

> plot3d(sin(x) * sin(y), x = -4*Pi..4*Pi, y = -4*Pi..4*Pi,
grid = [60, 60], axes = FRAME);



Plotting Numerical Solutions of Equations

Occasionally, you might need to plot the numerical solution to an equation.  For example, suppose we wish to plot the  solution to the equation tan(y) + y = x as a function of x. (i.e. for a given x, find the value of y which satisfies the equation; then plot y as a function of  x)
 

We might start by trying to solve the equation for y exactly:

> solution := solve(tan(y) + y - x = 0,y);

No response.  Bummer!  Let's try getting a numerical solution for y with a particular value of x , say x=0.1
 

> sol1 := fsolve(tan(y) + y - 0.1 = 0,y);

2.045571457

OK, that's better.  Now, lets see if we can define a function to plot    
 

> soln := fsolve(tan(y) + y - x = 0,y);

Error, (in fsolve) should use exactly all the indeterminates

This doesn't work, because MAPLE can't apply the fsolve operator without a value for x .    We can fix this by telling  MAPLE to wait until a value is supplied for  x before trying to find a numerical root.
 

> solnfix := (x) -> 'fsolve(tan(y) + y - x = 0,y)';

func := x -> 'fsolve(tan(y) + y - x = 0,y)'

Here the (x) -> tells MAPLE that solnfix is a function of x, and the quotation marks around the fsolve tell MAPLE not to attempt to evaluate the fsolve until the function is called.  Note that you seem to have to type in the full equation that you are trying to solve; if you try to give the equation a name (eqn := tan(y) + y - x = 0)  and then try 'fsolve(eqn,y)' you'll get an error message.   There's probably a way to fix this, but we dont know what it is! 

We can now plot solnfix successfully

> plot(solnfix(x),x=0..10.);

Note that our function could have more than one argument, for example

> solnfix := (x,z) -> 'fsolve( tan(y) + y - z*x = 0,y)';

func := x -> 'fsolve(tan(y) + y - x z = 0,y)'

> plot(solnfix(x,1.0),x=0..10.);

produces the same graph as before.
 

Where does the strange discontinuity around x=8 come from? To check, let's plot the equation that we're trying to solve

> plot(tan(y) + y - 5. ,y=-10..10.);

Our equation evidently has multiple roots, which explains the discontinuity. Let's tell MAPLE that we're only interested in roots between -Pi/2 and +Pi/2

> solnnew := (x) -> 'fsolve( tan(y) + y - x = 0,y,-Pi/2..Pi/2)';

func := x -> 'fsolve(tan(y) +y - x = 0,y,-Pi/2..Pi/2)'

> plot(solnfix(x),x=0..10.);



Calculus

Maple is highly skilled with calculus as well. For instance, differentiation requires the simple command,

 

> diff(x^2, x);

2 x

where x within the diff command specifies differentiation with respect to x. No surprises here. Things get a little more interesting when we use Maple to differentiate the function x*y with respect to x,

 

> diff(x*y,x);

y

Clearly, Maple can carry out partial derivatives as well. Taking this a step further, let's differentiate x*y first with respect to x and then with respect to y,

 

> diff(x*y, x, y);

1

this can be recognized as the partial derivative of x*y, first with respect to x then with respect to y. Maple can carry out integrals as well. To explore this ability, we'll attempt to integrate 2*x,

 

> int(2*x,x);


  2
x
 

which helps us verify both, the integration here, as well as the differentiation performed earlier. We might need to integrate this function between specified limits. Taking the limits to be 0 and 5, for instance,

 

> int(2*x, x = 0..5);

25

Finally, for something a little uglier, we'll integrate the exponential function,

 

> int(exp(-x), x = 0..infinity);

1

Although Maple has an array of predefined functions such as sin(x) and exp(x), occasionally you will find it useful to define your own functions. To define the function sin(t)*exp(-omega*t), for example, proceed as follows:

 

> f(t) := sin(t) * exp(-omega * t);

f(t) := sin(t) exp(-omega t)

With this definition, you have added to Maple's list of functions the new function f(t). In all future operations, it will behave just as any of the predefined functions. For example, to differentiate the function with respect to t you would enter,

 

> diff(f(t), t);

cos(t) exp(-omega t) - sin(t) omega exp(-omega t)

Similarly, integration would require simply,

 

> int(f(t),t);

  exp(- omega t) cos(t)   omega exp(- omega t) sin(t)
- --------------------- - ---------------------------
  2                          2
 omega  + 1                 omega  + 1
	             

Printing

A remaining issue is the question of printing Maple graphics or worksheets. To print an entire worksheet, just click on the worksheet you'd like to print, and then select   click on the `File' option at the top left hand corner of the Maple interface.   A menu will drop down with a `print...' option on it: select this option and enter a file name (the default filename is `untitled.ps').  Hit the Print    button, and a postscript file will appear in your directory.  You can then  send this file to a postscript printer (on the suns, use the lpr command to print a file).
 

To print a single graph, first set up MAPLE so that graphs appear in a separate window instead of in the worksheet. To do this, first click on any MAPLE command line -- you will then see a menu appear at the top of the MAPLE interface that starts with File .. Edit.. View and includes Options. Click on Options, then select `plot display' from the drop down menu that appears, and finally select `Window' (the default configuration is `Inline:' this causes plots to appear in the worksheet). Now, plot your graph in the usual way: you will find the graph appears in a separate window in the MAPLE interface. Click on this window, and then follow the instructions for printing worksheets (File, Print..., etc) that were outlined in the preceding paragraph.

In this tutorial we have considered a range of Maple commands to help you get started with the package. These are, however, only a tiny subset of its capabilities. To proceed from here, depending on your needs, you could either work through some of the more advanced Maple tutorials available on the web (or Maple's own tutorial), or invest in a book on Maple. Once you are comfortable with Maple's basic capabilities, its probably best to aim to be aware of its more advanced aspects rather than attempting to master them all at once, returning to specific aspects only when the need arises.