Showing posts with label characteristic polynomial. Show all posts
Showing posts with label characteristic polynomial. Show all posts

Saturday, August 1, 2026

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues




The programs are listed for the Swiss Micros DM42 and Hewlett Packard 42S.



All examples are on rounded to four decimal places and complex mode turned on (CPXRES).



Monic Quadratic Equation: quad2.raw



The program QUAD2 solves the equation for w:

w² + Y * w + X = 0

Y: contents of stack Y

X: contents of stack X



Code:

00 { 35-Byte Prgm }

01▸LBL "QUAD2"

02 X<>Y

03 ENTER

04 2

05 ÷

06 +/-

07 ENTER

08 X↑2

09 X<>Y

10 R↓

11 X<>Y

12 -

13 SQRT

14 R↑

15 X<>Y

16 ENTER

17 ENTER

18 R↑

19 ENTER

20 R↓

21 X<>Y

22 +

23 R↓

24 -

25 R↑

26 RTN

27 .END.



Example 1: w² + 7 * w + 5 = 0

Stack: Y: 7, X: 5

Results: -6.1926, -0.8074



Example 2: w² – 4 = 0

Stack: Y: 0, X: -4

Results: -2.0000, 2.0000



Characteristic Polynomial: char2.raw



The program CHAR2 find the coefficients of 2 x 2 matrix:

[ [ A, B ] [ C , D ] ]

Result: λ² – T1 * λ + T2 = 0 where

T1 = A + D

T2 = det([[ A, B ] [ C, D ]]) = A * D – C * B



The program provides prompts and results in messages.



Code:

00 { 81-Byte Prgm }

01▸LBL "CHAR2"

02 "[[A,B][C,D]]"

03 AVIEW

04 PSE

05 PSE

06 "L↑2-T1×L+T2=0"

07 AVIEW

08 PSE

09 PSE

10 INPUT "A"

11 ENTER

12 ENTER

13 INPUT "D"

14 ENTER

15 R↓

16 +

17 "T1= "

18 ARCL ST X

19 AVIEW

20 STOP

21 R↓

22 ×

23 INPUT "C"

24 INPUT "B"

25 ×

26 -

27 "T2= "

28 ARCL ST X

29 AVIEW

30 RTN

31 .END.



Example 1: [ [ -6, 4 ] [ 2, 3 ] ]

Results: T1: 3, T2: -26



Example 2: [ [ 5, 8 ] [ -1, 2 ] ]

Results: T1: 7, T2: 18



Eigenvalues of 2 x 2 Matrices: egn2.raw



The program EGNV2 calculates the eigenvalues of a 2 x 2 matrix:

[ [ A, B ] [ C , D ] ]



The program provides prompts and results in messages. The command CPXRES sets the calculator to accept complex number results.



Code:

00 { 106-Byte Prgm }

01▸LBL "EGNV2"

02 CPXRES

03 "[[A,B][C,D]]"

04 AVIEW

05 PSE

06 PSE

07 INPUT "A"

08 INPUT "B"

09 INPUT "C"

10 INPUT "D"

11 RCL "A"

12 RCL "D"

13 +

14 +/-

15 RCL "A"

16 RCL "D"

17 ×

18 RCL "B"

19 RCL "C"

20 ×

21 -

22 X<>Y

23 ENTER

24 2

25 ÷

26 +/-

27 ENTER

28 X↑2

29 X<>Y

30 R↓

31 X<>Y

32 -

33 SQRT

34 R↑

35 X<>Y

36 ENTER

37 ENTER

38 R↑

39 ENTER

40 R↓

41 X<>Y

42 +

43 "E1="

44 ARCL ST X

45 AVIEW

46 PSE

47 PSE

48 R↓

49 -

50 "E2="

51 ARCL ST X

52 AVIEW

53 PSE

54 PSE

55 R↑

56 RTN

57 .END.



Example 1: [ [ -6, 4 ] [ 2, 3 ] ]

Results: 3.8151, - 6.8151



Example 2: [ [ 5, 8 ] [ -1, 2 ] ]

Results: 3.5 ± 2.37792 i


You can download the three programs here: https://drive.google.com/file/d/1SKcVIhKYHnnuhrvBJxfOmwsV8sRTSj0X/view?usp=sharing





Eddie


All original content copyright, © 2011-2026. 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.

Saturday, June 28, 2025

