True BASIC Program to Find Root

For Simultaneous Nonlinear Equations

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

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 [Enter]
F TOLERANCE? 1E-7 [Enter]
MAX ITERS? 100 [Enter]
ENTER GUESSES  
X(1)? 0[Enter]
X(2)? 0[Enter]
X(2)? 0[Enter]
X( 1 ) = -0.233720580897  
X( 2 ) =1.35319020628   
X( 3 ) = 3.2986489625  
ITERS= 11  
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 [Enter]
F TOLERANCE? 1E-7 [Enter]
MAX ITERS? 100 [Enter]
ENTER GUESSES  
X(1)? 5[Enter]
X(2)? 5[Enter]
X(2)? 5[Enter]
X( 1 ) =1.00000000000  
X( 2 ) =2.000000000000  
X( 3 ) = 2.99999999999  
ITERS= 9  
END OF PROGRAM  

Here is the BASIC listing:

OPTION NOLET
OPTION TYPO
! SOLVE FOR ROOTS OF MULTPLE NONLIEAR EQNS
DECLARE NUMERIC I,J,N,X0,F0,H,N1,FTOL,XTOL,MAXITERS,ITER

DEF FNF(X(),I)
  IF I=1 THEN FNF=X(1) + X(2) + X(3)^2 - 12
  IF I=2 THEN FNF=X(1)^2 - X(2) + X(3) - 2
  IF I=3 THEN FNF=2 * X(1) - X(2)^2 + X(3) - 1
END DEF

SUB SYS(A(,),B(),X())
LOCAL I, J, N
DIM BB(1,1),XX(1,1)
N = UBOUND(A,1)
MAT REDIM BB(N,1), XX(N,1)
FOR I=1 TO N
  BB(I,1)=B(I)
NEXT I
MAT A=INV(A)
MAT XX=A*BB
FOR I=1 TO N
  X(I)=XX(I,1)
NEXT I
END SUB


DEF FNORM(X())
LOCAL I, R
R = 0
FOR I = 1 TO UBOUND(X)
  R = R + X(I)^2
NEXT I
FNORM=SQR(R)
END DEF

PRINT "SOLVE SYSTEM OF NONLINEAR EQUATIONS"
DIM A(1,1),X(1),D(1),F(1)
N = 3 ! NUMBER OF NONLINEAR EQUATIONS
N1=SQR(N)
MAT REDIM A(N,N),X(N),D(N),F(N)
INPUT PROMPT "X TOLERANCE? ": XTOL
INPUT PROMPT "F TOLERANCE? ": FTOL
INPUT PROMPT "MAX ITERS? ": MAXITERS
PRINT "ENTER GUESSES"
MAT INPUT X
PRINT "WORKING..."
ITER = 0
DO
  ITER=ITER+1
  IF ITER>MAXITERS THEN EXIT DO
  FOR I= 1 TO N
    F(I) = FNF(X,I)
    F0=F(I)
    FOR J=1 TO N
      A(I,J) = F0
    NEXT J
  NEXT I
  FOR I=1 TO N
    X0=X(I)
    H=0.01*(1+ABS(X0))
    X(I)=X0+H
    FOR J=1 TO N
      F0=FNF(X,J)
      A(J,I)=(F0-A(J,I))/H
    NEXT J
    X(I)=X0
  NEXT I
  CALL SYS(A, F, D)
  MAT X=X-D
LOOP WHILE (FNORM(D)/N1 > XTOL) AND (FNORM(F)/N1 > FTOL)

IF ITER>MAXITERS THEN PRINT "SOLUTION FAILED!"

FOR I=1 TO N
  PRINT "X(";I;")=";X(I)
NEXT I
PRINT "ITERS=";ITER
PRINT "END OF PROGRAM"
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.

2. Edit the definition of function FNF. This user-defined function acts as a central switchboard and must return a value for all nonlinear functions. The current program code is aimed as solving the example shown earlier.

BACK

Copyright (c) Namir Shammas. All rights reserved.