Mathematics 171 – Numerical Analysis Lab Day 1 – Maple on the Unix Workstation Network, Numerical Computing September 8, 1998

Running Maple in SW 219

To run Maple on the Sun Unix workstation network, log onto any machine, open a Terminal window using the Workspace Menu/Programs option, and type in xmaple &. (The xmaple part should be more or less self-explanatory; the ampersand & means that Maple will be run in an independent window, and the Terminal window will be available for you to input additional commands, etc. If you omit the ampersand, the Terminal is not usable this way while Maple is running.)

The current version of Maple here and in HA408 is now Maple V Release 5 . There are few changes from Release 4 that will be significant for us in this course, so if you took Analysis 3 and used Maple V Release 4 on the Suns then, you will not need to make any adjustments. If you have only used Release 3, there will be a few differences, most notably in new advanced features of the worksheet interface (like the ability to create “open- and close-able” sections of worksheets).

Maple Worksheets

Worksheets are integrated text/graphics/mathematics documents where any or all of the following can be done:

a) you can type in commands from the keyboard to ask Maple to perform many different kinds of calculations, read data from external files, store work in files, etc.

b) output generated by Maple from your input commands (numerical values, symbolic formulas, and graphics) will be displayed,

c) you can modify commands, generating new output, store your results for use later, etc.

d) you can enter text to annotate and explain the results of computations.

Take a few seconds and notice the features of this window – especially the File, Edit, View, Insert, etc. pull-down menus across the top, the “tool bar” below them with the icons for various operations (can you guess what each of them does?) the “scroll bar” on the right that you can use to move around within the worksheet, to see previous input and output lines, etc. Most of the common operations appear both as items in one of the pull-down menus and as toolbar buttons. The toolbar buttons are faster to use, but somewhat limited in the options they offer, so you will want to have a general idea what is accessible from where in the pull-down menus. Take a few minutes and look these over.

Input and Text Regions in a Worksheet

Now look at the Untitled (1) window itself. On the first line of the worksheet, you should see a [>. This is the Maple input prompt – the signal that Maple is ready to accept a command from you and try to execute it. A new input prompt will be generated automatically in a worksheet each time you enter a command and execute it at the end of a worksheet in progress. You can also go back and insert new commands and output at any point in a worksheet as follows. You can insert an input prompt and an input region at any point in the worksheet by placing the cursor at the desired location and pressing the toolbar button marked with [>. (After executing an inserted command like this, Maple will drop to the next input line, so to insert several input lines in the middle of a worksheet, you will need repeat the above.)

As we mentioned before, Maple worksheets can contain text as well as commands and output. To create a text region you can press the [> button above, then the button marked by a capital T (this changes the region into a text region). Any text can now be entered, and this will be treated as an “inert” comment. That is, it will appear as you enter it when you print out the worksheet, and it will not be treated as Maple input. After you create a text region, if you press the capital sigma (Σ) toolbar button with the cursor placed on that region, you can type in a Maple expression in input format, and have it displayed (in “inert” form) in usual mathematical notation (with raised exponents, no asterisks for multiplication, etc. This is sometimes useful for creating “pretty” worksheets!

Saving Your Work, Printing, Etc.

As part of your user privileges on the Sun network, you have a directory on a local network disk. This is separate from your campus network P: drive, and your P: drive is not accessible from this system. You can save your Maple worksheets there by clicking the toolbar icon that looks like a floppy disk, filling out the filename field in the Save As form and clicking OK. You can read a previously saved worksheet back in by clicking the open folder toolbar icon and filling in the name of the worksheet or selecting it from the listing of files in the Open File form.

Important Note: If you read a previously saved worksheet back into a new Maple session, you have not actually executed any of the commands in the worksheet. In particular, any assignments of values to variable names, etc. in that worksheet have not been performed in the current Maple session. If you want to execute some of commands in the worksheet again, you will need to press ENTER on those lines again. You can also execute an entire worksheet from an option in the EDIT pull-down menu.

To print a file in SW 219, use the printer toolbar icon. In the Printer Setup form, the settings might need one adjustment – make sure the Print Command button is depressed, and the box next to it says lp. After you press Print, the output will be routed to the HP4MV printer in SW219 for you to pick up.

You exit Maple by selecting the EXIT option from the FILE menu.

Numerical Computing in Maple

The most important point to know here are:

• Maple’s default floating point number system uses 10 decimal digit rounding arithmetic.

• Numbers that are entered with a decimal point, or in exponential (“scientific notation”) format (for example, 5e-6 = 5 × 10^{−6}) are treated as floating point numbers (even if the fractional part is zero).

• Rational numbers like 34/19, symbolic constants like π (Pi in Maple), function values like sin(1), etc. are not automatically converted to floating point decimal form by Maple. (This is a conscious design choice: Maple consistently tries to do computations exactly if it can, and rational arithmetic, for example, involves no round-off errors!)

• To convert a number (or the value of an expression) to floating point form, Maple uses a built-in function called evalf (“\underline{eval}uate as \underline{f}loating point”). For instance, try entering the input command evalf(Pi);.

• The Maple notation for the four basic arithmetic operators are +, −, ∗, / and these produce floating point results, rounded to 10 decimal digits, when either of the two operands is a floating point number.

• You can add, subtract, multiply, or divide numbers with more than 10 decimal digits, but the results you get are what Burden and Faires write as fl(fl(x) + fl(y)), etc. (see page 21). For instance try entering

2.1234567894 + .0000000002;

at an input prompt and pressing ENTER. Why do you get what you do? (Why isn’t it 2.123456790?)

• You can change the default floating point number system by assigning a new value to the system variable Digits. For example, to use 5 decimal digit rounding arithmetic, you could first enter the command Digits := 5;, then proceed with numerical computations. The value 5 for Digits then remains in force within the current Maple session until you change it.

• Chopping arithmetic is not built-in in Maple, but it can be programmed simply enough. See Lab Exercise C below.

Lab Exercises

A) The Quadratic Formula is the general method for solving quadratic equations ax^{2} + bx + c = 0. Provided that b^{2} − 4ac ≥ 0, we get real roots:

(1)  x_{1} = \frac{−b + \sqrt{b^{2} − 4ac}}{2a} and x_{2} = \frac{−b − \sqrt{b^{2} − 4ac}}{2a}

By multiplying numerator and denominator by the conjugate radical expression and simplifying, we can get algebraically equivalent forms:

(2)  x_{1} = \frac{−2c}{b + \sqrt{b^{2} − 4ac}} and x_{2} = \frac{−2c}{b − \sqrt{b^{2} − 4ac}}

For each of the following quadratic equations, use both (1) and (2) and 5-digit rounding arithmetic in Maple to approximate the roots. Also compute the absolute and relative errors of each approximation.

1) .041232x^{2} − 23.222x + 1.2345 = 0

2) \frac{1}{5}x^{2} − 20x + \frac{1}{6} = 0 Which formulas are more accurate, and why? (Think about the “bad cases” for floating point arithmetic we discussed in class.) Explain your conclusions in a text region in your Maple worksheet.

B) To perform t-decimal digit chopping arithmetic with floating point numbers in Maple, we need to write a chop procedure that gives the result of chopping a number to t decimal places. I have written one for you to use, following a problem in the text. To use it, first enter and execute the command to read in an input file:

        read `/home/fac/little/Classes/Numer-98/chop.map`;