fx-991 CW: Finding the Eigenvalues of a 2 x 2 Matrix

fx-991 CW: Finding the Eigenvalues of a 2 x 2 Matrix



Finding the Eigenvalues of a 2 x 2 Matrix


To find the eigenvalues of a matrix M, the characteristic polynomial must be solved in terms of λ:


det( M – λ * I) = 0


where:

M = a square matrix of dimension n x n

I = an identity matrix of size n x n.


The identity matrix is a square matrix in which all elements have a value 0 except the diagonal elements, which have the value 1.


A 2 x 2 identity matrix: [ [ 1, 0 ] [ 0, 1 ] ]

A 3 x 3 identity matrix: [ [ 1, 0, 0 ] [ 0, 1, 0 ] [ 0, 0, 1 ] ]


For a 2 x 2 matrix:


M = [ [ a, b ] [ c , d ] ]


The characteristic polynomial used to find the eigenvalues are:


det( M – λ * I) = 0

det( [ [ a, b ] [ c , d ] ] - λ * [ [ 1, 0 ] [ 0, 1 ] ] ) = 0

det( [ [ a, b ] [ c , d ] ] - [ [ λ, 0 ] [ 0, λ ] ] ) = 0

det( [ [ a - λ, b ] [ c , d - λ ] ] ) = 0

(a – λ) * (d – λ) – b * c = 0

λ^2 – (a + d) * λ + (a * d – b * c) = 0


Note:

trace(M) = a + d

det(M) = a * d – b * c


The characteristic equation to be solved is:


λ^2 – (a + d) * λ + (a * d – b * c) = 0

λ^2 – trace(M) * λ + det(M) = 0



Casio fx-991CW Algorithm


The algorithm will involve two apps: Matrix and Equation. Here is a way of finding the eigenvalues of a 2 x 2 matrices using only the fx-991 CW calculator without the need for writing anything down.


Note: The variables I use in the procedure is just for illustrative purposes. You can use any variables you want to designate the corner elements, the trace, and the determinant. The point is to be organized.


Variables used in this procedure:

A = upper-left element

B = lower-right element

C = trace = A + B

D = determinant


Settings: It is assumed that the MathI/MathO Input/Output mode and a+bi is selected for Complex result.


The screen shots are generated using the ClassPad Math (classpad.net) emulator for the fx-991CW and illustrates finding the eigenvalues of the matrix:


MatA = [ [ 4, 2 ] [ 5, 4 ] ]


Matrix App


Step 1: Press the [ HOME ] key, select the Matrix app.


Step 2: Use the TOOLS key to define a Matrix of dimension 2 x 2.


Step 3: Enter the matrix’s elements. Register each element is registered by using [ OK ] or [ EXE ]. Be careful not to press [ EXE ] or [ OK ] without entering a value first, as we need to keep the matrix editing screen up.


Step 4: Go to element (1,1) (upper-left hand corner), press the [ VARIABLE] key, go to variable A, press [ OK ], and select Store. Then go to element (2,2) (lower-right hand corner), press the [ VARIABLE] key, go to variable B, press [ OK ], and select Store.**


Step 5: Without entering or editing an element, press either [ EXE ] or [ OK ] to leave the matrix editor. When the message “Press [TOOLS] to define Matrix.” appears, we are in the calculation mode of the Matrix app.


Note: If you leave the matrix edit mode before storing the corner elements, you can go back into matrix edit mode by pressing [ TOOLS ], selecting your matrix, and then selecting Edit. You can check to see if corner values are stored by pressing [ VARIABLE ].





Step 6: Press [ SHIFT ] [ 4 ] (A) + [ SHIFT ] [ 7 ] ( B ) [ EXE ]. This calculates the matrix’s trace. Then press [ VARIABLE ], choose C, press [ OK ], choose Store.


Step 7: Press [ CATALOG ], select the Matrix sub-menu, then the Matrix Calc sub-menu, and select Determinant. Then use the catalog to grab the matrix and press [ EXE ] to calculate the matrix. Use the variable list to store the value in D (similar procedure in Step 6).





When transferring between apps, the values stored in the memory registers A-F, x, y, and z are retained, even when the fx-991CW is turned off.


Equations App


Step 8: Press [ HOME ] and select the Equation app and press [ OK ]. Select Polynomial, ax²+bx+c (2nd order polynomial, quadratic equation).


