Showing posts with label complex numbers. Show all posts
Showing posts with label complex numbers. 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.

Sunday, May 3, 2026

HP 65 Programs: Triangulation, Percentile, Roots of Unity, Partial Fractions

 HP 65 Programs: Triangulation, Percentile, Roots of Unity, Partial Fractions


Triangulation

(done with HP-65 Emulator for Windows, Bernhard Emese)




Given:

Side angle α

Side angle ß

r = Length of base

Find:

h = height which crosses the base line at right angles



Code:

23: LBL

12: B

35: g

41: DEG

33 01: STO 1

35 08: R↓

41: ENTER

41: ENTER

44: CLX

51: -

31: f

04: SIN

35 09: R↑

31: f

04: SIN

71: *

33: STO

71: *

01: 1

35 08: R↓

61: +

31: f

04: SIN

33: STO

81: ÷

01: 1

34 01: RCL 1

24: RTN



Example: ß = 50°, α = 30°, r = 10

Result: h ≈ 3.89


Source:

“Triangulation (surveying)” Wikipedia. https://en.wikipedia.org/wiki/Triangulation (surveying) (last edited October 24, 2025). Retried March 17, 2026


Percentile in a Range


The program calculates the percentile of x in the range [a, b].


percentile = (x – a) ÷ (b – a) * 100%


Stack:

Z: x

Y: a

X: b


23: LBL

11: B

35 07: x<>y

51: -

35 00: LSTx

35 07: R↓

51: -

35 09: R↑

81: ÷

02: 2

32: f^-1

08: LOG (10^x)

71: ×

24: RTN


Example: The range: [210, 470], x = 376

Stack: Z: 376, Y: 210, X: 470

Result: 63.85



Roots of Unity

(done with HP-65 Emulator for Windows, Bernhard Emese)


w^n = 1

Registers used: R1 = n, R8 = n (counter)


n [ A ]


Cycle:

root # [ R/S ]

real part [ R/S ]

imaginary part [ R/S ] (the cycle starts again)


Repeat the cycle until the display is flashing zeros (forced 1/0 error). Hit [ CLx ] to stop the flashing.


This program switches the mode to radians angle measurement.



Code:

23: LBL

11: A

33 01: STO 1

33 08: STO 8

35: g

42: RAD

23: LBL

00: 0 (LBL 0 - subroutine)

34 08: RCL 8