(Those are “back-quotes” around the file name.) The file contains Maple source code for a procedure with two inputs: x – a number, and t – an integer. You call it by placing the expression chop(x,t) in an input command. For instance to chop 4.3332 to t = 3 decimal digits, you would enter and execute: chop(4.3332,3);. Then to perform 3 digit chopping arithmetic you could enter commands containing operations

chop(chop(x,3)+chop(y,3),3) for sums, chop(chop(x,3)*chop(y,3),3) for prod-

ucts, etc. Repeat exercise A using chopping arithmetic instead of rounding arithmetic. Are your conclusions substantially different?

C) Let P_{n}(x) be the degree n Taylor polynomial for the arctangent function (expanding at a = 0).

1) Look up the taylor and convert,polynom commands in Maple’s online Help (use Topic Search from the HELP pull-down menu), and figure out how to use them to compute P_{n}(x).

2) The following two formulas give ways to approximate π:

π ≐ 4(P_{n}(1/2) + P_{n}(1/3))

and

π ≐ 16P_{n}(1/5) − 4P_{n}(1/239)

Using Maple with 75-digit arithmetic, in each case determine how big n must be so that we obtain approximations to π correct to 25 decimal places.

D) There are situations when repetitive numerical computations can produce totally inaccurate results. Among the worst of these are situations where small errors in early steps of a calculation propagate and produce larger and larger errors in later steps. This effect is known as numerical instability. If this happens, you need to look for a different method to solve your problem! Here is an example. Consider the sequence produced by

(3)  \{ \begin{matrix}x_{0} = 1 \\ x_{1} = \frac{1}{3} \\ x_{n+1} = \frac{13}{3}x_{n} − \frac{4}{3}x_{n−1} & for all n ≥ 1 \end{matrix}

1) Using 5-digit rounding arithmetic, compute x_{2}, …, x_{15}.

2) Repeat 1, using 7-digit rounding arithmetic.

3) Repeat 2, now using 9-digit rounding arithmetic.

4) Show by mathematical induction that the exact value of x_{n} satisfying (3) is

x_{n} = \frac{1}{3^{n}} .

Thus, x_{n} > 0 for all n, and we should see x_{n} → 0 as n → ∞(!)

5) To give a partial explanation for the behavior you saw, consider the 5-digit computation, for instance. The floating-point value \overline{x_{1}} used for x_{1} is fl(1/3) = .33333, which is not exactly 1/3(!). In fact

\overline{x_{1}} = 1/3 + ϵ

(where the error ϵ < 0 in this case). Similarly, we get computed values \overline{x_{n}}, n ≥ 2. Even assuming the rest of the computations could be done without further roundoff error, what would the difference \overline{x_{n}} − \frac{1}{3^{n}} be? How does this explain what you saw in parts 1,2,3? Can this sequence be computed accurately in any decimal floating point number system?

Assignment

Individual lab write-ups (Maple worksheets) due in class, Tuesday, September 15.