Showing posts with label 3 x 3 linear system. Show all posts
Showing posts with label 3 x 3 linear system. Show all posts

Saturday, November 9, 2019

HP 35S: Determinant and Cramers Rule - 3 x 3 Matrices

HP 35S: Determinant and Cramers Rule - 3 x 3 Matrices

Determinant of a 3 x 3 Matrix

The following program calculates a determinant of a matrix:

[ [ K, N, Q ]
[ L, O, R ]
[ M, P, S ] ]

The determinant is  K*O*S + N*R*M + Q*L*P - M*O*Q - P*R*K - S*L*N.

Enter the elements in columns. 

Program HP 35S:  Determinant

D001 LBL D
D002 SF 10
D003 "DET 3x3"
D004 CF 10
D005 INPUT K
D006 INPUT L
D007 INPUT M
D008 INPUT N
D009 INPUT O
D010 INPUT P
D011 INPUT Q
D012 INPUT R
D013 INPUT S
D014 RCL K
D015 RCL* O
D016 RCL* S
D017 RCL N
D018 RCL* R
D019 RCL* M
D020 +
D021 RCL Q
D022 RCL* L
D023 RCL* P
D024 +
D025 RCL M
D026 RCL* O
D027 RCL* Q
D028 -
D029 RCL P
D030 RCL* R
D031 RCL* K
D032 -
D033 RCL S
D034 RCL* L
D035 RCL* N
D036 -
D037 RTN

Examples:

[ [ -3, 3, 2 ]
[ 5, 4, -1 ]
[ 2, 1, 4 ] ]
Determinant:  -123

[ [ 5, 0, 7 ]
[ -2, 4, -1 ]
[ -3, 11, 6 ] ]
Determinant:  105

Cramer's Rule

Cramer's Rule solves the linear system:

[[ A, D, G ]   [[ x ]  = [[ X ]
[ B, E, H ]     [ y ]  = [ Y ]
[ C, F, I ]]      [ z ]] = [ Z ]]

x = U, y = V, z = W,  T = determinant of the coefficients

Program HP 35S: Cramer's Rule

C001 LBL C
C002 GTO C027
C003 RCL K     // determinant calculation
C004 RCL* O
C005 RCL* S
C006 RCL N
C007 RCL* R
C008 RCL* M
C009 +
C010 RCL Q
C011 RCL* L
C012 RCL* P
C013 +
C014 RCL M
C015 RCL* O
C016 RCL* Q
C017 -
C018 RCL P
C019 RCL* R
C020 RCL* K
C021 - 
C022 RCL S
C023 RCL* L
C024 RCL* N
C025 - 
C026 RTN
C027 SF10  // input numbers into the system
C028 "COL 1"
C029 INPUT A
C030 STO K
C031 INPUT B
C032 STO L
C033 INPUT C
C034 STO M
C035 "COL 2"
C036 INPUT D
C037 STO N
C038 INPUT E
C039 STO O
C040 INPUT F
C041 STO P
C042 "COL 3"
C043 INPUT G
C044 STO Q
C045 INPUT H
C046 STO R
C047 INPUT I
C048 STO S
C049 "VECTOR" 
C050 INPUT X
C051 INPUT Y
C052 INPUT Z
C053 XEQ C003
C054 STO T
C055 "DET="
C056 VIEW T
C057 RCL X
C058 STO K
C059 RCL Y
C060 STO L
C061 RCL Z
C062 STO M
C063 XEQ C003
C064 RCL÷ T
C065 STO U
C066 "X="
C067 STOP
C068 RCL A
C069 STO K
C070 RCL B
C071 STO L
C072 RCL C
C073 STO M
C074 RCL X
C075 STO N
C076 RCL Y
C077 STO O
C078 RCL Z
C079 STO P
C080 XEQ C003
C081 RCL÷ T
C082 STO V
C083 "Y="
C084 STOP
C085 RCL D
C086 STO N
C087 RCL E
C088 STO O
C089 RCL F
C090 STO P
C091 RCL X
C092 STO Q
C093 RCL Y
C094 STO R
C095 RCL Z
C096 STO S
C097 XEQ C003
C098 RCL÷ T
C099 STO W
C100 "Z="
C101 CF 10
C102 TOP
C103 RTN