84: R/S (show root #)

35: g

02: π

71: ×

02: 2

71: ×

34 01: RCL 1

81: ÷

01: 1

32: f^-1

01: R->P (->rect)

84: R/S (show real part)

35 07: x<>y

84: R/S (show imaginary part)

35: g

83: DSZ (decrement R8 by 1)

22: GTO

00: 0 (GOTO LBL 0)

34 08: RCL 8

35: g

04: 1/x

24: RTN


Example (FIX 4):

w^3 = 1

#; R/S; real part; R/S; imaginary part

3; 1.0000; -0.0000

2; -0.5000; -0.8660

1; -0.5000; 0.8660

(flashing zeroes)




Partial Fractions

(w is the variable, and T, Z, Y, and X are the stack contents)

(Tw+Z) ÷ ((w+Y)(w+X)) → B÷(w+Y) + A÷(w+X)

Input Stack: T, Z, Y, X 

Output Stack: B, A


Code: 

23: LBL 

11: A 

35, 07: x<>y 

51: - 

35, 00: LST x 

35, 09: R↑ 

71: × 

35, 00: LST x 

35, 07: x<>y 

35, 09: R↑ 

35, 07: x<>y 

51: - 

35, 09: R↑ 

81: ÷ 

41: ENTER 

35, 08: R↓ 

51: - 

35, 09: R↑ 

35, 07: x<>y 

24: RTN



Example 1: 

(12*w + 28) ÷ ((w+2) * (w+3)) = 4 ÷ (w+2) + 8 ÷ (w+3) 

Input Stack: T: 12, Z: 28, Y: 2, X: 3 press [ A ] 

Output Stack: Y (B): 4, X (A): 8



Example 2: 

(33) ÷ ((w-5) * (w+6)) = 3 ÷ (w-5) + -3 ÷ (w+6) 

Input Stack: T: 0, Z: 33, Y: -5, X: 6 press [ A ] 

Output Stack: Y (B): 3, X (A): -3



Example 3: 

(2*w - 3) ÷ ((w-1) * (w+4)) = -0.2 ÷ (w-1) + 0.2 ÷ (w+4) 

Input Stack: T: 2, Z: -3, Y: -1, X: 4 press [ A ] 

Output Stack: Y (B): -0.2, X (A): 0.2





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.

Sunday, February 15, 2026

Casio fx-3650P vs. Casio fx-3650P II: Programs


Here are four programs that are done with both the fx-3650P and fx-3650P II to illustrate some programming similarities and differences.


When we start a new program with the fx-3650P II, we are prompted to the choose the appropriate mode at the beginning:


COMP (main calculation with real numbers)

CMPLX (complex numbers, primarily complex number arithmetic)

SD (single-derivation mode, single-variable statistics)

REG (regression mode, two-variable statistics)

BASE (base conversions and Boolean logic)


Left:  Casio fx-3650P (2002), Right:  fx-3650P II (2013)


Program 1: Exponential Distribution


This program calculates:


PDF: e^(-X ÷ A)

CDF (lower tail): 1 - e^(-X ÷ A)


where:

A = 1/λ parameter (i.e. average waiting time, time for an event to occur/fail/succeed)

X = time parameter


fx-3650P, 25 bytes:


? → A : ? → X :

e(-X ÷ A) ÷ A ◢

1 – e(-X ÷ A)


fx-3650P II, 25 bytes:


? → A : ? → X :

e^(-X ÷ A) ÷ A ◢

1 – e^(-X ÷ A)


Example 1:

Inputs: A = 100, X = 50

Outputs: 6.065306597E-3 (*10^-3), 0.39346934


Example 2:

Inputs: A = 50, X = 3

Outputs: 0.01883529, 0.058235466


Program 2: Using Newton’s Method to get the real root of the cubic equation that is closest to zero:


x^3 + A * x^2 + B * x + C = 0


Using Newton’s Method:


x1 = x0 – f(x0) / f’(x0) [ let y = f(x0) / f’(x0) ]


x1 = x0 - (x^3 + A * x^2 + B * x + C) / (3 * x^2 + 2 * A * x + B)



fx-3650P, 72 bytes:


? → A : ? → B : ? → C : 0 → X :

Lbl 0 :

(X³ + AX² + BX + C) ÷ (3X² + 2AX + B) → Y :

√(Y²) → D :

X – Y → X :

D ≥ 1E-8 ⇒ Goto 0 : X


√(Y²) returns Abs(Y) in Comp mode.

D is set up to be the control variable, where

D = abs((x^3 + A * x^2 + B * x + C) / (3 * x^2 + 2 * A * x + B))



fx-3650PII, 67 bytes:


? → A : ? → B : ? → C : 0 → X : 1 → Y :

While Abs(Y) ≥ 1E-8 :

(X³ + AX² + BX + C) ÷ (3X² + 2AX + B) → Y :

X – Y → X :

WhileEnd: X


We could have used the derivative function (d/dx) but typing out the derivative took less room.


Example 1: X³ + 2X² – 5X + 4 = 0

Inputs: A = 2, B = -5, C = 4

Output: -3.663076827


Example 2: X³ – 3X² + 9X + 6 = 0

Inputs: A = -3, B = 9, C = 6

Output: -0.5481911635



Program 3: Sum of the polynomial: Σ((AX + B)^C, X = 1 to Y)


fx-3650P, 57 bytes:


0 → M : ? → A : ? → B : ? → C : ? → Y : 1 → X :

Lbl 0 :

(AX + B)^C M+ : X + 1 → X : X > Y ⇒ Goto 1 : Goto 0 :

Lbl 1 : M



fx-3650PII, 41 bytes:


0 → M : ? → A : ? → B : ? → C : ? → Y : 1 → X :

For 1 → X To Y : (AX + B)^(C) M+ : Next : M



Example 1: Σ((5X – 3)^2, X = 1 to 8)

Inputs: A = 5, B = -3, C = 2, Y = 8

Output: 4092



Example 2: Σ((2X + 1)^3, X = 1 to 10)

Inputs: A = 2, B = 1, C = 3, Y = 10

Output: 29160


Program 4: Complex Roots of Unity


x^n = 1


where:

x = cos(2 * m * π / n) + i * sin(2 * m * π / n) = r * e^(2 * m * i / n)

m = 0 to n – 1, i = j = √-1


fx-3650P, 47 bytes:


Switch to complex mode. The CMPLX indicator will be displayed but not shown as a step.


Rad : ? → A : 0 → B :

Lbl 0 :

cos(2 B π ÷ A) + i × sin(2 B π ÷ A) ◢

1 + B → B : A > B ⇒ Goto 0 : 1


We must have an else condition when the jump command is used, that is why the second one is there. Also, that one “indicates” the end.


fx-3650PII, 34 bytes:


Rad : ? → A :

For 0 → B To A :

cos(2 B π ÷ A) + i × sin(2 B π ÷ A) ◢

Next : 1


The second one “indicates” the end and can be omitted.


In complex mode, the real and imaginary parts are displayed on part at a time. A settle R←→I indicator displays at the right hand side of the screen. Switch between seeing the parts by pressing [ SHIFT ] [ EXE ].


Example 1: x^3 = 1

Input: A = 3

Outputs:

1 + 0 i

-0.5 + 0.866025403 i

-0.5 - 0.866025403 i


Example 2: x^5 = 1

Input: A = 5

Outputs:

1 + 0 i

0.309016994 + 0.951056516 i

-0.809016994 + 0.587785252 i

-0.809016994 - 0.587785252 i

0.309016994 - 0.951056516 i


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, May 10, 2025

RPN with HP 15C and DM32: Complex Mode

RPN with HP 15C and DM32: Complex Mode



HP 15C Complex Mode


This section also applies to the Swiss Micros DM15 family.


Complex mode is a separate mode for the HP 15C. Complex mode is turned on by setting flag 8. Complex mode is on when there is a “C” indicator in the display.


In complex mode, the stack is expanded. Each stack level now includes a real part and a complex part. What is in the display is always the real part.


Stack T

Real part of T

Imaginary part of T

Stack Z

Real part of Z

Imaginary part of Z

Stack Y

Real part of Y

Imaginary part of Y

Stack X

Real part of X

Imaginary part of X


The great news is that the stack handles up to four complex numbers, one for each of the stack levels.


To display the imaginary part, we have two methods:


Temporary View: Press [ f ] and hold [ COS ] (i) to temporarily view the complex part. The stack remains unaffected.


Switch: Switch the real part and the imaginary part by pressing [ f ] [ - ] (Re<>Im). Doing this switches the real and imaginary parts of the X stack.


For example: X = 9 – 8i


Enter the complex number as such: 9 [ ENTER ] 8 [ CHS ] [ I ] * (see note).

The displays shows 9.

Press [ f ] [ - ] (Re<>Im). The display shows -8 and the X stack now has the complex number X = -8 + 9i.

Press [ f ] [ - ] (Re<>Im) to switch the parts back to the original complex number.


* Note: If complex mode is turned off (no C indicator), using this key sequence sets flag 8, turning on complex mode automatically.


The switching of parts is important, because memory registers can not hold an entire complex number, but it’s separate parts. Thus, we will need two registers, one for the real part and one for the imaginary part.



HP 15C: Leaving Complex Mode


To leave complex mode, clear flag 8 (CF 8). The imaginary parts of the stack are lost.



Angle Mode


In complex mode, the trigonometric functions operate as the angles are always in radian measure regardless of the angle setting. The only functions that recognize the angle setting are the polar/rectangular conversions. To take the cosine and sine of angle that respects the conversion, use the following sequence:


angle [ENTER] 1 [ f ] [ →R ]: X stack: cos(angle), Y stack: sin(angle)



HP 15C - Solving Monic Quadratic Equations


The HP 15C’s solver only works for real numbers, so manual methods and formulas must be used to solve equations for complex numbers.



Z^2 + w1 * Z + w0 = 0


Solution: Z = (w1 ± √(w1^2 – 4 * w0)) / 2

(D = √(w1^2 – 4 * w0), Z+ = (w1 + D) / 2 Z- = Z+ - D)


Store the following:


Complex coefficient w1: Real part in register 4, Imaginary part in register 5

Complex coefficient w2: Real part in register 1, Imaginary part in register 2


The results are stored in the following registers:


Discriminant (D): Real part in register 6, imaginary part in register 3

Complex root Z+: Real part in register .0 (decimal point-0), imaginary party in register 8

Complex root Z-: Real part in register 9, imaginary part in register 7


Code (use any label you want, I use label A for example):


Key

Key Code


Key

Key Code

LBL A

42, 21, 11


RCL 4

45, 4

SF 8

43, 4, 8


RCL 1

45, 1

RCL 4

45, 4


I

42, 25

RCL 1

45, 1


-

30

I

42, 25


2

2

x^2

43, 11


÷

10

RCL 5

45, 5


STO .0

44, .0

RCL 2

45, 2


Re<>Im

42, 30

I

42, 25


STO 8

44, 8

4

4


Re<>Im

42, 30

×

20


R/S

31

-

30


RCL 6

45, 6

√

11


RCL 3

45, 3

STO 6

44, 6


I

42, 25

Re<>Im

42, 30


-

30

STO 3

44, 3


STO 9

44, 9

Re<>Im

42, 30


Re<>Im

42, 30




STO 7

44, 7




Re<>Im

42, 30




RTN

43, 32



Example:

Z^2 + (4 + i) * Z + (2 - 5i) = 0

w1: 4 STO 4, 1 STO 1

w2: 2 STO 5, 5 CHS STO 2


[ f ] A or [ GSB ] A:

0.11724 [ f ] hold (i) 1.15309 (Z+ ≈ 0.11724 + 1.15309i)

[ R/S ]

-4.11724 [ f ] hold (i) -2.15309 (Z- ≈ -4.11724 – 2.15309i)



DM32 Complex Mode


This section also applies to the HP 32Sii, HP 32S, and the HP 41C/DM41X with the Advantage ROM plugged in. The names of the functions vary.


There is no “separate” complex mode for the DM32, all the functions are access with shifted CMPLX prefix function. The complex number functions available on the DM32 are:


CMPLX+, CMPLX-, CMPLX×, CMPLX÷

CMPLX+/- (change sign, multiply the complex number by -1)

CMPLX1/x, CMPLXe^x, CMPLXLN, CMPLXy^x

CMPLXSIN, CMPLXCOS, CMPLXTAN


The complex functions grabs the values from the four stack levels and uses them as up to two complex numbers:

Z

Imaginary part of T + Zi

T

Real part of T + Zi

Y

Imaginary part of X + Yi

X

Real part of X + Yi


Memory registers can not hold an entire complex number, but it’s separate parts. Thus, we will need two registers, one for the real part and one for the imaginary part.


To enter complex numbers, enter the imaginary part, press [ ENTER ], then enter the real part.



Square Root and Square (√ and x^2)


There is no complex square root or complex square function. We will need some creativity to tackle these functions. Here is just one way we can accomplish this task.


Assume the complex number A + Bi have the real part stored in A and imaginary part stored in B.


Square Root (√):

RCL B

RCL A

0.5

ENTER

Clx

x<>y

CMPLXy^x


Square (x^2):

RCL B

RCL A

RCL B

RCL A

CMPLX×


Angle Mode


In complex mode, the trigonometric functions operate as the angles are always in radian measure regardless of the angle setting. The only functions that recognize the angle setting are the polar/rectangular conversions. To take the cosine and sine of angle that respects the conversion, use the following sequence:


1 [ENTER] angle [ blue shift ] [ →y,x ]: X stack: cos(angle), Y stack: sin(angle)


(HP 32SII late editions: lavender/purple shift)



DM32 - Solving Monic Quadratic Equations


Like the HP 15C, the DM32’s solver only works for real numbers, so manual methods and formulas must be used to solve equations for complex numbers.


Z^2 + w1 * Z + w0 = 0


Solution: Z = (w1 ± √(w1^2 – 4 * w0)) / 2

(D = √(w1^2 – 4 * w0), Z+ = (w1 + D) / 2 Z- = Z+ - D)


Store the following:


Complex coefficient w1: Real part in A, Imaginary part in B

Complex coefficient w2: Real part in C, Imaginary part in D

The program will prompt for A, B, C, and D, in the order of imaginary part, then real part

Z^2 + (Bi + A) * Z + (Di + C) = 0


The results are stored in the following registers:


Discriminant (D): Real part in E, imaginary part in F

Complex root Z+: Real part in R, imaginary party in S

Complex root Z-: Real part in U, imaginary part in V


Code:

A01 LBL A

A24 x<>y

A02 INPUT B

A25 RCL B

A03 INPUT A

A26 RCL A

A04 INPUT D

A27 CMPLX-

A05 INPUT C

A28 2

A06 RCL B

A29 ENTER

A07 RCL A

A30 CLx

A08 RCL B

A31 x<>y

A09 RCL A

A32 CMPLX÷

A10 CMPLX×

A33 STO S

A11 4

A34 x<>y

A12 RCL× D

A35 STO R

A13 4

A36 x<>y

A14 RCL× C

A37 STOP

A15 CMPLX-

A38 RCL F

A16 0.5

A39 RCL E

A17 ENTER

A40 CMPLX-

A18 CLx

A41 STO U

A19 x<>y

A42 x<>y

A20 CMPLXy^x

A43 STO V

A21 STO E

A44 x<>y

A22 x<>y

A45 RTN

A23 STO F




Example:

Z^2 + (4 + i) * Z + (2 - 5i) = 0

[ XEQ ] A

B? 1 [R/S]

A? 4 [R/S]

D? -5 (5 [+/-] ) [R/S]

C? 2 [R/S]


Y: 1.15309, X: 0.11724 (Z+ ≈ 0.11724 + 1.15309i)

[R/S]

Y: -2.15309, X: -4.11724 (Z- ≈ -4.11724 – 2.15309i)




I hope you enjoyed this edition of RPN with HP 15C and DM2,


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.

HP 42S/DM42/Free 42: Two Functions Programmed Three Different Ways

HP 42S/DM42/Free 42: Two Functions Programmed Three Different Ways Introduction In 1979, Electro Scientific Industries (esi)...