RootsEqns is a program that finds the roots of N (<= 10) 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:
The maximum number of iterations is reached.
The expression ||x' - x|| / N^0.5 < XTol is true
The expression ||F(x)|| / N^0.5 < FTol is true
Usage:
To start the program execute XEQ "RTEQNS"
The program prompts you to enter the functions toleance value.
The program prompts you to enter the root-guesses toleance value.
The program prompts you to enter the maximum number of iterations.
The program displays the array of guesses in edit mode. Enter the
values (you can inspect the various values and edit them). When you are
done, press the R/S key.
The program displays the solution vector in edit mode. If the maximum number of
iterations is exceeded you get a message first. Press R/S to view the current
values in array of guesses.
Note: The program divides the tolerance values by the square-root of the number of equations
in order to get an average that works better with the norm of the arrays of guesses and
function values..
To program a new set of equations perform the following edits:
Set line 2 to be equal to the number of equations.
Edit or cretae labels 01 to 10 (as needed) to represent the nonlinear functions. Label 01
calculates the value for function F1(x). Label 02 calculates the value for function F2(x),
and so on
In labels 01 through 10 use registers 01 through 10 to represent x(1) through x(10).
Edit label 36 to make sure that all of the elements of array VX are copied to memory
registers 01 and up. Here is an example of label 36 copying data to solve for 2 nonlinear
equations:
LBL 36
INDEX "VX"
RCLEL
STO 01
I+
RCLEL
STO 02
RTN
To copy more elements insert the following command pattern as many times needed:
I+
RCLEL
STO <register_number>
After editing for a specific set of equations you may want to save the program under a
distinct name.
Example (1):
NOTE: You can load the file RootsEqns.raw to run the next example.
After performing the above edits, you are ready to use the program.
Find the roots near x(1) = 0, x(2) = 0, and x(3) = 0. Allow 100 iterations at most and use a
root tolerance of 1E-5 and function tolerance of 1E-8.
Make sure that line 2 contains the number 2 which is the number of nonlinear equations.
XEQ "RTEQNS"
XTOL= 1.0000E-5
1E-5 RUN
FTOL= 1.0000E-8
1E-8 RUN
MAX= 100.0000
100.0000 RUN
X1?
0.0000 RUN
X2?
0.0000 RUN
X3?
0.0000 RUN
VX= [ 3x1 Matrix ]
1:1= -0.2337
2:1= 1.3532
3:1= 3.2986
The program found roots at x(1) = -0.2337, x(2) = 1.3532, and x(3) = 3.2986.
Run the program again and provide an initial guesas of 1 for the three roots. Use the same values
for the tolerances and maximum iteration limits.
XEQ "RTEQNS"
XTOL= 1.0000E-5
RUN
FTOL= 1.0000E-8
RUN
MAX= 100.0000
RUN
X1?
1.0000 RUN
X2?
1.0000 RUN
X3?
1.0000 RUN
VX= [ 3x1 Matrix ]
1:1= 1.0000
2:1= 2.0000
3:1= 3.0000
The program found roots at x(1) = 1, x(2) = 2, and x(3) = 3.
Technical Notes:
Pseudo-code
N = number of equations ' hard coded
Redimention matrix A and arrays X, DX, and F
Input XTol, FTol, MaxIter, and array X
Iter = 0
Do
Increment Iter
If Iter > MaxIter then exit
For I = 1 to N
F(I) = FX(X, I)
FF = F(I)
For J = 1 to N
A(I,J) = FF
Next J
Next I
For I = 1 to N
XX = X(I)
H = 0.01 * (1 + ABS(XX))
X(I) = XX + H
For J = 1 to N
FF = FX(X, J)
A(J,I) = (FF - A(J,I)) / H ' Jacobian matrix elements
Next J
X(I) = XX
Next I
DX = INVERSE(A) * F ' This is a matrix operation
X = X - DX ' This is a matrix operation
Loop Until ||DX|| / N^0.5 < XTol Or ||F|| / N^0.5 < FTol
Display elements of array X
(The program listing with comments can be downloaded here: listing.txt)
Memory usage:
MA = Matrix of function slopes VB = Array of function values VX = Array of root guesses VDX = Array of root guesses refinements FTOL = Functions tolerance XTOL = Tolerance for root refinements N = Number of functions I = Loop control variable J = Loop control variable FF = Used XX = Used H = Increment in a guess value MAX = Maximum number ot iterations ITR = Number of iterations
R00 to R10 are used to store X(1) through X(10) R11 = Number of digits displayed R12 = Square root of the number of equations
Flags:
00 Display control 01 Display control 02 Control of indexing array MA
Label usage:
01 Label for function F1(x) 02 Label for function F2(x)
... 10 Label for function F10(x)
00 Main loop 11 loop 12 loop 13 loop 14 loop 15 used 16 used 17 Query display mode and digits 18 Restore display mode 20 Set I = 1 + N / 1000 21 Set J = 1 + N / 1000 30 Store value in X(I) 31 Recall X(I) 32 Store value in A(I,J) if flag 2 is clear or in A(J,I) if flag 2 is set 33 Recall A(I,J) if flag 2 is clear or recall A(J,I) if flag 2 is set 34 Store value in F(I) 35 Recall F(I) 36 Copy values of array X into memory registers 01 and up.