HP-71B Program to the Find Root

by Namir Shammas

The following program calculates the root of a function using Newton's algorithm:

x' = x - f(x) / f '(x)

where f(x) is the function whose root is sought. f '(x) is the first derivative of function f(x). The program approximate the derivative using:

f '(x) = (f(x + h) - f(x)) / h

where h = 0.01 * (1 + |x|)

The program prompts you to enter:

1. Guess for the root.

2. Tolerance for the root. The default value is 1E-7.

3. The maximum number of iterations. The default value is 55.

The program displays the following results:

1. The root value.

2. The number of iterations.

If the number of iterations exceeds the maximum limit, the program displays the text SOLUTION FAILED before displaying the above results.

Here is a sample session to find the root of f(x) = e^x - 3*x^2 near x = 5 and using the default values for the tolerance and maximum number of iterations:

PROMPT/DISPLAY

 ENTER/PRESS

> [RUN]
GUESS? 5[END LINE]
TOLER? 1E-7 [END LINE]
MAX ITERS? 55 [END LINE]
(Audio beep)  
ROOT= 3.73307902942 [CONT]
ITERS= 9  

Here is the BASIC listing:

10 DEF FNF(X) = EXP(X) - 3 * X^2
20 INPUT "GUESS? ";X
30 INPUT "TOLER? ","1E-7";A$
35 T = VAL(A$)
40 INPUT "MAX ITER? ","55";A$
45 M = VAL(A$)
50 I = 0
60 I = I + 1
70 IF I > M THEN 125
80 H = 0.01 * (1 + ABS(X))
90 F = FNF(X)
100 D = H * F /(FNF(X + H) - F)
110 X = X - D
120 IF ABS(D) > T THEN 60
125 BEEP
126 If I > M THEN DISP "SOLUTION FAILED" @ PAUSE
130 DISP "ROOT = ";X
135 PAUSE
140 DISP "ITERS = ";I
150 END

The program uses the variables shown in the following table:

Variable Name

Contents

X Guess for root
T Tolerance
M Maximum number of iterations
I Iteration counter
H Increment h
F Value of the function at X
D Root refinement
A$ Temporary input for T and M

You can customize the above listing by changing the definition of function FNF in line 10. The current listing is set to solve the following equation:

f(x) = e^x - 3 * x^ 2

The above equation has roots near -0.45, 0.91, and 3.73.

BACK

Copyright (c) Namir Shammas. All rights reserved.