Saturday, September 30, 2023

HP 15C: Quadratic Regression

HP 15C:  Quadratic Regression




Setup, Normal Equations, and Registers Used



This program fits bivariate date (x,y) to the quadratic polynomial:


y = c + b * x + a * x^2


This program uses the matrix feature of the HP 15C.  




The normal equations that are solved are:


n * c + Σx * b + Σx^2 * a = Σy

Σx *c + Σx^2 * b + Σx^3 *a = Σxy

Σx^2 * c + Σx^3 * b + Σx^4 * a = Σ(x^2 y)


Matrix A = 

[ [ n, Σx, Σx^2 ]

[ Σx, Σx^2, Σx^3 ]

[ Σx^2, Σx^3, Σx^4 ] ]


Matrix B = 

[ [ Σy ]

[ Σxy ]

[ Σ(x^2 y) ] ]


Matrix C = (Matrix B)^-1 Matrix A

[ [ c ] 

[ b ] 

[ a ] ]


Registers Used:


R0:  x data point, row pointer

R1:  y data point, column pointer


Default Statistics Registers:

R2:  n

R3:  Σx

R4:  Σx^2

R5:  Σy

R6:  Σy^2

R7:  Σxy


Additional Statistics Registers:

R8:  Σx^3

R9:  Σx^4

R.0:  Σ(x^2 y)   ("register point zero":  press the decimal point before the 0)



Instructions


1.  Run label A to clear the matrices and registers.  This needs to be done in order to get the most accurate results.  Zero is displayed to indicate when the calculator is ready.  


2.  For each point, enter y data point, press [ ENTER ], x data point, and run label B.  The number of data points (n) will be displayed.  


3.  To calculate the coefficients, run label C.   The coefficients c, b, and a are displayed in order.



Matrix Operations Used


MATRIX 0:  clear all the matrices 


MATRIX 1:  sets the row and counter pointer to 1,1.   


User Mode Set:  automatically advances the pointer to the right row by row.   In programs, turning on and off User Mode is not a step.  However, storage and recall operations in User Mode are marked with a "u" after the step number.  Be careful because if a matrix's pointers (row in R0 and column in R1) return to (1,1), while in USER mode, the next program is skipped.  A special acknowledgement to Torsten for pointing this to me.  



HP 15C Program Code: Quadratic Regression 



Program Memory:  67 steps, 90 bytes

Needs 15 additional memory registers to store the three matrices.


Comments begin with a hash symbol.  #



#  Label A:   Initialization

001 :  42,21,11 :   LBL A

002 :  42,34 :  CLEAR REG

003 :  42,16, 0 :  MATRIX 0

004 :  0 :   0

005 :  43,32  : RTN


# Label B:  Data Entry and Processing

006 : 42,21,12 :  LBL B

007 : 44, 0 :   STO 0

008 :  43,11 : x^2

009 :  34  :  x<>y

010 :  44, 1  : STO 1

011 :  20  :  ×

012 :  44,40,.0 :  STO+ .0  (# store-add to register point zero)

013 :  45, 0  : RCL 0  

014 :  3 :  3

015 :  14 : y^x

016 :  44,40, 8 :  STO+ 8

017 :  45,20, 0 :  RCL× 0

018 :  44,40, 9 : STO+ 9

019 :  45, 1 : RCL 1

020 :  45, 0 : RCL 0

021 :  49  : Σ+

022 :  43,32 : RTN


# Label C:  Calculation

023 : 42,21,13 : LBL C

024 : 3 :  3

025 : 36 : ENTER

026 : 42,23,11 : DIM A

027 : 42,16, 1 : MATRIX A


# Matrix A - Row 1

# Turn on USER Mode ( [ f ] [ RCL ] (USER))

028 : 45, 2 : RCL 2

029 u 44,11 :  STO A

030 : 45, 3 : RCL 3

031 u 44,11 : STO A

032 :  45, 4 :  RCL 4

033 u 44,11 : STO A


# Matrix A - Row 2

034 : 45, 3 : RCL 3

035 u 44,11 : STO A

036 : 45, 4 :  RCL 4

037 u 44,11 : STO A

038 :  45, 8 : RCL 8

039 u 44,11 : STO A


# Matrix A - Row 3

040 : 45, 4 : RCL 4

041 u 44,11 : STO A

042 : 45, 8:  RCL 8

043 u 44,11 : STO A

044 : 45, 9 : RCL 9

045 u 44,11 : STO A

# Turn off USER Mode (unless the next step would be skipped unnecessarily)



# Matrix B

046 : 42,16, 1 : MATRIX 1

047 : 3 :  3

048 : 36 : ENTER

049 : 1 : 1

# Turn on USER Mode

050 : 42,23,12 : DIM B

051 : 45, 5 : RCL 5

052 u 44,12 : STO B

053 : 45, 7 : RCL 7

054 u 44,12 : STO B

055 : 45,.0 : RCL .0   (# recall registers point-zero)

# Turn off USER Mode (unless the next step would be skipped unnecessarily and in this case, an Error 11 would occur)

056 :  44,12 : STO B



# Matrix C - Results

057 : 42,26,13 : RESULT C

058 : 45,16,12 : RCL MATRIX B

059 : 45,16,11 : RCL MATRIX A

060 : 10 : ÷

061 : 42,16, 1 : MATRIX 1

062 u 45,13 : RCL C

063 : 31 : R/S

064 u 45,13 : RCL C

065 : 31 : R/S

066 u 45,13 : RCL C

067 : 43,32 : RTN


Examples


Example 1:

(3, 1.3)

(4, 1.6)

(5, 1.5)

(6, 1.4)


c = -0.54

b = 0.92

a = -0.1


y = -0.54 + 0.92 x - 0.1 x^2


Example 2:

(0, 99.856)

(3, 97.232)

(5, 93.481)

(7, 96.005)

(10, 102.008)


c ≈ 100.3437

b ≈ -2.5318

a ≈ 0.2495


y ≈ 100.3437 - 2.5318 x + 0.2495 x^2



Enjoy!


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. 


Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...