# M-1: The Maple Mental Model

Our first two pieces of example code, 2+2; and 10^100; show that Maple is at least as powerful as your calculator. We’ll eventually see that Maple can support several kinds of computations:

• exact arithmetic; for example, notice that 10100 was represented exactly
• fixed-precision arithmetic, a.k.a. approximate or floating-point arithmetic
• symbolic manipulation, where Maple performs some algebra on indeterminate variables.

You should not always think of these as hard-and-fast distinctions since many Maple commands will mix ideas from more than one of these paradigms; nonetheless they give you a hint of the different ways to use Maple. Sometimes, you will notice that Maple is doing one thing where you really meant something else, so it helps to know how to give Maple different variations on the same instructions.

# Blueberry, Apple, or Sweet Potato?

Maple has three kinds of pi. This gives an example of comparing fixed-precision and symbolic arithmetic.

• pi is the greek letter pi,
• PI is the capital greek letter pi,
• and Pi is the numerical constant 3.1415926…

This is more of a caveat than anything else, because we’ll only be using Pi for our lessons. Type

> Pi;

Here, we use the > sign only as a hint that we are talking about stuff you would input to Maple. This is because Maple itself uses > to indicate a place for input. But you actually should only type shift-P, i, semicolon, return (and not any greater-than sign).

Notice that the output of the previous command is just the symbol π. Is this symbol really what we wanted? Maple does recognize it as the ratio of a circle’s circumference to its diameter. Check out the result of

> cos(Pi);

and you’ll see that it gives a familiar result.

Note: we’ll be explaining the ; later on. For the time being, type it at the end of every command that you write. This does not mean to write it at the end of every function/procedure call:  abs(cos(Pi);); will not work, rather  abs(cos(Pi)); is the way to go.

What if we want the actual decimal value of π? The evalf() function is used to convert a symbolic/exact number to its decimal representation. (The f in the name stands for floating-point.) Call evalf(Pi); and check out the result.

## Getting  ?help

I once memorized 50 digits of π. But, you may have noticed that evalf() only prints out 10 decimal digits by default (in this case, 1 before the decimal point and 9 afterwards). Is there a way to get help on evalf() so that we can see more digits?

Maple has a built-in help facility to give documentation for its commands. Sometimes it is very helpful (although you should not also hesitate to use Internet search engines in general). To use the built-in Maple documentation, type in a question mark followed by the command name. For example, type in

> ?cos

and you’ll see the help page for trigonometric functions. Close it and then try the following exercise.

Make sure you retain a worksheet with the full calculations you used to answer each exercise; this has to be handed in as a way of showing all your work. We’ll explain more details about the expected format in lesson M-H.

Exercise (nickname: pizero)

Look up the help documentation for evalf. Read it to get some general idea of Maple’s inputs and what evalf does. Then, we specificaly want you to figure out from the help how to evaluate more digits of π. Further down the page, you will see a link to evalf/details. Click on it and read how to increase the precision of a call to evalf. Finally: look for the first 0 in the decimal representation of π, and tell us the decimal digit immediately before the first zero, and after it. As well, tell us the command you used to find it.

Notes:

1. You can delete ?cos from your document after you are done with it. You also can search Maple using the drop-down Help menu.
2. Digits is another way to increase the precision of all floating-point calculations. Don’t use it here, since we don’t know how to set variables yet… although this is about to change.

## := (assignment)

Since Maple is a programming language, it lets you assign values to variables for future use. To do this, write

> «variable name» := «value you want stored»;

For example, write in googol := 10^100; which will store a value in a new variable called googol. (You can call it schmoogol if you want as long as you use the same name consistently.) Then to recall the stored value, type the variable name. It can be used inside of expressions or function calls too. E.g., on the next line, type sqrt(googol); — you will get another number ending in only fifty zeroes.

Your variable names should not contain spaces or hyphens, but you can use the _ character as well as uppercase letters, lowercase letters, and digits. Caveat: you can’t use D as a variable name, since it’s predefined as the differential operator.

To give a purposeful example, let’s say that we want to compute the roots of the mathematical equation $$105x^2-268x-253=0$$. Since we know the quadratic formula, we could literally write down the whole expression

> (-(-268) + sqrt(268*268 - 4*105*(-253))) / (2*105);

but this is cumbersome, and error-prone since we’re typing in the same thing multiple times. Instead, try the following:

> a := 105;
> b := -268;
> c := -253;
> disc := b*b - 4*a*c;
> root1 := (-b + sqrt(disc))/(2*a);

Check that this actually gives you a root by evaluating

> a*root1*root1 + b*root1 + c;

