Re: Simul. Solve w/ HP 33s Message #8 Posted by martin cohen on 2 Aug 2004, 9:19 p.m., in response to message #7 by Norris
Here is my transcription (from my 33s) of my routines for solving 2x2 and 3x3 linear systems.
===========================================================
These are my routines for solving 2x2 and 3x3 linear equations.
They use Cramer's rule (ratio of determinants) to solve
the equations. The coefficients are not changed
when the equations are solved. This is useful on the
33s, where so few values can be stored.
The routines are:
S: Solve 3x3 linear system
(A,B,C;E,F,G;I,J,K).(X,Y,Z)=(D,H,L)
det in W
Result in W (det), X, Y, Z;
Display is detin X, then X, Y, Z
Rv if display != 0 to see X, Y, Z
T: Solve 2x2 linear system
A*X+B*Y=D, E*X+F*Y=H
Det in W; displayed when computed
Result in X and Y and regs
Note these are a subset of the 3x3 systems,
instead of the expected
A*X+B*Y=C, D*X+E*Y=F
U: Get det(A,B,C;E,F,G;I,J,K)
V: Swap column (D;H;L) with col (X-reg)
I: Input A..L (uses label J)
Inputs if X>0; clears if X=0; sets to random if X<0.
Note: "Rv" means "rotate down", "!=" means "not equal to",
"det" means "determinant".
Comments indicated by "//".
S: Solve 3x3 linear system
(A,B,C;E,F,G;I,J,K).(X,Y,Z)=(D,H,L)
det in W
Result in W (det), X, Y, Z;
Display is det in X-reg, then X, Y, Z
Rv if display != 0 to see X, Y, Z
Or, in matrix form,
(A B C) (X) (D)
(E F G). (Y) = (H)
(I J K) (Z) (L)
LBL S
XEQ U STO W // GET DET
1 XEQ V XEQ U STO X // GET DET SWAPPING COL 1 WITH (D,H,L)
1 XEQ V // RESTORE MATRIX
2 XEQ V XEQ U STO Y // SAME FOR COL 2
2 XEQ V
3 XEQ V XEQ U STO Z // SAME FOR COL 3
3 XEQ V
RCL W x!=0? STO/ X // IF DET NOT ZERO, SET X
x!=0? STO/ Y // SAME FOR Y
x!=0? STO/ Z // SAME FOR Z
RCL Z RCL Y RCL X RCL W // GET RESULTS
RTN // DONE
T: Solve 2x2 linear system
A*X+B*Y=D, E*X+F*Y=H
Det in W; displayed when computed
Result in X and Y and regs
Note these are a subset of the 3x3 systems,
instead of the expected
A*X+B*Y=C, D*X+E*Y=F
LBL T
A*F-B*E
STO W // SAVE THE DET
VIEW W // SHOW IT
D*F-B*H // DET FOR X
STO X // STORE IT
A*H-D*E // DET FOR Y
STO Y // STORE IT
RCL W // GET DET
x!=0? STO/ X // IF NON-ZERO, GET TRUE X
x!=0? STO/ Y // IF NON-ZERO, GET TRUE Y
RCL Y RCL X RCL W // GET RESULTS
RTN // DONE
U: Get det(A,B,C;E,F,G;I,J,K)
The matrix is:
| A B C |
| E F G |
| I J K |
LBL U
RCL F RCL* K // F*K
RCL G RCL* J // G*J
- RCL* A // A*(F*K-G*J)
RCL B RCL* K // B*K
RCL C RCL*J // C*J
- RCL* E - // THAT - E*(B*K-C*J)
RCL B RCL* G // B*G
RCL C RCL* F // C*F
- RCL* I + // THAT + I*(B*G-C*F)
RTN // THAT'S THE DET
V: Swap column (D;H;L) with col (X-reg)
LBL V
STO i // STORE COLUMN INDEX (1=(A,E,I), 2=(B,F,G), 3=C,G,E))
RCL D x<>(i) STO D // SWAP D WITH FIRST ROW
4 STO+ i // POINT TO NEXT ROW
RCL H x<>(i) STO H // SWAP H WITH SECOND ROW
4 STO+ i // POINT TO NEXT ROW
RCL L x<>(i) STO L // SWAP L WITH THIRD ROW
RTN // THAT'S IT
I: Input A..L (uses label J)
Inputs if X>0; clears if X=0; sets to random if X<0.
LBL I
STO X // SAVE INPUT
1.012 STO i // SET LOOP CONTROL
LBL J
RCL X // GET CONTROLLER
x>0? INPUT (i) // IF > 0, ASK FOR VALUE
x<0? RANDOM // IF < 0, GET A RANDOM VALUE
STO (i) // STORE WHAT YOU GOT
ISG i GTO J // LOOP
RTN // DONE AFTER 12
K: Check one equation – i=0 for first
LBL K
1 STO+ i // POINT TO ROW ELEMENT
RCL X RCL* (i) // X*THAT
1 STO+ i Rv // POINT TO NEXT IN ROW
RCL Y RCL* (i) + // ADD Y*THAT
1 STO+ i Rv // POINT TO NEXT IN ROW
RCL Z RCL* (i) + // ADD Z*THAT
1 STO+ i Rv // POINT TO NEXT IN ROW
RCL- (i) // SUBTRACT RIGHT SIDE
RTN // THAT'S THE DIFFERENCE
Martin Cohen
|