Sunday, April 10, 2022

Casio fx-CG 50 and fx-9750GIII: Regression Builder

Casio fx-CG 50 and fx-9750GIII:  Regression Builder


Introduction


The program LRBUILD lets the user fit bi-variate data (x,y) to a customized regression equation in the form:


y = C + Σ Cₙ * fₙ(x) 


where:


C = constant term.  This term is automatically calculated for by the program.


Cₙ = coefficient of each subsequent term of fₙ(x)


fₙ(x) = function, where the user can choose any of the following forms:


1.  x^A

2.  ln(A * x)

3.  e^(A * x)

4.  sin(A * x)


where A is a real number.  Angles are assumed to be in radian measure.  


For example, we can fit data by least squares method to this equation:


y = C + C_1 * x^2 + C_2 * e^(0.5 * x) + C_3 * sin(x)


You can add as many fₙ(x) terms as you like.


The program will give you the coefficients in a vertical matrix, starting from C and so forth, and the R^2 goodness of fit:


R^2 = 1 - SS_res / SS_tot


SS_res = Σ( y_observed - y_predicted)^2


SS_tot = Σ(y_observed - mean(y_oberserved))^2



Notes:


The characters _n (lower subscript n) and Σ (sigma symbol) are found on the CHAR (Character Select) on the programming editor menu (almost exit until the soft menu TOP, BOTTOM, SEARCH, ... displays).


The lines:


For 1 → I To 750

Next


are used to create a short pause.


There are two versions:  


Program files with the g3m extension is for the color Casio graphing calculators, such as the fx-CG50.   You can download this file here:

https://drive.google.com/file/d/1YidknrzjU_oRT3KLIzkEogbU_XFBCH0j/view?usp=sharing



Program files with the g1m extension is for the monochrome Casio graphing calculators, such as the fx-9750GIII (United States) and the fx-9860GIII (International).  You can download this file here:

https://drive.google.com/file/d/1GOO6VHGV_JRlNlbZB8ww3hK1qDqZaag_/view?usp=sharing



To transfer between Casio calculators, it is recommended to save them as text files.  Then transfer to the SAVE-F folder and load it on the home calculator.  Some editing may be necessary, such as <-1> with x^-1.


Casio fx-9750gIII and fx-CG50 (and probably most other Casio modern graphing calculators) Program: LRBUILD

(Extra spaces included for readability - this meant to be typed on the calculator)


Rad

ClrText

Locate 1,1,"LIN REG BUILDER"

Locate 1,2,"EWS 2022-01-17"

Locate 1,3,"Y=C+Σ(C_n×F_n(X))"   (n is the lower script n from the CHAR screen)

For 1 → I To 750

Next

"X DATA"? → List 1

"Y DATA"? → List 2

Dim List 1 → N

Dim List 1 → Dim List 3

{0} → List 4

{0} → List 5

Seq(1,X,1,N,1) → List 6

List→Mat(List 6) → Mat A

List→Mat(List 2) → Mat B

"CONSTANT TERM ADDED"

For 1 → I To 750

Next

Lbl 0

Menu "TERM?", "X^A", 1, "ln (AX)", 2, "e^(AX)", 3, "sin (AX)", 4, "CALCULATE", 5

Lbl 1

"A"? → A

Augment(List 4, {1}) → List 4

Got A

Lbl 2

"A"? → A

Augment(List 4, {2}) → List 4

ln(A×List 1) → List 6

Goto A

Lbl 3

"A"? → A

Augment(List 4, {3}) → List 4

e^(A×List 1) → List 6

Goto A

Lbl 4

"A"? → A

Augment(List 4, {4}) → List 4

sin(A×List 1) → List 6

Goto A

Lbl A

Augment(List 5, {A}) → List 5

List→Mat(List 6) → Mat D

Augment(Mat A, Mat D) → Mat A

Goto 0

Lbl 5

Dim List 4 → M

(Trn Mat A × Mat A)^-1 × Trn Mat A × Mat B → Mat C

"COEFFS:" ◢

Mat C ◢  

For 1 → I to N

0 → Y

For 1 → J To M

List 4[ J ] → L

List 5[ J ] → A

Mat C[ J, 1 ] → C

List 1[ I ] → X

L = 0 ⇒  Y + C → Y

L = 1 ⇒ Y + C × X^A → Y

L = 2 ⇒ Y + C × ln(AX) → Y

L = 3 ⇒ Y + C × e^(AX) → Y

L = 4 ⇒ Y + C × sin(AX) → Y

Next

Y = 0 ⇒ 1E-7    (lower case 10 on the fx-CG 50)

Y → List 3[ I ]

Next

1 - (Sum (List 2 - List 3)^2)÷(Sum (List 2 - Mean(List 2))^2) → R

"R^2="

R ◢

List→Mat(List 1, List 2, List 3) → Mat D



Variables:


List 1:  X Data

List 2:  Y Data

List 3:  Expected Y Data (Calculated)

List 4:  Operation Types:  0 - constant, 1 - x^a, 2 - ln(a*x), 3 - e^(a*x), 4 - sin(a*x)

List 5:  A


Mat A:  X data matrix 

Mat B:  Y data matrix

Mat C:  matrix of calculated coefficients



Examples


Example 1:

Fit the following data:


x data = {10, 25, 34, 46, 65}

y data = {39.7, 100, 135.9, 183.9, 260}


to the curve y = C + C1 * x + C2 / x


Remember the constant term is automatic.   

On the TERM? menu, select 

1.  X^A with A = 1

1.  X^A (again), this time with A = -1

5.  CALCULATE


Calculated coefficients:

[[ 0.1012904425 ]

[ 3.99874051 ]

[ -3.775351133 ]]


y = 0.012904425 + 3.99874051x - 3.775351133/x


R^2 = 0.9999994909


y' predicted data: 

{39.71116043, 99.91878916, 135.947281, 183.9612811, 259.9613413}


Example 2:

Fit the following data:


x data = {-0.35, -0.16, 0.27, 0.84}

y data = {-0.64, -0.31, 0.58, 2.06}


to the curve y = C + C1 * sin x  + C2 * e^x


Remember the constant term is automatic.   

On the TERM? menu, select 

4.  sin(A*X) with A = 1

3.  e^(A*X) with A = 1

5.  CALCULATE


Calculated coefficients:

[[ -0.9847711767 ]

[ 1.023495272 ]

[  0.9854914931 ]]


y = -0.9847711767 + 1.023495272 * sin x + 0.9854914931 * e^x


R^2 = 0.9999986129


y' predicted data:

{-0.6412613439, -0.3080521531, 0.5791860102, 2.060127487}


Example 3:

Fit the following data:


x data = {2, 3, 4, 5, 6}

y data = {22.5, 34, 45.5, 57.5, 69.75}


to the curve y = C + C1 * x^1.2 + C2 * ln(3*x)


Remember the constant term is automatic.   

On the TERM? menu, select 

1.  X^A with A = 1.2

2.  ln(A*X) with A = 3

5.  CALCULATE


Calculated coefficients:

[[ -0.08646217016 ] 

[ 6.83429695 ]

[ 3.855357541 ]]


y = -0.08646217016 + 6.83429695 * x^1.2 + 3.855357541 * ln(3* x)


R^2 = 0.9999924407


y' predicted data:

{22.52250254, 33.92570965, 45.56537698, 57.50145068, 69.73496014}


Eddie


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