Step 9: Enter the following coefficients: 1 x² – C x + D (note the minus sign on C).


Step 10: Press [ OK ]. The first eigenvalue is displayed. Press the down arrow ([↓]) to get the other eigenvalue. 

 An optional step is to use the [ VARIABLE ] key to store the results (like in E or F, for example). 

 Another optional step is to press [ FORMAT ], select Decimal to see the decimal approximation.





The results are:


C = trace = 8

D = determinant = 6

Eigenvalues:

λ1 = 4 + √10

λ2 = 4 - √10



Other Examples



Find the eigenvalues of Mat B = [ [ -8, 1 ] [ 16, 7 ] ]


Results:

C = trace = -1

D = determinant = -72

Eigenvalues:

λ1 = 8

λ2 = -9





Find the eigenvalues of Mat C = [ [ -5, -7 ] [ 3, - 2] ]


Results:

C = trace = -7

D = determinant = 31

Eigenvalues:

λ1 = (-7 + 5 * √3 * i) / 2

λ2 = (-7 - 5 * √3 * i) / 2




I hope you find this useful and beneficial. Until next time,


Eddie


All original content copyright, © 2011-2025. 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.

Saturday, November 4, 2023

HP 15C and Casio fx-9750GIII: Solving Recurrence Relations

HP 15C and Casio fx-9750GIII:   Solving Recurrence Relations


Introduction


The following program attempts to find a closed-form formula to the recurrence relation:


(I)

a_n + P × a_n-1 + Q × a_n-2 = 0


or 


(II)

a_n = -P × a_n-1 + -Q × a_n-2


Without loss of generality, usually P and Q in the latter form (II) are presented as positive constants.  


Two initial conditions are provided, a_0 and a_1 (where n = 0 and n = 1, respectively).  


Equation (I) can be transformed into the characteristic equation:


x^2 + P × x + Q = 0


where the roots are:


x = ( -P ± √(P^2 - 4 × Q)) ÷ 2


and the roots are x = S and x = T.  Assume that the roots S and T are real numbers.


If S ≠ T, the solution is in the form of:


a_n = B × S^n + C × T^n


where B and C are determined by the equations:

a_0 = B + C

a_1 = B × S + C × T


If S = T, the solution is in the form of:


a_n = B × S^n + C × n × S^n


where B and C are determined by the equations:

a_0 = B

a_1 = S × (B + C)




HP 15C:  Solving Recurrence Relations 


Store values to the following registers:


R1 = P 

R2 = Q

R3 = I 

R4 = J


Output registers:


R5 = S

R6 = T

R7 = B

R8 = C


Code:

Step :  Key :  Code


001 :  LBL A :  42,21,11    (solve for S, T)

002 :  RCL 1 :  45,1

003 :  x^2 :  43,11

004 :  4  :   4

005 : RCL× 2 : 45,20, 2

006 : - :  30

007 : √  : 11

008 : STO 0 : 44, 0

009 : RCL- 1 : 45,30, 1

010 : 2 : 2

011 : ÷ : 10

012 : STO 5 : 44, 5

013 : RCL- 0 : 45,30, 0

014 : STO 6 : 44, 6

015 : RCL 5 : 45, 5

016 : TEST 6 : 43,30, 6  (x ≠ y)

017 : GTO 1 : 22, 1


018 : RCL 3 : 45, 3  (if S = T, solve for B, C)

019 : STO 7 : 44, 7

020 : RCL 4 : 45, 4

021 : RCL 5 : 45, 5

022 : RCL× 7 : 45,20, 7

023 : - : 30

024 : RCL÷ 5 : 45,10, 5

025 : STO 8 : 44, 8

026 : GTO 2 : 22, 2


027 : LBL 1 : 42,21, 1  (if S≠T, solve for B, C)

028 : RCL 6 : 45, 6

029 : RCL× 3 : 45,20, 3

030 : RCL- 4 : 45,30, 4

031 : RCL 6 : 45, 6

032 : RCL- 5 : 45,30, 5

033 : ÷ : 10

034 : STO 7 : 44, 7

035 : RCL 5 : 45, 5

036 : CHS : 16

037 : RCL× 3 : 45,20, 3

038 : RCL+ 4 : 45,40, 4

039 : RCL 6 : 45, 6

040 : RCL- 5 : 45,30, 5

041 : ÷ : 10

042 : STO 8 : 44, 8


043 : LBL 2 : 42,21, 2 (view results)

