A Short Introduction to the Maple Language

This section contains an introduction to some of the Maple vocabulary used for solving problems. It is not meant to cover everything, just some of the basics. Read it through quickly, to get an overview of the language. Then you can come back and read with more understanding later.

Arithmetic

First, there is arithmetic: addition, subtraction, multiplication, division and exponentiation. These can be combined, just as on a calculator. The order of precedence is the the usual one: exponentiation first, then multiplication and division, then addition and subtraction. So saying (that is, typing in and pressing the enter key)

> 2-3+4/5*6^7;

[Maple Math]

is the same as saying

> (2-3)+(4/5)*(6^7);

[Maple Math]

You will notice that Maple works with fractions whenever possible, changing to decimal numbers only on demand. So typing and entering (pressing the enter key)

> 1/3 + 1/2;

[Maple Math]

will get a return of 5/6. If you put a decimal point in one of the numbers, that forces Maple to return a decimal answer.

> 1/3. + 1/2;

[Maple Math]

Another way to get decimals is to use the maple word evalf to convert a result to decimal form.

> evalf(1/2+1/3);

[Maple Math]

The name for [Maple Math] , the area of the circle of radius 1, in Maplese is Pi. So to calculate the area of a circle of radius 3, you would say

The Maple constant Digits is set to 10 by default. If you want to more or less digits of accuracy, use evalf with a second argument.

> Pi*3^2;

[Maple Math]

Maple leaves [Maple Math] in its symbolic form unless instructed to convert to floating point. For example, if we wanted the area of our circle to 100 digits of accuracy, we could say

> evalf(Pi*3^2,30);

[Maple Math]

>

>

This command returned the value of [Maple Math] to rounded to 30 digits of accuracy. If you want all of your floating point caluculation to be done with 30 digits of accuracy, set Digits to 30;

> Digits :=30;

[Maple Math]

> sqrt(2) + sqrt(3) = sqrt(2.) + sqrt(13.);

[Maple Math]

>

I usually don't mess with Digits unless I am investigating the accuracy of some numerical method.

> Digits := 10;

[Maple Math]

Maple does arithmetic with complex numbers too. I is a Maple constant standing for [Maple Math] . So entering

> (3+2*I)*(2-I)/(3-I);

[Maple Math]

>

will produce an output of [Maple Math] .

To get the principal square root of 2+I, say

> sqrt(2+I);

[Maple Math]

To express it in the form [Maple Math] , use evalc

> sqrt(2+I)=evalc(sqrt(2+I));

[Maple Math]

Complex numbers first come up when you solve quadratic equations.

>

> solve(3*x^2+2*x +1 ,x);

[Maple Math]

>

Expressions, Names, Statements, and Assignments

Quantities to be computed like 1/2+1/3 are called expressions .

A name is a string of characters which can be used to store the result of a computation.

A statement in Maple is a string of names and expressions terminated with a semicolon, or a colon if you don't want to see the output, which when entered will produce some action.

The assignment statement is one of the most common statements. It is of the form

name := expression; For example, the assignment

> area := Pi*3^2;

[Maple Math]

stores [Maple Math] in a location marked by the name area.

A more useful assignment for the area of a circle is

> area := Pi*r^2;

[Maple Math]

In this case, the expression [Maple Math] is stored in area and with this assignment, the area of a circle of any given radius can be computed using the Maple word subs . So to calculate the area when r is 3, we enter

> subs(r=3,area);

[Maple Math]

Here, it is convenient to think of the assignment as defining area as a function of the radius r.

Functions

A function is a rule f (possibly very complicated) for assigning to each argument x in a given set, a unique value f(x) in a set. In calculus the arguments and values of a function are always real numbers, but the notion of function is much more flexible than that.

Functions can be defined in several useful ways in Maple.

As an expression: The assignment

> area := Pi*r^2;

[Maple Math]

defines the area of a circle as a function of it's radius. The area function defined as an expression is evaluated with subs. Since this function assigns real numbers to real numbers, its values can be plotted on a graph with the Maple word plot. So the statement

> plot(area,r=0..4);

[Maple Plot]

will produce in a separate plot window, the graph of the area function over the interval from [Maple Math] to [Maple Math] .

With the arrow operator the assignment: If you have a simple function, you can often use the arrow operator . For example,

> area := r -> Pi*r^2;

[Maple Math]

defines the area function also. Now to find the area of a circle of radius 3, we simply enter the statement

> area(3);

[Maple Math]

To plot this function over the domain r = 0..4 , say

> plot(area,0..4);