(Maple can actually directly solve quadratic equations, but the purpose of the above discussion was to illustrate using variables.)

It is important to note that this is an assignment statement and not a mathematical equality. For example in math, we know that LHS = RHS always has the same meaning as RHS = LHS. But  LHS := RHS is not the same as RHS := LHS. For example,

> 10^100 := googol;

will result in an error (trying to redefine the numerical constant 10100).

Another important caveat about := is that you can overwrite the value a variable has. So just because you set a := 3; at some point in time, that won’t necessarily stay true forever, since you might overwrite a with a different value later. As an elaborated example, in

a := 3;
b := a;
a := 4;
b;

we set the value of a to 3, then b to 3, then a to 4. Despite that we initially defined b as equal to a, that is not true at the end.

Exercise (nickname: tba)

Simulate the following code on paper, without running it in Maple. Assume that variables a and b are predefined with some numerical values. In a sentence, what is the effect on a and b of the following three lines of code?

t := b;
b := a;
a := t;

Hints:

1. try some examples on paper (e.g., imagine preceding these two lines with a:=10; and b:=99;)
2. to simulate an example, put yourself in Maple’s shoes. What are the values of a, b and t after the first line executes, and then after the second line, etc. Draw a table of how the values change with each step if necessary.

Here is another exercise, which gets more at the difference between symbolic and floating-point calculation.

Exercise

(Not to be handed in.) Define two variables rootTwo := sqrt(2); and floatingRootTwo := evalf(rootTwo);. What is the value of 2 – rootTwo2, and what is the value of 2 – floatingRootTwo2?

## Punctuation’s Revenge

In this section we discuss

• = (equality) and its relatives ><, <=, >=, and <>
• evalb, for evaluating boolean expressions
• ! (factorial)
• # (comments)

Maple indeed has a = symbol, which is a symbolic relational operator. But it is normally not something that you actually mean to use. Type in

> luckyNumber = 7;

and you will notice that the output is just luckyNumber = 7. (Like LaTeX, variable names are italicized by default.) You are not likely to need this kind of relational operator until later when we begin to discuss if statements. However, it has cousins < and > for less-than and greater-than, as well as <=, >= and <> which stand for the mathematical $$\le$$, $$\ge$$ and $$\ne$$.

How can we ask Maple to compare two numbers? Typing 1000 > 99; Maple only echoes back the input as its output. To explicitly ask Maple to evaluate a comparison, we use the evalb() function; here the b stands for boolean, which is the name for something that is either true or false. (On Wikipedia you can read more about booleans and their namesake, George Boole.) Try typing in the following commands:

> evalb(1+1 = 3);       # should be false
> evalb(1000 > 99);     # it's true

Here the pound symbol # is Maple’s way of indicating comments, exactly analogous to the % sign in $$\LaTeX$$.

For the next exercise, you’ll need to know that n! is Maple’s way of writing n factorial. You can use factorial(n) if you want, which is identical except that it looks more like a typical procedure call.

Exercise (nickname: exclaim)

Using evalb() and >, determine the smallest integer whose factorial is greater than 101000 (not a googol, but even bigger, the thousandth power of 10). Use only the tools that have been introduced in this lesson. Hint: there is a sensible way to do this so that you don’t need to type in more than approximately a dozen commands. It’s not important to absolutely minimize the number of commands, but your handed-in work should make clear the process by which you came to the answer.

In working through this exercise, you might find it handy to utilize Maple’s cut-and-paste facilities.

Even more than copying old statements, Maple’s interface allows you to go back and change the previous statements and edit them, and re-execute them by pressing Enter again. But note that Maple has some internal state which gets modified according to each individual command that you send to it by pressing Enter. Maple does not re-run all of your commands each time you make a single edit; it only re-executes the specific ones upon which you press Enter. For a concrete example, let’s say that you run

a := 3;

and then

b := a;

Right now a has value 3 and so does b. Let’s say that you go up and edit the first line to a := 4; and then re-execute it. At this point, you’ve explicitly changed the value of a to 4, but b still has the value 3. Try this: after executing the edited line a := 4; skip straight to the bottom and enter a; and b; to see their values.

As a final caveat, note that Maple does not store the values of all variables currently in memory when you save your worksheet (the .mw file). It’s therefore necessary that your worksheet contains a proper sequence of steps to re-generate everything from scratch, if you want your computations to be reproducible by others. The homework format specifications give some suggestions on accomplishing this in the most easy way.

These are course notes for the University of Waterloo's course Math 600: Mathematical Software.
© 2012—. Written and developed by David Pritchard and Stephen Tosh. Contact (goes to the CEMC)