### Introduction to Scientific ProgrammingComputational Problem Solving Using:Maple and CMathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

# Assignment Expressions Worksheet

Click below to download a Maple V worksheet. You can look at the appended non-interactive HTML version of the worksheet to learn what the worksheet covers.

This worksheet is designed to accompany Chapter 3 of Introduction to Scientific Programming: Computational Problem Solving Using Maple and C by Joseph L. Zachary. In it, we will use Maple to explore operator precedence and assigment statements in Maple (AW, Jan 97)

# Assignment

When a number is used several times in a worksheet, it is better to assign a name to that number, rather than retyping it on every occasion. This eliminates errors and speeds up your workrate.

For example, suppose we are doing computations that involve the population of the earth like we did in Chapter 2. Rather than typing the number 5.761e9 every time it is used, we can assign its value to a variable, say P:

> P := 5.761e9;

Similarly, we can assign the land area to a variable, say A:

> A := 57.8e6;

The variables A and P will now retain these values for further computations in this worksheet. For example, we can compute the number of persons per square mile as

> P/A;

Or we could compute the side length of the square (in feet) that each of us get by

> 5280 * sqrt(A/P);

Note that the colon that precedes the equal to sign is an essential part of the assignment statement; it cannot be ignored. Execute the assignment

> B = 14.56e-1;

followed by the expression

> 2*B;

That did not help much did it? Now insert the colon right before the equals sign and reevaluate these two expressions. Better?

# Choosing Variable Names

It is a good strategy to pick descriptive names that you can quickly recall. For example, suppose we need to solve the population density problem of Chapter 2 for the planets Mars and Venus (assuming a means of sustaining the entire population of the earth on these planets has been found.) Then it will be good to name these variables as follows:

> Aearth := 57.8e6; Amars := 56.0e6; Avenus := 177.7e6;

(Note how several expressions can be entered on the same line.)

Now return to the expressions P/A and 5280*sqrt(A/P) above. Re-execute them, with the appropriate variable inserted in place of A, to solve the population density problem for Mars and Venus.

# Special Symbols

There are a few symbols that have a pre-defined meaning in Maple that you may not use as variable names. Maple will warn you should you transgress. For example

> Pi := 3;

The problem is that the symbol Pi is reserved for the mathematical constant 3.14159... If you evaluate this,

> evalf(Pi);

You will see the first ten digits of Pi.

Observe how you can use Greek letters as variable names:

> alpha := 1;

> omega := 100;

> pi := 22/7;

Notice the difference between this pi and the Pi above. The lower case pi is simply the Greek letter for p, with no special meaning attached to it. The upper case Pi, on the other hand, stands for the ratio between the circumference of a circle and its diameter.

A useful built-in Maple variable is Digits. It allows you to specify the mantissa length for floating-point computations. The default value of Digits is 10.

> Digits;

So a ten-digit computation of 10/3 looks like this:

> evalf(10/3);

If we set Digits to 2, however,

> Digits := 2;

We get a result with only two digits

> evalf(10/3);

and if we set Digits to 30,

> Digits := 30;

we get a result with 30 digits.

> evalf(10/3);

Now try

> Digits := 2;

> 999 - 1;

> 999. - 1.;

Explain the difference between the two answers.

When you experiment with changing the value of Digits, always be sure to reset its value to the default value of 10 before continuing. Otherwise, you may get some unpleasant surprises later in your worksheet.

> Digits := 10;