[Maple Plot]

Note that the variable r is omitted here.

Use unapply . The ugly little word unapply transforms expressions of one or more variables into fuctions defined by an arrow operator. For example, if we had a polynomial defined by the assignment

> pol := x^2 + 4*x -1;

[Maple Math]

then the assignment

> pol := unapply(pol,x);

[Maple Math]

turns pol into a function defined by an arrow operator.

As a procedure : The Maple word proc can be used to define functions. For example,

> area := proc(r) Pi*r^2 end;

[Maple Math]

defines the area function too. It is evaluated and plotted as in the arrow operator definition. One advantage of this way of defining a function is that the domain can be specified. For example, the domain of the area function for a circle is all positive real numbers. This can be inserted into the procedure, with the Maple word ERROR . The message must be enclosed in backquotes ` or doublequotes ", which is on the key with the tilde .

> area := proc(r)

> if r <= 0 then ERROR("radius must be positive") else

> Pi*r^2 fi end;

[Maple Math]

> area(3);

[Maple Math]

> area(-3);

Error, (in area) radius must be positive

Note the if..then..fi control statement here. You can learn more about the word if by typing ?if in an input cell and entering it.

Functions of two variables can be defined and plotted just as easily in Maple as functions of one variable. For example, the volume V of a cylinder of height h and radius r is defined by

> V := (r,h) -> Pi*r^2*h;

[Maple Math]

To see what the graph of V looks like, use plot3d .

> plot3d(V,0..4,0..4,axes=boxed);

[Maple Plot]

Which way of defining a function is the preferred way? That really depends on the situation. The expression method works well for functions which have only one rule of evaluation, but eventually you cannot avoid using an -> or proc definition. You will find yourself using arrow or proc definitions more and more as time goes by.

Piecewise defined functions:

Many functions can only be described by stating various rules for various parts of the domain. The Maple word piecewise will help with defining such functions. Here is an example to show usage.

> f :=unapply(piecewise(x <= -1,x^3+8, x <= 2,7+ 2*x, x <= 4, 11 - cos(x),3*x),x);

[Maple Math]

> f(2);

[Maple Math]

When plotting piecewise defined functions, sometimes style = point is better.

> plot(f, -3..6,style= point);

[Maple Plot]

You can also plot discontinuous functions by setting discont = true.

> plot(f, -3..6,discont=true,color=blue);

[Maple Plot]

>

>

Composition of functions

One of the important skills needed for calculus and other mathematics is the skill to see a function as built up from simpler functions by the 4 arithmetic operations plus composition. In Maple these operations are named by +, *, -, /, and @. So for example the function [Maple Math] can be seen as the quotient of g (x) = [Maple Math] by h(x) = [Maple Math] .

>

> g := x-> x^2 +1; h := x->sqrt(x^3-1);

[Maple Math]

[Maple Math]

> f := g/h;

[Maple Math]

> f(x);

[Maple Math]

This analysis of a function into simpler functions is not unique and can often be extended further. For example, the denominator function h is seen to be the composition of the square root function [Maple Math] with the cubic polynomial function [Maple Math] .

> k := x-> x^3 -1;

[Maple Math]

> f := g/ (sqrt @ k) ;

[Maple Math]

> f(x);

[Maple Math]

Problem: Define three functions g,h, and k so that the function f defined by [Maple Math] is [Maple Math]

Built in Maple functions and Operations with Functions

All of the standard scientific functions are built into Maple. For example, sqrt is the square root function, abs is the absolute value function, the trig and inverse trig functions are sin , arcsin , cos , etc., the natural logarithm and exponential functions are ln and exp . For a complete list of built in functions, type

> ?inifcns;

New functions can be obtained from old functions by use of the arithmetic operations of addition, subtraction, multiplication, and division together with the operation of composition, which is denoted by @ . Thus the function defined by the assignment

> y := sin(cos(x^2+3));

[Maple Math]

and evaluated at x=3 by

> subs(x=3.,y);

[Maple Math]

could also be defined by the assignment

> y := sin@cos@(x->x^2+3);

[Maple Math]

and evaluated at x=3 by

> y(3.);

[Maple Math]

Using Maple as a fancy graphing calculator.

It is convenient to think of Maple as a fancy graphing calculator for many purposes. For example, suppose you want to find the real solutions of the equation [Maple Math] in the interval [Maple Math] . Then we can just plot the right hand side of the equation and look for where the graph crosses the x-axis.

> f := x -> 10*x^5 - 30*x +10 ;

[Maple Math]

> plot(f,-3..3);

[Maple Plot]

By inspection, the graph crosses near 0. We can look closer.

> plot(f,-1.5..1.5);

[Maple Plot]

>

We see that the graph crosses 3 times, the largest solution being between 1 and 1.5. If we wanted the largest solution more accurately, we could use fsolve . Note the syntax. There are three arguments, the equation to solve, the variable to solve for, and the interval in which to search for a solution.

> fsolve(f(x)=0,x,1..1.5);

[Maple Math]

Data types, Expression Sequences, Lists, Sets, Arrays, Tables

Maple expressions are classified into various data types . For example, arithmetic expressions are classified by whether they are sums type '+' , products type '*' , etc.

The Maple word whattype will tell what type a particular expression is.

> whattype(1/2);

[Maple Math]

> whattype(a + b);

[Maple Math]

> whattype(x^2 + x = 2*x - 1);

[Maple Math]

> whattype(a,b,3);

[Maple Math]

Expression Sequence.

An exprseq , expression sequence, is any sequence of expressions separated by commas. For example,

> viola := 1,2, w*r+m, a=b+c, 1/2, (x+y)/z,`hello`;

[Maple Math]

is an assignment to viola of an expression sequence of 7 expressions. To refer to the sixth expression in this sequence, use the expression viola[6];

> viola[6];

[Maple Math]

List .

A list is an expression sequence enclosed by square brackets. So

> explist:= [viola];

[Maple Math]

makes a list whose terms are those in viola . As with expression sequences, we can refer to particular terms of a list by appending to its name the number of the term enclosed in square brackets. Thus to get the fifth term of explist , type the expression

> explist[3];

[Maple Math]

You can also reference the fifth term in this list by by using the Maple word op .

> op(3,explist);

[Maple Math]

In general, op(n,explist); returns the nth term in the list explist .

To count how many terms are in a list, use the word nops . So for example,

> nops(explist);

[Maple Math]

tells us that there are 7 terms in the list explist . nops comes in handy when you

don't want to (or aren't able to) count the terms in a list by hand.

You can't directly use the word nops to count the number of terms in an expression sequence. But you can put square brackets around the expression sequence and count the terms in the resulting list. This device is used again and again.

> nops(3,4,a);

Error, wrong number (or type) of parameters in function nops

> nops([3,4,a]);

[Maple Math]

A point in the plane is a list of two numbers. Points can be added and subtracted and multiplied by a number.

> p := [1,2]; q := [-3,1];

[Maple Math]

[Maple Math]

> w := 3*p + 2*q - p;

[Maple Math]

One important use of lists is to make lists of points to plot. For example, to draw a picture of the square with vertices (1,1), (3,1), (3,3), (1,3), make a list and then plot it.

> ab := [[1,1],[3,1],[3,3],[1,3],[1,1]];

[Maple Math]

> plot(ab);

[Maple Plot]

Notice in the graph that the origin is not included in the field of view. We can specify that by restricting the x and y coordinates.

> plot(ab,x=0..4,y=0..4);

[Maple Plot]

Another use of lists is with parametric plots . If you have a curve in the plane described parametrically with [Maple Math] , [Maple Math] , as the parameter t runs from a to b, then you can draw it by making up a 3 term list to give to plot. Say you wanted to draw the upper half of the circle of radius 4 centered at (1,5). Then the list consists of the expressions for the x and y coordinates followed by an equation giving the range of the parameter.

> plot([1+4*cos(t),5+4*sin(t),t=0..Pi],
scaling=constrained);

[Maple Plot]

If you had to draw several pieces of circles, you might define a function to simplify things. You can call the function whatever you want, say circ.

> circ := (h,k,r,f,l) -> [h+r*cos(t),k+r*sin(t),t=f..l];

[Maple Math]

So if we wanted circles of radius 1/2 centered at the corners of the square ab we can construct the sequence of lists. Here is where the Maple word seq comes in handy.

> circs := seq(circ(op(ab[i]), 1/2,0,2*Pi),i=1..4);

[Maple Math]
[Maple Math]

In order to plot these circles, you need to enclose them in curly brackets to make a set of the sequence before you give them to plot . See below for a discussion of sets.

> plot({circs,ab},scaling=constrained);

[Maple Plot]

Sometime you might want to split a list of points to plot into a list of x-coordinates and another list of ycoordinates. The Maple word seq is very handy for this and many other operations. So to split off from ab the odd and even terms--

> xdat := [ seq(ab[i][1],i=1..nops(ab) )];

[Maple Math]

> ydat := [seq(ab[i][2],i=1..nops(ab) )];

[Maple Math]

What about the converse problem? Building up a list of points to plot from two lists can also be done. The first thing you might think of doesn't work, however.

> seq([xdat[i],ydat[i]],i=1..nops(xdat));

[Maple Math]

Seq doesn't work well with a pure expression sequence as input. However, with some coaxing we can get it to do what we want.

> newab :=[seq([xdat[i],ydat[i]],i=1..nops(xdat))];

[Maple Math]

What did we do to change the input to seq ? We enclosed it in square brackets. If you feed such a list of points to plot, it knows what to do. If you wanted to strip out the inside brackets, that can be done too, but in release 4 of Maple, plot would treat it as a sequence of constant functions.

> newab := [seq(op([xdat[i],ydat[i]]),i=1..nops(xdat))];

[Maple Math]

> plot(newab,color=black);

[Maple Plot]

>

Sets

A set is an expression sequence enclosed by curly brackets. This is much different from a list. For one thing, the order in which you specify the members of a set may not be the order in which they are stored. Also each member of the set is only stored once, no matter how many times you list it.

> Aset := {y+x+1,1,2,1,4,`bill`,x+y+1,`bill`};

[Maple Math]

The set operations of union , intersection , and minus are at your beck and call.

> Anotherset := Aset union {4,3,a,7} ;

[Maple Math]

> Anotherset minus Aset, Anotherset intersect Aset;

[Maple Math]

Sets are important when plotting more than one function at at time, to plot the quadratic function [Maple Math] and the linear function [Maple Math] on the same axes,

> plot({x^2-2,2*x+5},x=-5..5);

[Maple Plot]

>

plots the parabola [Maple Math] and the line [Maple Math] over the domain [Maple Math] on the same graph. If you have a very complicated drawing to make, you can use plots[display] from the plots package. Just give names to the plots you want to display and then display the list of plots you have named.

> pl1 := plot({x^2-2,2*x+5},x=-5..5):

> pl2 := plot([[2,1],[3,20],[0,0],[2,1]]):

> plots[display]([pl1,pl2]);

[Maple Plot]

>

Tables and Arrays

A table is a special kind of data structure which is very flexible. The packages of special vocabularies are really tables whose indices of the package are the names of the procedures and whose entries are the bodies of the procedures. We do not make much use of tables in this handbook, except for arrays.

An array is a special kind of table whose indices are numerical. Somet useful arrays are matrices (2 dimensional arrays) and vectors (1 dimensional arrays).

Matrix operations are made using Maple word evalm together with the symbol for matrix multiplication &* .

> a := array(1..2,1..2);

[Maple Math]

>

creates a 2 by 2 matrix, whose entries are accessed as a[1,1] etc.

So to rotate the square [Maple Math] through an angle of 31 degrees counter clockwise about the origin and display it, we could proceed as follows.

> rot := array([[cos,-sin],[sin,cos]]);

[Maple Math]

> ang := evalf(Pi/180*31);

[Maple Math]

> ab := [[1,1],[3,1],[3,3],[1,3],[1,1]];

[Maple Math]

> rotab := [seq(convert( evalm(rot(ang)&*ab[i]),list) ,i=1..nops(ab) )];

[Maple Math]
[Maple Math]

> plot({ [[0,0]],ab,rotab} );

[Maple Plot]

Maple control statements

There are two especially important control statements . One is the repetition loop , and the other is the conditional execution statement. The repetition loop is

for .. from .. by .. to .. while .. do .. od

and the conditional execution statement is

if ... then ... elif ... else ... fi

These statements can be used interactively or in a procedure.

Example: Add up the first 10000 numbers.

> s := 0: for i from 1 to 10000 do s := s+i od: s;

[Maple Math]

Note: The Maple word sum could be used here instead.

> i := 'i': sum(i,i=1..10000);

[Maple Math]

> i;

[Maple Math]

Note: This could also be computed using convert and seq .

> convert([seq(i,i=1..10000)],`+`);

[Maple Math]

Example: Add up the odd numbers between 1 and 973.

> s := 0: for i from 1 by 2 to 973 do s := s + i od: s;

[Maple Math]

Example: Add up the prime numbers between 1 and 10000

> s := 0: for i from 1 to 10000 do if isprime(i) then s := s+i fi od: s;

[Maple Math]

Example: Add up the first 1000 primes.

[Maple Math]

> s := 0: j := 0: for i from 1 by 2 while j <1000 do if isprime(i) then
s := s+i; j:= j+1 fi od: s;

[Maple Math]

>

Example: Compute the cubes of the first five positive integers and store them in a list. Then do it again, storing them in an array.

Solution with lists:

> locube := NULL: # start with the empty exprseq
for i from 1 to 5 do
locube := locube ,i^3 od:
locube := [locube]; # make locube a list.;

[Maple Math]

> locube[3]:=15;

[Maple Math]

> locube;

[Maple Math]

Note the way the list is built up from an empty exprseq NULL . Each time through the loop, one more term is added onto the end of the sequence. At the end, square brackets are put around the sequence, making it a list. With arrays, one can be more direct.

Solution with arrays:

> aocube := array(1..5): # initialize the array.

> for i from 1 to 5 do aocube[i]:= i^3 od;

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Math]

> op(aocube); # to see the array

[Maple Math]

>

Now the array aocube has the numbers stored in it. To refer to the third element of aocube , we would enter aocube[3] just as if it were a list, rather than an array.

> aocube[3]:=0;

[Maple Math]

> print(aocube);

[Maple Math]

> locube := [locube[1],locube[2],0,locube[4],locube[5]];

[Maple Math]

>

Conditional execution

if .. then .. elif .. else .. fi;

There are lots of times when you need to consider cases, and they can all be handled with the if .. then .. elif .. else .. fi; statement. For example, many functions are defined piecewise. The absolute value function abs is such a function.

Problem: Define your own version of the absolute value function.

A solution:

> myabs := proc(x) if x > 0 then x else -x fi end;

[Maple Math]

> myabs(-23);

[Maple Math]

> plot(myabs,-2..2,scaling=constrained,title=`my absolute value`); # to see what it looks like.

[Maple Plot]

A Brief Vocabulary of Maple Words

Here are some Maple words useful in mathematical problem solving, together with examples of their usage. For more information on these words and others, look at the helpsheets and use the help browser.

> y := (x+3)/tan(x^2-1); # use 'colon-equal' to make assignments.

[Maple Math]

> collect(x*2 + 4*x,x); # collects like powers of x.

[Maple Math]

> diff(cos(x),x); # calculates the derivative

[Maple Math]

> D(cos); # the differential operator

[Maple Math]

> y := denom((a+b)/(e+f)); # assigns e+f to y.

[Maple Math]

> y := 'y'; # makes y a variable again.

[Maple Math]

> evalc((2+3*I)^3); # performs complex arithmetic

[Maple Math]

> evalf(1/2^9); #evaluates 1/2^9 to a decimal number

[Maple Math]

> expand((x+b)^7); # expands the product

[Maple Math]

> p := x^2+5*x+6; # assigns the quadratic to p.

[Maple Math]

> factor(p); # factors the polynomial

[Maple Math]

> fsolve(x^5-3*x=1,x,0..2); # solve eqn for x in 0..2

[Maple Math]

> int(x*exp(x),x); # returns an antiderivative.

[Maple Math]

> Int(x*exp(x),x=0..1); # A passive integral.

[Maple Math]

> map(x->x^2,[1,3,2,5]); # returns a list of squares.

[Maple Math]

> nops([3,4,x,1]); # returns the number of terms in the list.

[Maple Math]

> numer((a+b)/c); # gives numerator, here a+b

[Maple Math]

> op([3,4,1,x]); # strips the brackets off the list

[Maple Math]

> plot(x^2+x, x=-3..3); # plots x^2+x as x goes from -3 to 3.

[Maple Plot]

> plot3d(x^2+y,x=-2..2,y=0..2); # plots a surface

[Maple Plot]

> f := x -> x^2; # defines the squaring function.

[Maple Math]

> f(3); # then returns 9.

[Maple Math]

> quo((x^4-4),(x^2-2),x); # divides polynomials

[Maple Math]

> iquo(23,2) ; # divides the integers

[Maple Math]

> rem((x^4-4*x+3),(x^2-2),x); # gives the remainder

[Maple Math]

> irem(23,2) ; # gives the integer remainder

[Maple Math]

> restart; # very handy. This word resets all assignments.

> eq1 := x^2 + 3*x -1 = a; # assigns the equation

[Maple Math]

> rhs(eq1); # yields the righthand side of eq1. There is also an lhs.

[Maple Math]

> simplify(a/x+b/y); # sometimes simplifies expr.

[Maple Math]

> solve(a*x+4*y=0,x); # solve the equation for x.

[Maple Math]

> subs(x=5,x^2+x); # substitute 5 for x where it occurs in x^2+x.

[Maple Math]

> i := 'i'; # makes i a variable again

[Maple Math]

> sum((i^2,i=2..9)); # add up the 2nd thru 9th squares

[Maple Math]

>

Defining your own words

Maple's mathematical vocabulary is extensive, but it can never be complete. That's because we are always discovering new ways to do things. The methods can then be defined in a Maple procedure.

One way to develop the definition is in a series of input cells: For example, suppose we wanted to define a word to solve a quadratic equation a [Maple Math] . We want the inputs to be the coefficients a,b,c of the equation, and the output to be the roots and a message describing the natrue of the roots. So given the inputs values in an input cell. Now develop the body of the procedure in an input cell below it. Thus the nature of the roots is determined by the discriminant [Maple Math] , so we need to use a conditional statement here.

> a := 3 : b:= 0: c := 5:

> if b^2 - 4*a*c < 0 then print("negative discriminant. no real roots.")
elif b^2 - 4*a*c = 0 then print("discriminant is 0. 1 real root.");
(-b +sqrt(b^2-4*a*c))/(2*a)
else print("discriminant is positive, two real roots.");
(-b +sqrt(b^2-4*a*c))/(2*a),(-b -sqrt(b^2-4*a*c))/(2*a) fi;

[Maple Math]

After changing the values of a, b, and c in the input cell and testing the body of the procudure, we can define the procedure in an input cell. Use the copy and paste item in the edit menu to put the body into the cell below then at the top put the 'proc line' and at the bottom put the 'end line'.

> quadroots := proc(a,b,c) # the proc line
if b^2 - 4*a*c < 0 then print("negative discriminant. no real roots.")
elif b^2 - 4*a*c = 0 then print("discriminant is 0. 1 real root.");
(-b +sqrt(b^2-4*a*c))/(2*a)
else print("discriminant is positive, two real roots.");
(-b +sqrt(b^2-4*a*c))/(2*a),(-b -sqrt(b^2-4*a*c))/(2*a) fi;
end; # the end line

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

Now, test the procedure again

> quadroots(2,3,4);

[Maple Math]

> quadroots(2,sqrt(32),4);

[Maple Math]

[Maple Math]

> quadroots(2,10,4);

[Maple Math]

[Maple Math]

At this point, you have added a word to the Maple language. It can be used just like any of the other words. It is not a permanent addition, however. The next time you run Maple, it won't be there, unless you make some special effort.

One way to keep all your procedures together is to put them into a "package". A package in Maple is a table whose indices are names of procedures and whose entries are the bodies of those procedures. For example, in this worksheet, we have defined two procedures that we might like to keep in a package, quadroots and one further back myabs.

We could call the package 'mystuff'.

> mystuff := table([]);

[Maple Math]
[Maple Math]

> mystuff[quadroots]:= proc(a,b,c) # the proc line
if b^2 - 4*a*c < 0 then print("negative discriminant. no real roots.")
elif b^2 - 4*a*c = 0 then print("discriminant is 0. 1 real root.");
(-b +sqrt(b^2-4*a*c))/(2*a)
else print("discriminant is positive, two real roots.");
(-b +sqrt(b^2-4*a*c))/(2*a),(-b -sqrt(b^2-4*a*c))/(2*a) fi;
end: # the end line
mystuff[myabs]:= proc(x) if x > 0 then x else -x fi end:
with(mystuff);

[Maple Math]

Now in order to use the procedures in the 'mystuff' package, load a worksheet containing the package and execute the cell containing the definitions. Include the line with(mystuff) at the bottom to complete the loading process.

Using the Packages that come with Maple

There are many packages that come with Maple, for example, plots and plottools are packages of words used to draw pictures, student is a package of words for calculus students, numtheory is a package of number theory words, linalg a package of linear algebra words, geometry a package of words for analytic geometry, and so on. To see the words in a package and at the same time make them usable, load the package using with

> with(plots);

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

Just like the regular vocabulary, these words all have help pages too. A good strategy when you have a task of some sort to do is to scan for words which might help you complete the task.

Example: Draw a blue ball and a red cone.

A solution. There might be some words in the plottools package to help here.

> with(plottools);

[Maple Math]
[Maple Math]
[Maple Math]

Yep. cone and sphere are the words we are looking for. What is their syntax? we can type

> example(cone);

This brings up the helpsheet for cone and goes to the examples section. copy the example and paste it into your worksheet.

> icecream := cone([0,0,-1],0.7,color=gold), sphere([0,0,0.1],0.6,color=brown):
plots[display](icecream, scaling=constrained, style=patch);

[Maple Plot]

This does not solve our problem, but we can modify it to solve the problem. All we have to do is change colors.

> icecream := cone([0,0,-1],0.7,color=red), sphere([0,0,.1],0.6,color=blue):
plots[display](icecream, scaling=constrained);

[Maple Plot]

Note the use of the word plots[display]. This is used to display several plots in the same picture. By using it as plots[display] instead of in the form display, it is not necessary to have loaded the plots package into vocabulary in order to use it. (If you say display(icecream) and have not loaded the plots package by saying with(plots) first, the output will be the input 'display(icecream)'.)

>

The linalg package

The linalg package of word is one of the most useful in Maple. To load the package, use with(linalg);

> with(linalg);

Warning, new definition for norm

Warning, new definition for trace

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

Most of the algorithms in Ma 322 (as well as many that aren't) can be found in this list. For example, the dot product of two vectors can be computed using the word linalg[dotprod]. If you have loaded in the linalg package in a worksheet, you can use the short name dotprod .

> dotprod([2,1,3],[4,2,1]);

[Maple Math]

A vector can be defined in maple as a list or as a vector

> A := [3,2,3];

[Maple Math]

> B := vector([3,2,1]);

[Maple Math]

> dotprod(A,B);

[Maple Math]

To get the angle between two vectors, use linalg[angle]. For example, if I want to know the angle between A and A + 2*B

> ang:= angle(A,A+2*B) ;

[Maple Math]

To convert the angle to a decimal number use evalf

> evalf(ang);

[Maple Math]

To convert the angle to degrees, we can multiple by 180/Pi

> evalf(180/Pi*angle(A,A+2*B));

[Maple Math]

A matrix is entered row by row. So, for example to define the 2 by 3 matrix whose top row is [3,1,5] and whose bottom row is [6,5,4]

> C := matrix(2,3,[3,1,5,6,5,4]);

[Maple Math]

To get the norm of a vector, take the square root of the dotproduct of the vector with itself.

> sqrt(dotprod(A,A));

[Maple Math]

Randmatrix is used to generate a matrix with some random entries.

> E:= randmatrix(2,3);

[Maple Math]

To add two matrices of the same size use matadd or &+ (together with evalm )

> matadd(C,E);

[Maple Math]

> evalm(C &+ E);

[Maple Math]

Row operations on matrices are important in linear algebra. One particular one is to add a scalar multiple of one row of a matrix to another row of that matrix. So, to add 5 times the first row of E above to the second row, use addrow

> addrow(E,1,2,5);

[Maple Math]

>

Matrix multiplication is especially important operation also. Use multiply or &* (together with evalm ) to do that

> F := matrix(3,2,[1,2,4,5,0,-3]);

[Maple Math]

> multiply(E,F);

[Maple Math]

> evalm(E &* F);

[Maple Math]

The best way to learn to use the words in linalg is to work problems in linear algebra. Over the course of a semester, you can become fairly proficient at using linalg to do (or check) the computation you need to do.

Making Movies

A movie (or animation) is a sequence of frames displayed one after the other. A well conceived animation can be of great use in gaining an understanding of some phenomena. They are also fun to make. Here is an example:

Example: Have a blue ball move up out of a red cone.

A solution: We can start with the picture we drew before

> icecream := cone([0,0,-1],0.7,color=red), sphere([0,0,.1],0.6,color=blue):
plots[display](icecream, scaling=constrained);

[Maple Plot]

This is the first frame of our movie. What has to change in order for the ball to rise up out of the cone? the z-coordinate of the center of the ball. If we define our frame like so

> frame := t ->
plots[display](cone([0,0,-1],0.7,color=red), sphere([0,0,t],0.6,color=blue), scaling=constrained);

[Maple Math]

> frame(2);

[Maple Plot]

>

Now in order to see the ball clearly we have to change the orientation and put in some axes.

> frame := t ->
plots[display](cone([0,0,-1],0.7,color=red), sphere([0,0,t],0.6,color=blue), scaling=constrained,axes=boxed,orientation=[50,70]);

[Maple Math]
[Maple Math]

> frame(2);

[Maple Plot]

Now we can make a movie using plots[display] with the option insequence=true

> plots[display](seq(frame(i/2),i=1..5),insequence=true,scaling=constrained);

[Maple Plot]

>

To run the animation, activate the plot and press the play button.

Trouble Shooting Notes

Learning to use Maple can be an extremely frustrating experience, if you let it. There are some types of errors which occur from the beginning that can be spotted and corrected easily by a person fluent in Maple, so if you have access to such a person, use him or her.

Here are a few suggestions that may be of use when you're stuck with a worksheet that's not working like it should.

Important! Loading an old Maple worksheet does not excute it. This is something you must do separately, either one cell at a time or all at once.

Also important! Remember that you can rexecute any cell in the worksheet at any time. This can lead to confusion about what you did last, because it may not be what is just above.

Use help: There is a help sheet with examples for every Maple word. A quick read thru will often clear up syntax problems. One very common early mistake is to leave out the parentheses around the inputs of a word. For example, typing

> plot x^2;

Syntax error, missing operator or `;`

will get you a syntax error, because you left out the parentheses.

>

The maple prompt is `>` . You can begin entering input after it. Make sure you are typing into an input cell, if you are expecting output.

End maple statements with a semicolon `;` or colon `:` . Maple suppresses output when the command line ends with a colon. Maple does not process until it comes to a (semi)colon. If you are getting no output when you should be, try feeding in a semicolon. This often works.

> p := expand((a+b)^5);

[Maple Math]

> p;

[Maple Math]

> q := expand((1+x)^9):

> q;

[Maple Math]

>

When in doubt, put in parentheses. For example, (x+3)/(x-3) is very different from x+3 / x-3 .

> (x+3)/(x-3),x+3 / x-3 ;

[Maple Math]

>

Make sure your variables are variable . You may have assigned a value, say 3, to x in a previous problem. To make x a variable again, type x := 'x': . Use the forward quote ' key, just below the double quote " here. If you forget this, strange things might happen. One way to handle this is to keep an input cell of variables used.

> x := 3;

[Maple Math]

> x := 'x';

[Maple Math]

>

Use restart; By typing restart; in an input cell and pressing enter, you clear all assignments, and start with a clean slate. This fixes a lot of problems fast, but you will need to re-execute input cells.

> hello := yes;

[Maple Math]

> hello;

[Maple Math]

> restart;

> hello;

[Maple Math]

Are you using the correct quote symbol? In Maple, the forward quote ' is used to suppress evaluation.
The
back quote ` is used to form names. The double quote " is used to define ascii strings.

