\magnification = \magstep1
\centerline{Mathematics 171 -- Numerical Analysis}
\centerline{Lab Day 1 -- Maple on the Unix Workstation Network, Numerical Computing}
\centerline{September 8, 1998}
\bigskip
\noindent
{\it Running Maple in SW 219}
\bigskip
To run Maple on the Sun Unix workstation network, log onto any machine,
open a {\it Terminal\/} window using the Workspace Menu/Programs option, 
and type in {\tt xmaple \&}.  (The {\tt xmaple} part should be more or less 
self-explanatory; the ampersand {\tt \&} 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 {\it 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).

\bigskip
\noindent
{\it Maple Worksheets}
\bigskip
Worksheets are integrated text/graphics/mathematics
documents where any or all of the following can be done: 
\medskip
\itemitem{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.
\itemitem{b)}  output generated by Maple from your input
commands (numerical values,
symbolic formulas, and graphics) will be displayed, 
\itemitem{c)} you can modify commands, generating new output, store
your results for use later, etc.    
\itemitem{d)} you can enter text to annotate and explain the 
results of computations.
\medskip
\noindent
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.

\bigskip
\noindent
{\it Input and Text Regions in a Worksheet\/}
\bigskip
Now look at the Untitled (1) window itself.  On the first
line of the worksheet, you should see a $[>$.  This 
is the Maple {\it 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 {\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 {\it 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 ($\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!
\bigskip
\noindent
{\it Saving Your Work, Printing, Etc.}
\bigskip
As part of your user privileges on the Sun network, you have a directory 
on a local network disk.  This is {\it 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.

\bigskip
\noindent
{\it Important Note:}  If you read a previously saved worksheet 
back into a new Maple session, you {\it have not actually executed} 
any of the commands in the worksheet.  In particular, any assignments 
of values to variable names, etc. in that worksheet 
{\it 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
{\tt 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.
\bigskip
\noindent
{\it Numerical Computing in Maple}
\bigskip
\noindent
The most important point to know here are:
\bigskip
\item{$\bullet$} Maple's default floating point number system uses 
{\it 10 decimal digit rounding} arithmetic.  
\item{$\bullet$}  Numbers that are entered with a decimal
point, or in exponential
(``scientific notation'') format (for example, {\tt 5e-6} $ = 5\times 10^{-6}$) 
are treated as floating point numbers (even
if the fractional part is zero).  
\item{$\bullet$} Rational numbers like $34/19$, symbolic constants
like $\pi$ ({\tt Pi} in Maple), function values like $\sin(1)$, etc.
are {\it not automatically converted to 
floating point decimal form by Maple\/}.  
(This is a conscious design choice:  Maple consistently tries
to do computations {\it exactly\/} if it can, and rational arithmetic, for
example, involves no round-off errors!)  
\item{$\bullet$}  To {\it convert\/} a number (or the value of 
an expression) to floating point form, Maple uses a built-in function
called {\tt evalf} (``$\underline{\rm eval}$uate as $\underline{\rm f}$loating point'').
For instance, try entering the input command {\tt evalf(Pi);}.
\item{$\bullet$} The Maple notation for the four basic arithmetic operators
are $+,-,*,/$ and these produce floating point results, rounded to 
10 decimal digits, when {\it either of the two operands\/} is
a floating point number.
\item{$\bullet$} 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$?)
\item{$\bullet$}  You can change the default floating point number
system by assigning a new value to the system variable {\tt Digits}.
For example, to use 5 decimal digit rounding arithmetic, you could
first enter the command {\tt Digits := 5;}, then proceed with numerical
computations.  The value 5 for {\tt Digits} then remains in force within 
the current Maple session until you change it.
\item{$\bullet$}  Chopping arithmetic is not built-in in Maple, but it can
be programmed simply enough.  See Lab Exercise C below.
\bigskip
\noindent
{\it Lab Exercises}
\bigskip
\item{A)}  The Quadratic Formula is the general method for solving
quadratic equations $a x^2 + b x + c = 0$.  Provided that $b^2 - 4 a c \ge 0$, 
we get real roots:
$$x_1 = {-b + \sqrt{b^2 - 4ac}\over 2a}\ {\rm and}\ x_2 = {-b - \sqrt{b^2 - 4ac}\over 2a}
\leqno(1)$$
By multiplying numerator and denominator by the conjugate radical expression
and simplifying,
we can get algebraically equivalent forms:
$$x_1 = {-2c \over b + \sqrt{b^2 - 4ac}}\ {\rm and}\ x_2 = {-2c \over b - \sqrt{b^2 - 4ac}}
\leqno(2)$$
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.
\itemitem{1)}  $.041232 x^2 - 23.222 x + 1.2345 = 0$
\itemitem{2)}  ${1\over 5} x^2 - 20 x + {1\over 6} = 0$
\item{} Which formulas are more accurate, and {\it 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.
\bigskip
\item{B)}  To perform $t$-decimal digit chopping arithmetic with floating
point numbers in Maple, we need to write a {\tt 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:
\medskip
\centerline{\tt read `/home/fac/little/Classes/Numer-98/chop.map`;}
\medskip
\item{}  (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
{\tt chop(x,t)} in an input command.  For instance to chop $4.3332$ to 
$t = 3$ decimal digits, you would enter and execute:  {\tt chop(4.3332,3);}.
Then to perform 3 digit chopping arithmetic you could enter commands
containing operations {\tt chop(chop(x,3)+chop(y,3),3)} for sums, 
{\tt chop(chop(x,3)*chop(y,3),3)} for products, etc.
Repeat exercise A using chopping arithmetic instead of rounding arithmetic.
Are your conclusions substantially different?
\bigskip
\item{C)}  Let $P_n(x)$ be the degree $n$ Taylor polynomial for the arctangent
function (expanding at $a = 0$).  
\itemitem{1)} Look up the {\tt taylor} and {\tt 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)$.
\itemitem{2)}  The following two formulas give ways to approximate $\pi$:
$$\pi \doteq 4(P_n(1/2) + P_n(1/3))$$
and 
$$\pi \doteq 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 $\pi$ correct to 25 decimal places.
\bigskip
\item{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 {\it 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 
$$\cases{x_0 = 1&\cr
         x_1 = {1\over 3}&\cr
         x_{n+1} = {13\over 3} x_n - {4\over 3} x_{n-1}& for all $n \ge 1$\cr}
         \leqno(3)$$
\itemitem{1)}  Using 5-digit rounding arithmetic, compute $x_2, \ldots, x_{15}$.
\itemitem{2)}  Repeat 1, using 7-digit rounding arithmetic.
\itemitem{3)}  Repeat 2, now using 9-digit rounding arithmetic.
\itemitem{4)}  Show by mathematical induction that the exact value of $x_n$ 
satisfying (3) is
$$x_n = {1\over 3^n}.$$
Thus, $x_n > 0$ for all $n$, and we should see $x_n \to 0$ as $n \to \infty$(!)
\itemitem{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 + \epsilon$$
(where the error $\epsilon < 0$ in this case).  Similarly, we get 
computed values $\overline{x_n}$, $n \ge 2$.  Even assuming the rest of 
the computations could be done {\it without further round-off error\/},
what would the difference $\overline{x_n} - {1\over 3^n}$ be?  How
does this explain what you saw in parts 1,2,3?  Can this sequence
be computed accurately in {\it any decimal floating point number system\/}?
 
\bigskip
\noindent
{\it Assignment}
\bigskip
Individual lab write-ups (Maple worksheets) due in class, Tuesday, September 
15.
\bye

