HP-71B Program to Find Roots

For Simultaneous Nonlinear Equations with Math Module

by Namir Shammas

This program finds the roots of N nonlinear functions. The program uses a straightforward version of Newton's algorithm:

x' = x - F(x) / J(x)

Where

x = array of guesses
x' = array of refined guesses
F(x) = array of nonlinear functions
J(x) = Jacobian matrix of function derivatives, J(i,j) = dFi(x)/dxj

The iterations continue until one of these conditions are met:

1. The maximum number of iterations is reached.
2. The expression ||x' - x|| / N^0.5 < XTol is true
3. The expression ||F(x)|| / N^0.5 < FTol is true

Click here to download a ZIP file containing the project files for this program.

The program prompts you to enter:

1. Tolerance for the roots (to be compared with ||x' - x|| / N^0.5). The default value is 1e-7.

2. Tolerance for the function norm (to be compared with ||F(x)|| / N^0.5). The default value is 1e-7.

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

4. The guesses for the roots,

The program displays the following results:

1. The values of the roots.

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.

The current code solves the following system of nonlinear equations:

F1(x) = x(1) + x(2) + x(3)^2 - 12
F2(x) = x(1)^2 - x(2) + x(3) - 2
F3(x) = 2 * x(1) - x(2)^2 + x(3) - 1

The initial guesses for the roots zeros. The tolerances for the roots x and y are 1E-7 and 1E-7, respectively. The maximum number of iterations is 100.

PROMPT/DISPLAY

 ENTER/PRESS

> [RUN]
X TOLERANCE? 1E-7 [END LINE]
F TOLERANCE? 1E-7 [END LINE]
MAX ITERS? 100 [END LINE]
ENTER GUESSES  
X(1)? 0[END LINE]
X(2)? 0[END LINE]
X(2)? 0[END LINE]
WORKING...  
(Audio beep)  
X( 1 ) = -0.233720580897 [CONT]
X( 2 ) =1.35319020628  [CONT]
X( 3 ) = 3.2986489625 [CONT]
ITERS= 11 [CONT]
END OF PROGRAM  

Run the program again with guesses all being equal to 5. Notice the different results.

PROMPT/DISPLAY

 ENTER/PRESS

> [RUN]
X TOLERANCE? 1E-7 [END LINE]
F TOLERANCE? 1E-7 [END LINE]
MAX ITERS? 100 [END LINE]
ENTER GUESSES  
X(1)? 5[END LINE]
X(2)? 5[END LINE]
X(2)? 5[END LINE]
WORKING...  
(Audio beep)  
X( 1 ) =1.00000000000 [CONT]
X( 2 ) =2.000000000000 [CONT]
X( 3 ) = 2.99999999999 [CONT]
ITERS= 9 [CONT]
END OF PROGRAM  

Here is the BASIC listing:

10 ! SOLVE FOR ROOTS OF MULTPLE NONLIEAR EQNS
20 DESTROY ALL @ STD
30 N = 3 ! NUMBER OF NONLINEAR EQUATIONS
40 N1=SQR(N)
50 DIM A(N,N),X(N),D(N),F(N)
60 DEF FNF(I)
70 ! ACCESS GLOBAL ARRAY X
80 IF I=1 THEN FNF=X(1) + X(2) + X(3)^2 - 12
90 IF I=2 THEN FNF=X(1)^2 - X(2) + X(3) - 2
100 IF I=3 THEN FNF=2 * X(1) - X(2)^2 + X(3) - 1
110 END DEF
120 INPUT "X TOLERANCE?","1E-7";T1
130 INPUT "F TOLERANCE?","1E-7";T2
140 INPUT "MAX ITERS?","100";M
150 DISP "ENTER GUESSES" @ WAIT 2
160 MAT INPUT X
170 DISP "WORKING..."
180 K = 0
190 REM START
200 K=K+1
210 IF K>M THEN 420
220 FOR I= 1 TO N
230 F(I) = FNF(I)
240 F0=F(I)
250 FOR J=1 TO N
260 A(I,J) = F0
270 NEXT J
280 NEXT I
290 FOR I=1 TO N
300 X0=X(I)
310 H=0.01*(1+ABS(X0))
320 X(I)=X0+H
330 FOR J=1 TO N
340 F0=FNF(J)
350 A(J,I)=(F0-A(J,I))/H
360 NEXT J
370 X(I)=X0
380 NEXT I
390 MAT D=SYS(A,F)
400 MAT X=X-D
410 IF (FNORM(D)/N1 > T1) AND (FNORM(F)/N1 > T2) THEN 190
420 REM EXIT
430 IF K>M THEN DISP "SOLUTION FAILED!" @ PAUSE
440 BEEP
450 FOR I=1 TO N
460 DISP "X(";I'")=";X(I) @ PAUSE
470 NEXT I
480 DISP "ITERS=";K @ PAUSE
490 DISP "END OF PROGRAM"
500 END

You can customize the above listing by performing the following steps:

1. Assign the correct number of nonlinear equations to solve to variable N in line 3.

2. Edit the definition of function FNF in lines 60 to 110. This user-defined function acts as a central switchboard and must return a value for all nonlinear functions. The function accesses the global array X to access the values for the current guesses for the roots. The current program code is aimed as solving the example shown earlier.

BACK

Copyright (c) Namir Shammas. All rights reserved.