The Museum of HP Calculators
This program is by Namir Shammas and is used here by permission.
This program is supplied without representation or warranty of any kind. Namir Shammas and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.
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:
|TOLER? 1E-7||[END LINE]|
|MAX ITERS? 55||[END LINE]|
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
126 If I > M THEN DISP "SOLUTION FAILED" @ PAUSE
130 DISP "ROOT = ";X
140 DISP "ITERS = ";I
The program uses the variables shown in the following table:
|X||Guess for root|
|M||Maximum number of iterations|
|F||Value of the function at X|
|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.
Go back to the software library
Go back to the main exhibit hall