> `this is a long name` := 12;

[Maple Math]

> `this is a long name`;

[Maple Math]

> check := "do not worry";

[Maple Math]

> check;

[Maple Math]

Do not forget to end loops with od , `if` statements with fi , and procedures with end . If you start a loop with do , Maple does not begin processing until it finds the end of the loop, which is signaled by the word od ; The same applies to the if .. then ... fi; and proc ... end; contructions. If you are getting no output when you should be, try feeding an od; , fi; , or end; This often works.

>

>

Unwanted output?: Is there output you need but don't want to see? Use a colon `:` instead of a semicolon to end the Maple statement which generates the output.

Use printlevel := 10; if you want to see what Maple is doing behind the scenes when you give it a command. If you want to see more, use printlevel := 50 or higher. Often by inspecting the output when printlevel is greater than 1 (the default), you can discover what is ailing your worksheet.

> unapply(x^2-100,x);

[Maple Math]

> printlevel:= 10;

[Maple Math]

>

> unapply(x^2-200,x);

{--> enter unapply, args = x^2-200, x

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Math]

<-- exit unapply (now at top level) = proc (x) options operator, arrow; x^2-200 end}

[Maple Math]

> printlevel:=1;

[Maple Math]

Are all words you are using defined? Have you executed the input cells containing the procedures you need? Have you loaded the packages containing the words you need? Undefined words are simply returned by Maple. Hint: When using words from packages in procedure definitions, use the full name, e.g, use plots[display] instead of display.

> thisword := proc(x) x^2 + 10 end;

[Maple Math]

> thisword(3);

[Maple Math]

> restart;

> thisword(3);

[Maple Math]

>

Use debug . If you hav e defined a word, say ` thisword ` and it does not do what you want, you can often discover the error by typing debug(something); in an input cell and pressing the enter key. When you use the word again, its behind the scene computations are printed out for your inspection.

> debug(thisword);

[Maple Math]

> thisword(3);

{--> enter thisword, args = 3

[Maple Math]

<-- exit thisword (now at top level) = 19}

[Maple Math]

> undebug(thisword);

[Maple Math]

> thisword(3);

[Maple Math]

>

Want to see a word definition? Say you want to see how plot works. Type interface(verboseproc=2); in an input cell and press enter. Then type print(plot);

> interface(verboseproc=2);

> print(plot):

[Maple Math]

>