Post Reply 
(15C) Quadratic Regression
07-24-2023, 03:47 AM (This post was last modified: 07-29-2023 05:49 PM by Eddie W. Shore.)
Post: #1
(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.

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. #

Code:
#  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,22,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

# Matrix B
046 : 42,16, 1 : MATRIX 1
047 : 3 :  3
048 : 36 : ENTER
049 : 1 : 1
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)
056 u 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

(Typos pointed to me by Torsten. The key codes are now correct.)

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
Visit this user's website Find all posts by this user
Quote this message in a reply
07-25-2023, 11:04 PM
Post: #2
RE: (15C) Quadratic Regression
Cool use of matrices for quadratic regression!!

Namir
Find all posts by this user
Quote this message in a reply
07-26-2023, 04:59 AM
Post: #3
RE: (15C) Quadratic Regression
Thank you, Namir!

Eddie
Visit this user's website Find all posts by this user
Quote this message in a reply
07-29-2023, 05:47 PM
Post: #4
RE: (15C) Quadratic Regression
One thing I noticed after running the program is that when I recalled Matrix A, the display shows a couple of dashes after A: A - - 3 3. I was not able to find anything in the manual, do you know if the dashes are significant?
Visit this user's website Find all posts by this user
Quote this message in a reply
07-29-2023, 06:18 PM
Post: #5
RE: (15C) Quadratic Regression
From the Advanced Function Handbook (pp. 96):

Understanding the LU Decomposition

Quote:The decomposition is flagged in the process, and its descriptor includes two dashes when displayed.
Find all posts by this user
Quote this message in a reply
08-01-2023, 10:04 AM
Post: #6
RE: (15C) Quadratic Regression
Here is another program that solves the following equation for \(\mathbf{c}\):

\(
\mathbf{A}^\top \mathbf{A} \mathbf{c} = \mathbf{A}^\top \mathbf{b}
\)

It stores the data in the matrices \(\mathbf{A}\) and \(\mathbf{b}\).
This can be useful if you only want to change the y-values so you don't have to re-enter the x-values.

However, the disadvantage is that due to the limited memory, a maximum of 11 data points can be entered.

Code:
   001 {    42 21 11 } f LBL A
   002 {    42 16  1 } f MATRIX 1
   003 {    42 21  0 } f LBL 0
   004 {       45  0 } RCL 0
   005 {          31 } R/S
   006 {           1 } 1
   007 {    44 11  u } STO A
   008 {          34 } x↔y
   009 {    44 11  u } STO A
   010 {       43 11 } g x²
   011 {    44 11  u } STO A
   012 {       22  0 } GTO 0
   013 {       43 32 } g RTN
   014 {    42 21 12 } f LBL B
   015 {    42 16  1 } f MATRIX 1
   016 {    42 21  1 } f LBL 1
   017 {       45  0 } RCL 0
   018 {          31 } R/S
   019 {    44 12  u } STO B
   020 {       22  1 } GTO 1
   021 {       43 32 } g RTN
   022 {    42 21 13 } f LBL C
   023 {    42 26 13 } f RESULT C
   024 {    45 16 11 } RCL MATRIX A
   025 {    45 16 12 } RCL MATRIX B
   026 {    42 16  5 } f MATRIX 5
   027 {    42 26 14 } f RESULT D
   028 {    45 16 11 } RCL MATRIX A
   029 {          36 } ENTER
   030 {    42 16  5 } f MATRIX 5
   031 {    42 26 13 } f RESULT C
   032 {          10 } ÷
   033 {    42 16  1 } f MATRIX 1
   034 {       43 32 } g RTN

Important: Make sure to have USER mode active while entering the program.

Example

(0, 99.856)
(3, 97.232)
(5, 93.481)
(7, 96.005)
(10, 102.008)

Initialisation:

USER
0 DIM (i)
MATRIX 0
5 ENTER 3 DIM A
5 ENTER 1 DIM B


Enter the x-values:

A
1.0000
0 R/S

2.0000
3 R/S

3.0000
5 R/S

4.0000
7 R/S

5.0000
10 R/S


Enter the y-values:

B
1.0000
99.856 R/S

2.0000
97.232 R/S

3.0000
93.481 R/S

4.0000
96.005 R/S

5.0000
102.008 R/S


Calculate the coefficients:

C
running

RCL C
100.3437

RCL C
-2.5318

RCL C
0.2495
Find all posts by this user
Quote this message in a reply
08-03-2023, 01:53 PM
Post: #7
RE: (15C) Quadratic Regression
Revised code: with comments on user mode.

Code:
#  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
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)