044 : RCL 7 : 45, 7

045 : R/S : 31

046 : RCL 5 : 45, 5

047 : R/S : 31

048 : RCL 8 : 45, 8

049 : R/S : 31

050 : RCL 6 : 45, 6

051 : RTN : 45, 32


Casio fx-9750GIII Program: RCHAR


Text file listing:


'ProgramMode:RUN

"2023_-_09_-_26 EWS"

ClrText

"SOLVE"

"A(N) _+_ P_*_A(N_-_1) "

"_+_ Q_*_A(N_-_2) = 0"

"P"?->P

"Q"?->Q

"A(0)"?->I

"A(1)"?->J

(-P+Sqrt(P^<2>-4Q))/2->S

(-P-Sqrt(P^<2>-4Q))/2->T

If S<>T

Then 

(T*I-J)/(T-S)->B

(-S*I+J)/(T-S)->C

ClrText

Locate 1,3,"B_*_S_^_N _+_ C_*_T_^_N"

Else 

I->B

(J-S*B)/S->C

ClrText

Locate 1,3,"B_*_S_^_N _+_ C_*_N_*_T_^_N"

IfEnd

Locate 1,4,"B="

Locate 4,4,B

Locate 1,5,"S="

Locate 4,5,S

Locate 1,6,"C="

Locate 4,6,C

Locate 1,7,"T="

Locate 4,7,T



Notes:  

This is the text file from Casio fx-9750GIII.  

_:  space

->:  store  (→)

^<2>:  ^2

Sqrt:  √

<>: ≠


Examples


1.  a_n - 11 × a_n-1 + 24 = 0,  a_0 = 3, a_1 = 8


Characteristic Equation:  x^2 - 11x + 24 = 0


Roots:  S = 3, T =-8


Different Roots


3 = B + C

8 = 3 × B - 8 × C


B = -0.2, C = 3.2


Solution:


a_n = -0.2  × 8^n + 3.2 × 3^n



2.  a_n + 6 × a_n-1 + 9 × a_n-2 = 0, a_0 = 2, a_ 11


Characteristic Equation:  x^2 + 6x + 9 = 0


Roots:  S = T = -3


Same Roots


2 = B 

10 = -3 × (B + C)


B = 2, C = -16/3 ≈ -4.3333


Solution:


a_n = 2 × (-3)^n - 16/3 × n × (-3)^n



3.  a_n - 6 × a_n-1 + 4 × a_n-2 = 0, a_0 = 1, a_1 = 15


Characteristic Equation:  x^2 - 6x + 4 = 0


Roots:  

S = 3 + √5 ≈ 5.236067977

T = 3 - √5 ≈ 0.7639320225


Different Roots


1 = B + C

15 = (3 + √5) × B - (3 - √5) × C


B ≈ 3.183281573

C ≈ -2.183281573


Solution:


a_n ≈

3.183281573 × 5.236067977^n - 2.183281573 × 0.7639320225^n



Source


Levin, Oscar.  "2.4 Recurrence Relations"  Discrete Mathematics: An Open Introduction  openmathbooks.org  University of Northern Colorado.   Retrieved August 30, 2023.  https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Discrete_Mathematics_(Levin)/2%3A_Sequences/2.4%3A_Solving_Recurrence_Relations


Eddie


All original content copyright, © 2011-2023.  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. 


Saturday, February 9, 2013

Greetings From Monrovia! Finding a Formula to Generate the Fibonacci Sequence

Hi everyone. I am blogging from the Friends Café in downtown Monrovia, CA on a beautiful, chilly day.

Today's blog entry is about to the Fibonacci Sequence and how to find a general formula to generate a sequence.

The Fibonacci Sequence

The world famous Fibonacci Sequence, first introduced by Fibonacci's Liber Abaci, written in 1202, is:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 293, 377...

