Looping; Flags - HP -11C Owner's Handbook Manual

Hide thumbs Also See for HP-11C:
Table of Contents

Advertisement

214
Section 11: Programming Techniques
Looping
The Newton's Method program deals with the common problem of
approximating a solution to an equation. Such approximations are
necessary because finding an exact root to certain functions is
often difficult and sometimes impossible.
In Newton's Method, we choose a point x at which the function f(x)
exists and, we hope, is close to a root of the function. (Applying a
little basic algebra will help narrow the guessing range.) What goes
on next is to adjust the guess, calculating a closer approximation of
the root based on the initial guess. The calculated value is then
used to calculate another closer approximation. This process is
repeated with the new guess until, in the limit of an infinite number
of repetitions, the exact solution may be reached.
We see that a section of the program must be repeated over and
over again in succession until we get the answer we desire. Such a
section is called a "loop". (The main loop in this program is
between lines 026 and 052.) Of course, an infinite number of
repetitions implies an infinite period of time, so we must set a
boundary on the number of iterations performed.
One method of setting this boundary would be to insert a counter in
the loop such that after each pass through the loop a certain
register will have been incremented by one. The total number of
repetitions is compared with a desired maximum. The program will
then exit from the loop when the two values are equal. (In this
program, the maximum
is decremented each time and the loop
terminates when the value has gone to zero. See line 051.) Notice
that this method does not ensure any standard of accuracy. Also,
depending on the function and the guess, finding a reasonable
answer may take an unreasonable number of repetitions. In other
words, stopping the process at even one-hundred repetitions may
yield an answer so far from the actual root as to be useless.
A preferable method is to test the last two computed values to see
whether or not they are significantly different. This difference,
called the Ax Limit, is up to the user and is input when initializing
the program.
The format for such a loop is as follows:
1.
Store the Ax limit and the initial guess.
2.
Compute the first approximation from the guess and store it.
~~
ww
SPP
A
os
ee
ee
Section 11: Programming Techniques
216
3.
Compute the next approximation from the modified guess
and store it.
4.
Recall the value of the previous approximation and subtract
it from the value of the present approximation.
5.
Recall
the
Ax
limit and
compare
its value with
the
magnitude of the difference just computed.
6.
If the magnitude of the difference is greater than that of the
Ax limit continue looping.
7.
If the magnitude of the difference is less than that of the 4x
limit, exit from the loop.
Notice that this method does not guarantee an exit from the loop.
The first guess may have been very far from the actual root, or the
root may not even exist. The best method then is a combination of
the two, as is the method used by this program.
Not at all obvious is the fact that the nature of certain functions is
such that phantom roots may appear. These phantom roots occur
when the slope of the function is so large that the difference
between two consecutive approximations falls within the Ax limit.
This program provides a safeguard against this by testing not only
the loop count and the Ax limit but also the value of the function at
the point in question. The functional value is compared with the
tolerance to see how close to zero the user wants the solution to be.
Flags
When writing a program that deals with different problems in
similar but not identical fashions, flags can be used to control the
differences in the treatment of the data.
Flags
serve
as
reminders.
For
instance,
when
the program
performs an optional initial calculation, a flag may be set. Later in
the program we may need to know whether or not the operation
was performed, so we test the flag, i.e, Is flag 0 set? (Was the
operation performed?) In this way, flags are part of the decision-
making power of the calcualtor.
In the Curve Fitting program, three very similar processes may be
performed depending on the choice of curve to be fit. To illustrate
the role of the flags, two more columns may be added to the chart
on page 162.

Advertisement

Table of Contents
loading

Table of Contents