Simultaneous equation solver for the HP-42S

This program is based on an RPL program written by Mike Ingle, originally posted on the HP Museum Forum.
To use the solver:

Enter your expressions and save them in variables. They should be expressions for which the calculator will find a zero. If you start out with equations, put parens around both sides and change the = to a - sign.

Enter initial guesses and save them in the appropriate variables. All the variables must exist, so the expressions will eval to numbers.

Enter four lines on the stack: List of expressions to solve. List of variables to solve for. Acceptable error, indicating when to stop. Delta (small number which is added to obtain slopes.)

Run the solver. If you saved it as MSLV, enter MSLV CLMF

Example: '2*X^3+Y+7*Z-96' 'E1' STO '3*X+6*Y^3-2*Z-65' 'E2' STO '-6*X+4*Y+2*Z^3-42' 'E3' STO 1 'X' STO 2 'Y' STO 3 'Z' STO { 'E1' 'E2' 'E3' } { 'X' 'Y' 'Z' } 1E-7 1E-7 MSLV CLMF

In 4 FIX: X=3.3161 Y=2.1666 Z=2.9857

After the first iteration, you will see two lines. The first shows the iteration count starting at 1. The second shows the worst residual. The first line will count up and the second will hopefully move toward zero.

If the equations are ill-behaved the program may generate an error. Try a different set of initial guesses.

The program evals each expression at the current guess and at guess + delta for each variable. It fills out a matrix of the slopes and a vector of the values, uses the built-in linear algebra to solve the linear system, subtracts the result from the guesses, and repeats until the worst residual is less than the acceptable error.

%%HP: T(3)A(R)F(.); \<< 4 PICK SIZE DUP IDN DUP2 SWAP 1 \->LIST RDM 0 DUP DO DROP 1 + 0 3 ROLL 1 6 PICK FOR i 9 PICK i GET DUP \->NUM 3 ROLL i 1 \->LIST 3 PICK PUT 3 ROLLD 1 8 PICK FOR j 10 PICK j GET 9 PICK DUP2 STO+ 8 ROLL i j 2 \->LIST 6 PICK \->NUM 6 PICK - 12 PICK / PUT 8 ROLLD STO- NEXT ABS 4 ROLL MAX 3 ROLLD DROP NEXT DUP 5 PICK / 1 7 PICK FOR i 9 PICK i GET OVER i 1 \->LIST GET STO- NEXT DROP 3 ROLLD OVER 1 DISP DUP 2 DISP UNTIL DUP 8 PICK < END 9 ROLLD 8 DROPN \>>

-- END --

Here's a quick-and-dirty port of Mike's program to the HP-42S. It could use some work to give it a nice user interface -- entering the vectors with the function and parameter names is pretty awkward right now. See the TEST program for how to initialize the parameters etc.; it's basically identical to Mike's RPL program.
In case you want to run it on an emulator, save yourself the trouble of typing and load ses.raw instead; it contains the Simultaneous Equation Solver. You may also want to get sestest.raw, which contains the three functions for the example given above, plus a test program that shows how to run the SES.

- Thomas

00 { 221-Byte Prgm }
01>LBL "SES"
02 CF 21
03 STO 00
04 Rv
05 STO 01
06 Rv
07 STO "VARS"
08 Rv
09 STO "EQS"
10 STO "RES"
11 DIM?
12 Rv
13 ENTER
14 ENTER
15 NEWMAT
16 STO "DER"
17 Rv
18 1E3
19 ÷
20 1
21 +
22 STO 04
23 CLX
24 STO 02
25>LBL 00
26 1
27 STO+ 02
28 CLX
29 STO 03
30 RCL 04
31 STO 05
32>LBL 01
33 INDEX "EQS"
34 RCL 05
35 1
36 STOIJ
37 RCLEL
38 STO 07
39 INDEX "RES"
40 RCL 05
41 1
42 STOIJ
43 XEQ IND 07
44 STOEL
45 ABS
46 RCL 03
47 X<>Y
48 X>Y?
49 STO 03
50 RCL 04
51 STO 06
52>LBL 02
53 INDEX "VARS"
54 RCL 06
55 1
56 STOIJ
57 RCLEL
58 STO 08
59 RCL 00
60 STO+ IND 08
61 XEQ IND 07
62 RCL 00
63 STO- IND 08
64 INDEX "RES"
65 RCL 05
66 1
67 STOIJ
68 R^
69 RCLEL
70 -
71 RCL÷ 00
72 INDEX "DER"
73 RCL 05
74 RCL 06
75 STOIJ
76 Rv
77 Rv
78 STOEL
79 ISG 06
80 GTO 02
81 ISG 05
82 GTO 01
83 RCL "DER"
84 STO÷ "RES"
85 RCL 04
86 STO 05
87>LBL 03
88 INDEX "RES"
89 RCL 05
90 1
91 STOIJ
92 RCLEL
93 INDEX "VARS"
94 RCL 05
95 1
96 STOIJ
97 RCLEL
98 R^
99 STO- IND ST Y
100 ISG 05
101 GTO 03
102 CLA
103 RCL 02
104 AIP
105 |-"\LF"
106 ARCL 03
107 AVIEW
108 RCL 01
109 RCL 03
110 X>=Y?
111 GTO 00
112 CLD
113 END

00 { 37-Byte Prgm } 01>LBL "E1" 02 MVAR "X" 03 MVAR "Y" 04 MVAR "Z" 05 2 06 RCL "X" 07 3 08 Y^X 09 × 10 RCL+ "Y" 11 7 12 RCL× "Z" 13 + 14 96 15 - 16 END

00 { 40-Byte Prgm } 01>LBL "E2" 02 MVAR "X" 03 MVAR "Y" 04 MVAR "Z" 05 3 06 RCL× "X" 07 6 08 RCL "Y" 09 3 10 Y^X 11 × 12 + 13 2 14 RCL× "Z" 15 - 16 65 17 - 18 END

00 { 41-Byte Prgm } 01>LBL "E3" 02 MVAR "X" 03 MVAR "Y" 04 MVAR "Z" 05 -6 06 RCL× "X" 07 4 08 RCL× "Y" 09 + 10 2 11 RCL "Z" 12 3 13 Y^X 14 × 15 + 16 42 17 - 18 END

00 { 89-Byte Prgm } 01>LBL "TEST" 02 1 03 STO "X" 04 2 05 STO "Y" 06 3 07 STO "Z" 08 3 09 1 10 NEWMAT 11 ENTER 12 EDIT 13 "E1" 14 ASTO ST X 15 -> 16 "E2" 17 ASTO ST X 18 -> 19 "E3" 20 ASTO ST X 21 EXITALL 22 X<>Y 23 EDIT 24 "X" 25 ASTO ST X 26 -> 27 "Y" 28 ASTO ST X 29 -> 30 "Z" 31 ASTO ST X 32 EXITALL 33 1E-7 34 1E-7 35 XEQ "SES" 36 .END.