Examples:

[[ -3, 2, -4 ]   [[ x ]  = [[ 0 ]
[ 6, 1, 2 ]       [ y ]  = [ 2 ]
[ 3, 3, 7 ]]      [ z ]] = [ 6 ]]
T:  -135
x ≈ 0.0296
y ≈ 0.9333
z ≈ 0.4444

[[ 0, 10, 6 ]  [[ x ] = [[ 3 ]
[ 5, 3, 8 ]     [ y ]  = [ 6.5 ]
[ -5, 8, 2 ]]    [ z ]] = [ 7 ]]
T:  -830
x ≈ -0.0843
y ≈ 0.6687
z ≈ 0.6145

Source:

Pike, Scott.  "Using Cramer's Rule to Solve Three Equations with Three Unknowns"  Mesa Community College.  http://www.mesacc.edu/~scotz47781/mat150/notes/cramers_rule/Cramers_Rule_3_by_3_Notes.pdf  Retrieved September 24, 2019

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Thursday, June 30, 2016

Fun With the HP 71B III

Fun With the HP 71B III


3x3 Matrices: Determinant, Inverse, 3x3 Linear Systems
EWS 6/29/2016

The program MATX3 calculates:

1. The determinant and (if possible), the inverse of a 3x3 matrix M.
2. The solution to a 3x3 linear system: Mq=D. The determinant of M will also be displayed.

If det(M) = 0, then the matrix is singular and execution stops.

The matrix M is broken into three columns (3x1 arrays): [ M ] = [ A | B | C ].

Hence M = [[ A1 B1 C1 ] [ A2 B2 C2 ] [ A3 B3 C3 ]]

Other variables used:
E = det(M)
I = M^-1. Unlike M, I will be a 3 x 3 array.
R, K, S, H: other variables used

Program MATX3 (767 bytes)
10 DESTROY A,B,I,C,R,K,S,H,D,Q
11 DISP “1. DET/INV  2. 3x3” @ WAIT 2
12 INPUT “1. D/I 2. SYS:”; H
13 DIM A(3),B(3),C(3),I(3,3),D(3)
14 OPTION BASE 1
20 FOR K=1 TO 3
21 DISP “ROW “;K @ WAIT 1
22 INPUT “A:”; A(K)
24 INPUT “B:”; B(K)
26 INPUT “C:”; C(K)
28 IF H=2 THEN INPUT “D:”; D(K)
30 NEXT K
40 DEF FND(X,Y,Z,T)=X*T-Y*Z
42 E=A(1)*FND(B(2),C(2),B(3),C(3))
44 E=E-B(1)*FND(A(2),C(2),A(3),C(3))
46 E=E+C(1)*FND(A(2),B(2),A(3),B(3))
50 DISP “DET:”; E
52 IF E=0 THEN STOP
60 I(1,1)=FND(B(2),B(3),C(2),C(3))/E
62 I(1,2)=-FND(B(1),B(3),C(1),C(3))/E
64 I(1,3)=FND(B(1),B(2),C(1),C(2))/E
66 I(2,1)=-FND(A(2),A(3),C(2),C(3))/E
68 I(2,2)=FND(A(1),A(3),C(1),C(3))/E
70 I(2,3)=-FND(A(1),A(2),C(1),C(2))/E
72 I(3,1)=FND(A(2),A(3),B(2),B(3))/E
74 I(3,2)=-FND(A(1),A(3),B(1),B(3))/E
76 I(3,3)=FND(A(1),A(2),B(1),B(2))/E
78 IF H=2 THEN 100
80 FOR R=1 TO 3
82 FOR S=1 TO 3
84 DISP “I(“; R; “,”; S; “):”; I(R,S)
86 PAUSE
88 NEXT S
90 NEXT R
92 STOP
100 DIM Q(3)
102 FOR K=1 TO 3
104 Q(K)= I(K,1)*D(1) + I(K,2)*D(2) + I(K,3)*D(3)
106 DISP “Q”; K; “:”; Q(K) @ PAUSE
108 NEXT K

Example:

M = [[ 1, 2, -8 ] [ 0, -2, 9.5 ] [ 3.2, 2.7, -1 ]]
D = [[ 0.5 ] [ 1.5 ] [ 2.5 ]]

DET = -14.05
I ≈ [[ 1.6833, 1.3950, -0.2135 ] [ -2.1637, -1.7509, 0.6762 ] [ -0.4555, -0.2633, 0.1423 ]]

Solutions:
Q ≈ [[ 2.4004 ] [ -2.0178 ] [ -0.2669 ]]

Days From January 1, Days Between Dates
HP 71B – Day Counts

Number of Days from January 1

D = Day
M = Month
L = Leap Year indicator (1 if the year is a leap year, 0 if it is not)

Program DAYJAN1 (183 bytes)
10 DESTROY D,M,L,N
12 INPUT “MONTH:”; M
14 INPUT “DAY:”; D
16 INPUT “LEAP? (Y=1,N=0):”; L
20 IF M>2 THEN 28
21 REM M<=2
22 N=IP(30.6*(M+13))+D-429
24 GOTO 32
27 REM M>2
28 N=IP(30.6*(M+1))+D+L-64
32 DISP “# DAYS:”; N

Test 1: January 1 to May 29, non-leap year (L=0). Result: 148
Test 2: January 1 to May 29, leap year (L=1). Result: 149

Days between Dates

M, D, Y: Month, Day, four-digit year

Program DDAYS (299 bytes)
10 DESTROY M1, M2, D1, D2, Y1, Y2, F1, F2
11 DESTROY F,M,D,Y,N,X,Z
12 INPUT “1: M,D,Y:”; M1, D1, Y1
13 INPUT “2: M,D,Y:”; M2, D2, Y2
15 M=M1 @ D=D1 @ Y=Y1
17 GOSUB 40
19 F1=F
21 M=M2 @ D=D2 @ Y=Y2
23 GOSUB 40
25 F2=F
27 N=F2-F1
29 DISP “# DAYS:”; N
31 STOP
40 IF M>2 THEN X=IP(.4*M+2.3) ELSE X=0
42 IF M>2 THEN Z=Y ELSE Z=Y-1
44 F=365*Y+31*(M-1)+D+IP(Z/4)-X
46 RETURN

Test 1: January 2, 2015 to March 17, 2016 (1,2,2015 to 3,17,2016). Result: 440
Test 2: March 14, 1977 to June 29, 2016 (3,14,1977 to 6,29,2016). Result: 14352

Great Circle Distance

N: Longitude
E: Latitude

Separate Degrees (H), Minutes (M), and Seconds (S) during input
Program GRCIC (367 bytes)
10 DESTROY D,M,H,S
11 DESTROY N1,N2,E1,E2,G
12 DEGREES
14 DISP “N: LATITUDE” @ WAIT 1
16 DISP “E: LONGITUDE” @ WAIT 1
20 INPUT “N1: D,M,S:”; H,M,S
21 GOSUB 50
22 N1=D
25 INPUT “E1: D,M,S:”; H,M,S
26 GOSUB 50
27 E1=D
30 INPUT “N2: D,M,S:”; H,M,S
31 GOSUB 50
32 N2=D
35 INPUT “E2: D,M,S:”; H,M,S
36 GOSUB 50
37 E2=D
40 REM CALCULATION
41 G=SIN(N1)*SIN(N2)+COS(N1)*COS(N2)*COS(E1-E2)
42 G=ACOS(G)*3959*PI/180
44 DISP “DIST: “; G; “ MI”
46 STOP
50 D=SGN(H)*(ABS(H)+M/60+S/3600)
52 RETURN

Test:
Los Angeles, N = 34°13’0” and E = -118°15’0”
San Francisco, N = 37°47’0” and E = -112°25’0”
Distance ≈ 408.5961 mi

Euclid Division – Finding the GCD

Finds the GCD (greatest common divisor) between integers M and N.  The program displays a “calculating” screen while the calculation is in process.