(Source: Pickover, Clifford. "The Math Book" Sterling Publishing, New York. 2009 - Eddie says: Go get this book - it's awesome!)

It is easy to build the sequence. Start with two entries of 1. Add them up to get the next term. Each subsequent term is the sum of the last two. 1+1=2, 1+2=3, 2+3=5, 3+5=8, 5+8=13, and so on.

We can use this recursion formula:

a_(n+2) = a_(n+1) + a_n

with the initial conditions a_0 = 1 and a_1 = 1.

We can generate a general formula for this.


Technique

Start with the recursion formula:

a_(n+2) = a_(n+1) + a_n

with the initial conditions a_0 = 1 and a_1 = 1.

This is a linear recursion formula. A way to find the general formula is to use a characteristic polynomial. The characteristic polynomial is formed by turning subscripts to exponents (and usually replacing a with another letter, such as x) and finding the roots of the polynomial.

The general formula will have the form

a_n = c_1 * (x_1)^n + c_2 * (x_2)^n + ...

Where n is the number of initial conditions.

In the case of the Fibonacci sequence, we have two initial conditions (a_0 = 1 and a_1 = 1). Hence n=2.

Then use the initial conditions to solve for the c_k constants.


Finding the General Formula

a_(n+2) = a_(n+1) + a_n
with the initial conditions a_0 = 1 and a_1 = 1.

The characteristic equation is:

x^(n+2) = x^(n+1) + x^n

Assuming x ≠ 0, divide both sides by x^n:

x^2 = x + 1

x^2 - x - 1 = 0

The roots of the polynomial are:

x_1 = (1 + √5)/2
x_2 = (1 - √5)/2

Then our general form has:

a_n = c_1 * ((1 + √5)/2)^n + c_2 * ((1 - √5)/2)^n

Our next step is to find c_1 and c_2.

Note when n = 0, a_0 = 1 and:

1 = c_1 + c_2

When n = 1, a_1 = 1 and:

1 = c_1 * (1 + √5)/2 + c_2 * (1 - √5)/2

Leaving us with the system of linear equations:

c_1 + c_2 = 1
c_1 * (1 + √5)/2 + c_2 * (1 - √5)/2 = 1

I like using matrices to solve systems of linear equations, you may a different preferred way, it's all good here:

[ [1, 1], [(1 + √5)/2, (1 - √5)/2] ] * [ [c_1], [c_2] ] = [ [ 1 ], [ 1 ] ]

[ [c_1], [c_2] ] = [ [1, 1], [(1 + √5)/2, (1 - √5)/2] ]^-1 * [ [ 1 ], [ 1 ] ]

[ [c_1], [c_2] ] = [ [(5 - √5)/10, √5/5], [(5 + √5)/10, -√5/5] ] * [ [ 1 ], [ 1 ] ]

which evaluates and simplifies to:

c_1 = (5 + √5)/10
c_2 = (5 - √5)/10

The general formula to generate the Fibonacci sequence is:

a_n = (5 + √5)/10 * ((1 + √5)/2)^n + (5 - √5)/10 * ((1 - √5)/2)^n


An approximate formula would be (8 digits):

a_n ≈ 0.72360680 * 1.6180339^n + 0.27639320 * (-0.6180339)^n

Round off when necessary.

Testing the general formula, find the 2nd and 6th term:

a_2 = (5 + √5)/10 * ((1 + √5)/2)^2 + (5 - √5)/10 * ((1 - √5)/2)^2
a_2 = (5 + √5)/5 + (5 - √5)/2
a_2 = 10/2 = 2

a_6 = (5 + √5)/10 * ((1 + √5)/2)^6 + (5 - √5)/10 * ((1 - √5)/2)^6
a_6 = (65 + 29 * √5)/10 + (65 - 29 * √5)/10
a_6 = 130/10 = 13


A Broader Problem

The technique applied can be applied to the general problem:

a_(n+2) = S * a_(n+1) + T * a_n

With initial conditions a_0 = A and a_1 = B.

Using the trusty HP 50g to assist me, I come up with the following:

a_n = c_1 * (x_1)^n + c_2 * (x_2)^n

where

x_1 = (S + √(S^2 + 4 * T))/2

x_2 = (S - √(S^2 + 4 * T))/2

c_1 = (B - A * x_2)/(x_1 - x_2)

c_2 = (A * x_1 - B)/(x_1 - x_2)


Next time, I am going to look at the general recursion formula:

a_(n+1) = S * a_n + T with the initial condition a_0 = A

I am working a basic programming series, which my target is with the HP 39gii calculator and the series would be posted in March 2013.

Thank you everyone who reads, follows, and comments on this blog. As always it is much appreciated.

Have a great day!

Eddie


This blog is property of Edward Shore. 2013

Python in Numworks: Duplicating and Grayscale

Python in Numworks: Duplicating and Grayscale All three scripts presented today use the math, random, and the Numworks specific ...