Showing posts with label square. Show all posts
Showing posts with label square. Show all posts

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.

Saturday, March 8, 2025

RPN with HP 15C & DM32: Expanding Linear Regression

RPN with HP 15C & DM32: Expanding Linear Regression



Linearize” the Equation


The linear regression mode of the HP 15C and Swiss Micros DM32 can be expanded to fit different curves for bi-variate data.


Curves can be set up for linear regression if we can get the equation into the form:


y = b + m * x ⇒ f(y) = b + m * g(x)


where b is the y-intercept and x is the slope. The correlation is r and r^2 can be used to determine the relationship between x (independent) and y (dependent) variables. If r^2 is close to 1, the better the curve fits to the data.


Note: f(y) and g(x) are functions that contain only one term (sin y, cos y, e^y, 1/y, y^2 …. sin x, cos x, e^x, 1/x, x^2, ….)


The DM32 labels the slope as m, y-intercept as b. Each of the parameters can be recalled separately.


The HP 12C labels the slope as a, y-intercept as b. Both a and b are calculated by the key sequence [ f ] [ Σ+ ] (L.R.): slope is on the y-stack and the y-intercept is on the x-stack. To get the correlation, enter any (valid) number and press [ f ] [ . ] (y-hat, r) [ x<>y ].


Let’s illustrate this for with a couple of examples.


y = 1 / (b + m * e^(-x))

1/y = b + m * e^(-x)


We have the equation in the required form with the following adjustments: x’ = e^(-x), y’ = 1/x.


y = b * m^x

ln y = ln (b * m^x)

ln y = ln b + ln (m^x)

ln y = ln b + x * ln m


We have the equation in the required form with the following adjustments: x’ = x, y’ = ln y.

Now note that we have ln b and ln m. This will require an adjustment when the linear regression is calculated. To get the “true” slope and y-intercept in this case, we must calculate e^b and e^m.


To employ different curve fittings, I use (at least) two programs:


Entering data (DM32, HP15C):

LBL #

g(x)

x<>y

f(y)

x<>y

Σ+

R/S

GTO #


Entering data:

CLEAR Σ (CLΣ)

y1 ENTER x1 XEQ/GSB #

yn ENTER xn R/S


You can enter as many data points as you like.


Calculating the Parameters:

DM32:

LBL @

b

(adjustments if needed)

R/S

m

(adjustments if needed)

R/S

r

x^2

RTN


HP 15C:

LBL @

L.R.

(adjustment if needed)

R/S

x<>y

(adjustment if needed)

R/S

1

y-hat, r

x<>y

x^2

RTN



Example 1: y = 1 / (b + m * e^(-x))

Linearized: 1 / y = b + m * e^(-x)


Adjustments: x’ = e^(-x), y’ = 1 / y, no adjustment to b or m


Enter Data:

HP 15C Code

HP 15C Key

DM32

42, 21, 11

LBL A

LBL D

16

CHS

+/-

12

e^x

e^x

34

x<>y

x<>y

15

1/x

1/x

34

x<>y

x<>y

49

Σ+

Σ+

31

R/S

R/S

22, 11

GTO A

GTO D


Calculating the Parameters:

HP 15C Code

HP 15C Key

DM32

42, 21, 1

LBL 1

LBL R

42, 49

L.R.

b

31

R/S

R/S

34

x<>y

m

31

R/S

R/S

1

1

r

42, 48

y-hat, r

x^2

34

x<>y

RTN

43, 11

x^2


43, 32

RTN



Example:

x

y

0.0

0.125

0.1

0.13

0.2

0.134

0.3

0.138

0.4

0.143

0.5

0.147


Results (FIX 5):


HP 15C

DM32

Intercept (b)

4.98312

4.98312

Slope (m/a)

3.01575

3.01575

r^2

0.99841

0.99841



Example 2: y = ln(b + m * e^(-x))

Linearized: e^y = b + m * e^(-x)


Adjustments: x’ = e^(-x), y’ = e^(y), no adjustment to b or m


Enter Data:

HP 15C Code

HP 15C Key

DM32

42, 21, 12

LBL B

LBL E

16

CHS

+/-

12

e^x

e^x

34

x<>y

x<>y

12

e^x

e^x

34

x<>y

x<>y

49

Σ+

Σ+

31

R/S

R/S

22, 12

GTO B

GTO E


Calculating the Parameters:

HP 15C Code

HP 15C Key

DM32

42, 21, 1

LBL 1

LBL R

42, 49

L.R.

b

31

R/S

R/S

34

x<>y

m

31

R/S

R/S

1

1

r

42, 48

y-hat, r

x^2

34

x<>y

RTN

43, 11

x^2


43, 32

RTN



Example:

x

y

0.98

1.946

0.99

1.942

1.00

1.938

1.01

1.934

1.02

1.929

1.03

1.925


Results (FIX 5):


HP 15C

DM32

Intercept (b)

3.99987

3.99985

Slope (m/a)

8.00051

8.00056

r^2

0.99844

0.99844

*differences may be due to rounding error in the internal algorithms


Example 3: y = √(b + m * x^2)

Linearized: y^2 = b + m * x^2


Adjustments: x’ = x^2, y’ = y^2, no adjustment to b or m


Enter Data:

HP 15C Code

HP 15C Key

DM32

42, 21, 13

LBL C

LBL F

43, 11

x^2

x^2

34

x<>y

x<>y

43, 11

x^2

x^2

34

x<>y

x<>y

49

Σ+

Σ+

31

R/S

R/S

22, 13

GTO C

GTO F


Calculating the Parameters:

HP 15C Code

HP 15C Key

DM32

42, 21, 1

LBL 1

LBL R

42, 49

L.R.

b

31

R/S

R/S

34

x<>y

m

31

R/S

R/S

1

1

r

42, 48

y-hat, r

x^2

34

x<>y

RTN

43, 11

x^2


43, 32

RTN



Example:

x

y

1.05

2.066

1.25

2.337

1.45

2.620

1.65

2.912

1.85

3.209

2.05

3.511


Results (FIX 5):


HP 15C

DM32

Intercept (b)

1.40009

1.40009

Slope (m/a)

2.59996

2.59996

r^2

1.00000

1.00000



Example 4: y = b * m^x

Linearized: ln y = ln b + x * ln m


Adjustments: x’ = x, y’ = ln y, result adjustments: e^b, e^m


Enter Data:

HP 15C Code

HP 15C Key

DM32

42, 21, 14

LBL D

LBL G

34

x<>y

x<>y

43, 12

LN

LN

34

x<>y

x<>y

49

Σ+

Σ+

31

R/S

R/S

22, 14

GTO D

GTO G


Calculating the Parameters:

HP 15C Code

HP 15C Key

DM32

42, 21, 2

LBL 2

LBL S

42, 49

L.R.

b

12

e^x

e^x

31

R/S

R/S

34

x<>y

m

12

e^x

e^x

31

R/S

R/S

1

1

r

42, 48

y-bar, r

x^2

34

x<>y

RTN

43, 11

x^2


43, 32

RTN



Example:

x

y

0.84

2.358

0.87

2.363

0.90

2.369

0.93

2.374

0.96

2.379

0.99

2.385


Results (FIX 5):


HP 15C

DM32

Intercept (b)

2.21302

2.21302

Slope (m/a)

1.07843

1.07843

r^2

0.99916

0.99919

*differences may be due to rounding error in the internal algorithms


Expanding Linear Regression Table


Regression

X

Y

B = ITC

M = SLP

Logarithmic: y = b + m * ln x

ln x

y

b

m

Exponential: y = b * e^(m*x)

x

ln y

e^b

m

Inverse: y = b + m/x

1/x

y

b

m

Power: y = b * x^m

ln x

ln y

e^b

m

General Exponential: y = b * m^x

x

ln y

e^b

e^m

Simple Logistic: y = 1/(b + m * e^(-x))

e^(-x)

1/y

b

m

Square Root Linear: y = √(b + m * x)

x

y^2

b

m

Cosine: y = b + m*cos(ω(x – Ï•))

With

Ï• = the point (x) nearest to zero where the trough or peak begins

ω = (2*π)/period (radians) or

ω = 360°/period (degrees)

cos(ω(x – Ï•))

y

b

m

Logarithmic-Linear-Exponential:

y = ln(b + m * e^(-x))

e^(-x)

e^y

b

m

Square Root Quadratic:

y = √(b + m * x^2)

x^2

y^2

b

m



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.

Python – Earth’s Radius and Gravity in US Units

Python – Earth’s Radius and Gravity in US Units Introduction The following script, gravus2.py, estimates the Earth’s gravity i...