Program EUCLID (143 bytes)
10 DESTROY M,N,A,B,C
15 INPUT “M,N:”; M,N
20 IF M>N THEN A=M  @ B=N
22 IF M<N THEN A=N @ B=M
30 C= A – IP(A/B)*B
35 DISP C;B;A   // this is the “busy” indicator
40 IF C=0 THEN 50
45 A=B @ B=C @ GOTO 30
50 DISP “GCD:”; B

Test 1:  M=144, N=14;  Result: 2
Test 2:  N=14, M=144;  Result: 2

Fractions:  Addition and Multiplication

Adds or multiplies two fractions W/X and Y/Z.  Gives the result in the simplest form.  Proper or improper fractions only.

Separate each part with a comma (W,X,Y,Z) as prompted.

Program FRAC (380 bytes)
10 DESTROY W,X,Y,Z,N,D,H,A,B,C
20 INPUT “1. + 2. *:”,H
24 ON H GOTO 41,51
41 REM ADD
42 INPUT “W/X+Y/Z:”; W,X,Y,Z
44 N=W*Z+X*Y @ D=X*Y
46 GOSUB 61
48 DISP “=”; N; “/”; D @ STOP
51 REM MULT
52 INPUT “W/X*Y/Z:”; W
54 N=W*Y @ D=X*Z
56 GOSUB 61
58 DISP “=”; N; “/”; D @ STOP
61 REM SIMPLIFY
62 IF N>D THEN A=N @ B=D
64 IF D>N THEN A=D @ B=N
66 IF D=N THEN N=1 @ D=1 @ RETURN
68 C= A – IP(A/B)*B
69 DISP C;B;A
70 IF C=0 THEN 74
72 A=B @ B=C @ GOTO 68
74 N=N/B @ D=D/B @ RETURN

Test 1:  4/7 + 3/13;   Input:  4,7,3,13.  Result:  73/91
Test 2: 4/7 * 3/13; Input: 4,7,3,13.  Result:  12/91

Statistics: Regression

The program CURVEFIT fits data to one of five regression models:

1.  Linear Regression:  y = a + bx
2.  Exponential Regression:  y = a * e^(b*x)
3.  Logarithm Regression: y = a + b * ln x
4.  Power Regression:  y = a * x^b
5.  Inverse Regression:  y = a + b/x

On the HP 71, the ln function is represented by LOG.

The program will allow different calculations with the same data set.

Program CURVEFIT (622 bytes)
10 DESTROY H,S,D,X,Y,A,B,E
12 STAT S(2) @ CLSTAT
20 REM CHOOSE REG
22 DISP “1. LIN 2. EXP 3. LOG” @ WAIT 1.5
24 DISP “4. POW 5. INV” @ WAIT 1.5
28 INPUT “CHOICE #:”; H
29 IF E=1 THEN 60
30 REM INPUT PROCESS
32 INPUT “X,Y:”; X,Y
34 ON H GOTO 36,38,40,42,44
36 ADD X,Y @ GOTO 46
38 ADD X,LOG(Y) @ GOTO 46
40 ADD LOG(X),Y @ GOTO 46
42 ADD LOG(X),LOG(Y) @ GOTO 46
44 ADD 1/X,Y @ GOTO 46
46 INPUT “DONE? (Y=1,N=0):”; D
48 IF D=0 THEN 32
60 LR 2,1,A,B
62 IF H=2 OR H=4 THEN A=EXP(A)
64 ON H GOTO 70,72,74,76,78
70 DISP A; “+”; B; “x” @ GOTO 80
72 DISP A; “*EXP(“; B; “x)” @ GOTO 80
74 DISP A; “+”; B; “*LOG(x)” @ GOTO 80
76 DISP A; “x^”; B @ GOTO 80
78 DISP A; “+”; B; “/x” @ GOTO 80
80 PAUSE
84 DISP “ANOTHER ANALYSIS?” @ WAIT 1.5
86 INPUT “Y=1,N=0:”; E
88 IF E=1 THEN 20
90 DISP “DONE”


 Have fun!  See you in the second half of 2016!  

Eddie

This blog is property of Edward Shore, 2016

TI-84 Plus CE: Logistic Map

TI-84 Plus CE: Logistic Map It feels like forever since I with the TI-84 Plus CE. Introduction The program LOGISTIC plots ...