# Activity 2: Getting started with Maple

Maple is a powerful program for doing all kinds of math. It is to be found in the ``Mathematics and Statistics"' folder on UCS Macintoshes. Open the folder, and double click on the Maple icon. We will be using Maple to do two things: basic computation and plotting. You should try typing in all the examples given here, and make up your own, to make sure you know what is going on.

## Basic computation

At the Maple prompt (the ``>'' sign) type in the following:
```> 2 + 2 ;
```
4

Note the semicolon! This tells Maple that you have finished entering things and would like it to give you an answer. When you hit `Enter' (the key at the lower-right corner of the keyboard) you should see a 4 on the next line, and a new prompt looking for more input. Maple will do all of the simple forms of computation you are familiar with and much more besides. Here are some examples (everything after a # is a comment. You do not have to type it, and if you do, it will not be read in by Maple):
```> 10 - 5 ;  # 10 minus 5
> 25.5 * 4 ; # * means multiply
> 25.5 / 4 ; # / means divide
> 2^4 ; # 2*2*2*2, or 2 to the power of 4
```
You can group expressions with parentheses, just as you do in ordinary math notation:
```> (284 + 44) / 88 ;
```
41/11

Note that the result of the last computation is a rational number (a fraction): We will see how to change this to a real number (decimal number) a little later. Maple cares a great deal about whether you forget a character such as ``;'' or ``or spell a word wrong, but it does not mind extra spaces inserted between words or characters (though not within words of course). So following expressions are treated the same:
```> (284   +   44) / 88 ;
> ( 284+44 )/88;
```

### Assigning a value to a variable

Often, we will want to save the result of some computation in a form that is easy to refer to. If we do this:
```> a := (284 + 44) / 88 ;
```
a := 41/22

then the variable a now has the value > a ;
Here is another example:
```> q := 9 ;
```
q := 9

```> q + 3 ;
```
12

We may want Maple to evaluate a, that is, to tell us what the decimal value of is. We can do this by typing:
```> evalf(a) ;
```
3.7272727272

Maple will also calculate functions. It already knows common functions such as sine, cosine, and square root (sqrt). You will read more about functions below.
```> sin(1.2) ;
```
.9320390860

```> sqrt(2) ;
```

2 to the power of a half is the same thing as square-root of two. A handy way to get Maple to evaluate the last expression is as follows:
```> evalf(") ;
```
1.414213562

The " refers to the last expression Maple evaluated. Thus evalf(") in this case is the same thing as evalf(sqrt(2)). The expression before the last one is "", etc. Maple also knows most common constants such as (enter this as Pi) and e (enter as E). You can use evalf() to get numerical values for these:
```> E ;
```
E

```>evalf(") ;
```
2.718281828

## Exercises 1

Throughout this tutorial, you will be asked to perform some calculations yourself. When you enter these into Maple, you should copy and paste the input into a text file which you save in your student locker account. When you are done, you should email me the file. Details on editing a text file, saving it in your locker account, and including it in an email message will be given in lab. To allow the Macintosh to cut and paste from a Maple session, you must open the ``Format'' menu, choose ``Output'', and from that menu, choose ``Character''. In future, this sort of choice will be written simply as format->output->character.

Exercises are numbered. In order to be sure of getting a grade of B, you must do correctly all those without a star. Exercises numbered with a star (e.g. 1.4*) are somewhat harder (though they should be do-able). Doing these in addition will head you towards an A or A+. All exercises should be completed and mailed to me by Monday, Sept. 18. A grade of F for this activity will be awarded to all submissions thereafter. Perform the following calculations. Note that we are not using Maple syntax; that is, you will have to translate the English into appropriate Maple expressions in order to do the calculations.

• 1.1: the square root of 1849
• 1.2: 1.384 multiplied by 48.9
• 1.3: to the power of 3 (use evalf() to get a numerical value)
• 1.4*: the product of the cosine (cos) of and the tangent (tan) of /8
Get the symbolic answer first, that is, the answer expressed directly in terms of cos, etc.; then use evalf(") to get a numerical value.

## Tests

Often, we want to know whether two numbers are equal, or whether one is larger or smaller than the other. In each case, the answer is either true or false. A test of this sort is called a boolean test, and there is a special functions called evalb() to do it:
```> evalb(2.0 > 4.0) ;
```
false

```> evalb(2.0 = sqrt(4.0)) ;
```
true

Comparisons like these are done one at a time. So, if we want to know whether 0.5 is between 0 and 1 (which it is), although we might write this as 0 < 0.5 < 1, in Maple we type:
```> evalb(0 < 0.5 and 0.5 < 1) ;
```
true

The function evalb() understands the following operators:
=, <, >, <= (less-than-or-equal), >=, and, or, and not. Some more examples:
```> evalb(3.4 < 2.7) ;
```
false

```>evalb(not 3.4 < 2.7) ;
```
true

## Exercises 2

• 2.1: Test whether the square root of 117649 is equal to 347
• 2.2: Test whether the cosine of 12.5 is greater than 0 and less than 1
• 2.3*: Using assignment (see above), let number1 be the natural logarithm (log) of 3.0, let number2 be the natural logarithm of 4.0. Then test whether the sum of number1 and number2 is equal to the natural logarithm of 12.0. (Note that you can type all three expressions, each followed by ``;'', before hitting `Enter'.)

## Plotting

One of the best things about Maple is that we can plot functions. A function takes a number or numbers (inputs) and produces a number (an output). For example, the function y = 3x takes a single number as input and produces a number which is three times as big. Let us call this function threetimes(). Then threetimes(4) = 12, threetimes(2) = 6, and so on. Let us look at this function. First we will assign the name threetimes to the function.
```> threetimes := x -> 3*x ;
> threetimes(7) ;
```
The first line says that threetimes is a function which takes a single input (x) and produces a single output (3*x). Now type:
```> plot(threetimes, 0..10) ;
```
This will show the value of threetimes for all input values between 0 and 10. The plot appears as a separate window on the screen. (You will have to wait a few seconds before the window appears.) When you are done with it, you can close the window (just as you would close any Macintosh window). This helps to free up some memory and is highly recommended. Try also simply typing
```> plot(threetimes) ;
```
what is the difference? Here is a plot of the sine function from 0 to 25:
```plot(sin, 0..25) ;
```
Functions can be all sorts of expressions. Suppose you are interested in the function which maps all numbers less than 5 to -1, and all numbers greater than or equal to 5 to +1. You can define this function using an if statement. This has the form:
```if (some-condition) then some-action else some-other-action fi
```
Note that the statement ends with ``if'' spelled backwards. This is the usual way to end long statements in Maple. Note also that you will need parentheses around the condition so that Maple knows where it ends. Here is an example:
```> funnyfunction := x -> if (x < 5) then -1 else 1 fi ;
> plot(funnyfunction) ;
```
You can plot more than one function on the same graph, by making the first argument to plot a list of functions. A list is enclosed in curly brackets ({}), and its elements are separated by commas:
```> plot({cos, funnyfunction}, 0..10) ;
```

## Exercises 3

Plot the following functions. For each function, print out the plot. You will be required to hand in the plots (with your name at the top of each page) at the lecture on Tues., Sept. 19, or beforehand. Paper submissions like this may always be given to me at any lecture before the due date, or during my office hours.
• 3.1: The function which returns the difference between its input (x) and the square root of x
• 3.2: x cubed (i.e., x to the power of 3). Show values for -3 < x < 3
• 3.3*: The function which has the value x when x is between 5 and 10 and -x everywhere else. Show values for 0 < x < 15. Hint: You will need to use ``and''.

## Investigating periodic functions

Which of the following functions are periodic? If they are periodic, approximately what is their period? Note that if you do not specify a range of values for x, Maple will choose -10 < x < 10. You should experiment to find suitable values for each function.

## Exercises 4

• 4.1: cosine of (0.5 * x)
• 4.2: the nearest integer to x (use round(x))
• 4.3: the square of the cosine of x
• 4.4*: assign the following values: a := 1, b := 1, c := 0. Now assign the function generalfunction to be a * (cos(x))^b + c. Plot this. Now assign b := 100, and note what changes. Reset b to 1, let a equal 10, and note what this does. Now let c equal -2, and note the result. Try out a few more values. If you're feeling bold, try b := -1. Now describe, as precisely as you can, what the effects are of varying a, b, and c. If you complete this exercise, you are really getting into the swing of things! (Note: For this exercise, you submit both the plots and the answers to the questions about how the output of the function varies with the values of a, b, and c.)

## Investigating phase

Finally, we will plot pairs of functions. Your job is to say which are in phase and which are out of phase.

## Exercises 5

• 5.1: cos(x) and 2*cos(x)
• 5.2: sin(x) and sin(*x)
• 5.3: sin(x) and sin(3*x + 1)
• 5.4*: sin(x^2) and sin(x^3) (Plot this in the range from -4 to 4.)