HP Forums

Full Version: (48) Complex Roots Of Multiple Non-Linear Equations (Newton-Raphson Method)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Complex Roots of Multiple Non-Linear Equations Via Newton-Raphson Method, HP-48
Gerardo V. Lozada, M.S., P.E.E., May 12, 2015

Computes the real and complex roots of a system of simultaneous non-linear equations using the Newton-Raphson Method utilizing the forward difference method to approximate the Jacobian matrix.

Input parameters on the stack:

MQ - A list containing the simultaneous non-linear equations, each enclosed in single quotes ' ', the number n of variables should equal the number of equations. Variables are denoted as X(1), X(2), X(3) ...... X(n).

X0 - Array containing the initial estimate of the roots, may be real or complex.

Example: Find the roots of the three simultaneous equations X1^2 - 2*X1*X2 + X2^2 - ln(X3) = 16, X1^2 - X2^2 + 20*sin(X3) = 9 and X1^3 + X2^2 + X3^2 = -27 using initial estimates of X1=1-j7, X2=2+j10 and X3=-4-j11.

Enter {'X(1)^2-2*X(1)*X(2)+X(2)^2-LN(X3)-16' 'X(1)^2-X(2)^2+20*SIN(X)-9' 'X(1)^3+X(2)^2+X(1)^2+27'} and [(1,-7) (2,10) (-4,-11)] on the stack and then run the program NR.

The resulting complex roots are: X = [(1.1215, 4.0809) (5.313, 3.7512) (-3.9988, -1.588) ] converged in
15 iterations. Final estimate error is 4.5802e-7. Other root combinations may be found depending on the initial root estimates used.


Program NR

<< -> MQ X0 <<
1e-6 'TL' STO
MQ SIZE 'N' STO
X0 'X' STO
IF X0 TYPE 4 == THEN
(0.01,0.01) 'D' STO ELSE
0.01 'D' STO
END
0 'ITER' STO
DO
0 'YM' STO
1 N FOR I
MQ I GET ->NUM
NEXT
N ->ARRY
'Y' STO
1 N FOR I
Y I GET
ABS 'YI' STO
IF YI YM > THEN
YI 'YM' STO
END
NEXT
IF YM TL >= THEN
1 N FOR I
1 N FOR J
X J GET
D +
X J 3 ROLL
PUT
'X' STO
MQ I GET
->NUM
Y I GET -
D /
X J GET
D -
X J 3 ROLL
PUT
'X' STO
NEXT
N ->ARRY
NEXT
N ROW-> 'JAC' STO
Y JAC /
'DX' STO
X DX -
'X' STO
END
ITER 1+
'ITER' STO
UNTIL YM TL < END
MQ
X "X" ->TAG
ITER "ITER" ->TAG
YM "YM" ->TAG
>> >>
Request. Could you put the code within "code" tags? It gets more readable.

Code tags are the following: <code></code> substituting '<' with '[' and '>' with ']'
First of all, this program, because of "ROW->" only works on HP48GX, but NOT on HP48SX (the HP48SX does not have the "ROW->" command). At least that was my experience with Emu48.
The zip file contents (incl. the .HP binary file) at https://www.hpcalc.org/details/8802 are incorrect. I created a working version of the below files as the cplxroots.HP attachment to this post.

The inputs are the following:
Equations:
{ 'X(1)^2-2*X(1)*X(2)+X(2)^2-LN(X(3))-16' 'X(1)^2-X(2)^2+20*SIN(X(3))-9' 'X(1)^3+X(2)^2+X(3)^2+27' }

Initial guesses for X(1), X(2), and X(3):
[ (1,-7) (2,10) (-4,-11) ]

You have to provide the above arguments to the program below:

« \-> MQ X0
« .000001 'TL' STO MQ SIZE 'N' STO X0 'X' STO
IF X0 TYPE 4 ==
THEN (.01,.01) 'D' STO
ELSE .01 'D' STO
END 0 'ITER' STO
DO 0 'YM' STO 1 N
FOR I MQ I GET \->NUM
NEXT N \->ARRY 'Y' STO 1 N
FOR I Y I GET ABS 'YI' STO
IF YI YM >
THEN YI 'YM' STO
END
NEXT
IF YM TL \>=
THEN 1 N
FOR I 1 N
FOR J X J GET D + X J 3 ROLL PUT 'X' STO MQ I GET \->NUM Y I GET - D / X J GET D - X J 3 ROLL PUT 'X' STO
NEXT N \->ARRY
NEXT N ROW\-> 'JAC' STO Y JAC / 'DX' STO X DX - 'X' STO
END ITER 1 + 'ITER' STO
UNTIL YM TL <
END MQ X "X" \->TAG ITER "ITER" \->TAG YM "YM" \->TAG
»
»

(Notes:
Replace in the above the \-> sequence with the green right shift + 0 character.
Replace in the above \>= sequence with the HP character map's >= character.)
The « and » characters should translate fine to the characters inserted by pressing green right shift + - (minus sign). )
Reference URL's