Showing posts with label exponential regression. Show all posts
Showing posts with label exponential regression. Show all posts

Sunday, January 14, 2018

Curve Fitting: Fitting to the Curve y = a*e^(-b*x^2)

Curve Fitting:  Fitting to the Curve y = a*e^(-b*x^2)

Introduction

This blog is to fit data to the equation y = a*e^(-b*x^2).  A famous example is the equation:

y = 2/√(π) * e^(-x^2)

which the indefinite integral of 0 to real number t is the basis of the error function. 

Turning y = a*e^(-b*x^2) into a Linear Form

y = a*e^(-b*x^2)

ln y = ln (a*e^(-b*x^2))

ln y = ln a + (-b)*x^2

Y’ = A’ + B’ * x^2

Where:  Y’ = ln y,  A’ = ln a, B’ = -b

Results:  e^A, B

Inputs:  square each of the data points of X, take the natural logarithm for Y.

Example

I use the HP Prime for this example. 

Fit the data below to the curve y = a*e^(-b*x^2).

x
y
-0.75
0.642931
-0.35
0.998284
0
1.128379
0.35
0.998284
0.62
0.768267
0.94
0.466951

Adjust data:

x
C1 = x^2
y
C2 = ln y
(to 6 decimal places)
-0.75
0.5625
0.642931
-0.441718
-0.35
0.1225
0.998284
-0.001717
0
0
1.128379
0.120782
0.35
0.1225
0.998284
-0.001717
0.62
0.3844
0.768267
-0.263618
0.94
0.8836
0.466951
-0.761531

Running the linear_regression(C1^2, LN(C2)) returns the matrix [B’, A’]:

[-0.998758, 0.120567]

The results must be adjusted: e^(.120567) = 1.128136

Final adjusted curve:  y ≈ 1.128136 * e^(-0.998758*x^2)

Using the Exponential Regression Model

We can use exponential regression function as an alternative to the linear regression. 

For the input, square all of the x data.

x
C1 = x^2
C2 = y
-0.75
0.5625
0.642931
-0.35
0.1225
0.998284
0
0
1.128379
0.35
0.1225
0.998284
0.62
0.3844
0.768267
0.94
0.8836
0.466951

The function exponential_regression from the HP Prime that fits data to the model y = B * A^x. 

To fit this model asked, y = a*e^(-b*x^2), we will need to take the natural logarithm of B. 

Hence:  exponential_regression(C1^2, C2) returns

[0.368337, 1.128136]

With ln(0.368337) ≈ -0.998757, the required equation is

y = 1.128136*e^(-0.998757*x^2)

Summary

To fit data to the curve y = a*e^(-b*x^2):

Linear Regression, Y’ = A’ + B’*X’
Exponential Regression, Y’ = B’*A’^X’
Input:  X’ = x^2, Y’ = ln y
Output:  a = e^A’, b = B’
Input: X’ = x^2, Y’ = y
Output: a = A’, b = ln(B’)

Eddie


This blog is property of Edward Shore, 2018.

Tuesday, July 18, 2017

Expanding Linear Regression

Expanding Linear Regression


Introduction – Linear Regression

All scientific calculators that have two-variable dimensions has curve fitting.  The most (and universal) curve fitting mode is linear regression.   Linear regression attempts to model bivariate data ((x,y)) to a line by the equation:

y = a + bx

Where a is the y-intercept (ITC) and b is the slope (SLP).  The correlation coefficient, r, is calculated by:

r = SXY / √(SX2 * SY2) where
SX2 = Σ(x – μx)^2
SY2 = Σ(y – μy)^2
SXY = Σ( (x – μx)*(y – μy) )
μx = mean of x values, μy = mean of y values

Source:  HP 17B II Financial Calculator Owner’s Manual, Corvallis, OR  1989.

The best fits are were |r| = 1, or as close to 1 as we can get.  If r is close to 0, the first is not good.  Note that the formula for correlation does not involve the slope or intercept.  We can use this to our advantage.

“Linearize” the Equation

If you can “linearize” equation, we can use the linear regression mode to fit a variety of curves.  The ultimate goal is of course:

y = a + bx

Now, most calculators use the form for linear equations.  However, some switch the a and b around and use the form, like the TI-30X IIS, 

y = ax + b

Hewlett Packard calculators has the b for the intercept and m for slope. 

For clarity, I will use the form Y = ITC + SLP*X. 

  
Calculator Comparison

To demonstrate how we can use the linear regression to fit a variety of curves, I use two calculators:



Sharp EL-W516T which offers 7 regressions.

TI-30X IIS which offers just linear regression.  I use the TI-30X IIS to use the linearized form.

[screen shot of the calcs] 

Sharp EL-W516T
TI-30X IIS
SLP
B
a
ITC
A
b
Linear Regression Equation
y = a + bx
y = ax + b

Screen shots are generated from a Casio fx-CG50. 


Logarithmic Regression:  y = a + b ln x



Translation to Linear:  This is pretty simple, we already have the form that we need, except we note that we have ln x instead of x.  Set the following variables as such:

X = ln x, Y = y, a = ITC, b = SLP

Example data set:
x
y
X = ln x  (8 decimal places)
1.0
2.00
0
1.3
3.00
0.2623643
1.7
4.15
0.5306283
2.3
5.34
0.8329091
2.8
6.10
1.0296194
3.5
7.00
1.252763

To use the linear regression mode, enter the data as such:  (ln x, y).  Everything else remains the same.

Logarithmic Regression Results:
ITC = a = 1.990614146
SLP = b = 4.003372076
r = 0.99905634

Comparing results between using the Logarithmic regression with the Sharp EL-W516T and the Linear regression with the TI-30X IIS, I get the same results.




Exponential Regression:  y  = a*e^(b*x)



Translation to linear: 

y = a*e^(b*x)
ln y = ln (a*e^(b*x))
ln y = ln a + ln(e^(b*x))
ln y = ln a + b*x

We got our form of Y = ITC + SLP*X where:
X = x, Y = ln y, a = e^ITC, b = SLP

When using the linear regression mode, enter data as (x, ln y).  When calculating a, note that

ITC = ln a
e^ITC = a

Example data set:
X
y
Y = ln y  (8 decimal places)
1.0
109.2
4.693181
1.3
363.0
5.8944028
1.7
1795.6
7.4930945
2.3
19794.3
9.8931493
2.8
146260.8
11.893147
3.5
2405208.5
14.693147

Results:
ITC = 0.693829923;  a = 2.001365951
SLP = b = 3.99773169
r = 0.999999992

Inverse Regression: y = a + b/x



Translation to Linear:  Like logarithmic regression, we pretty much have the equation pretty much set, with the exception of 1/x instead of x.  Hence:

X = 1/x, Y = y, a = ITC, b = SLP


 When using the linear regression mode, enter data as (1/x, y). Everything else remains.

Example data set:
X
y
X = 1/x  (8 decimal places)
1.0
6.00
1
1.3
5.55
0.7692308
1.7
5.16
0.5882353
2.3
4.86
0.4347826
2.8
4.73
0.3571429
3.5
4.58
0.2857143

Results:
a = ITC = 4.005529472
b = SLP = 1.993191341
r = 0.999733184

Power Regression:  y = a*x^b



Translation to Linear:  X = ln x, Y = ln y, A = e^a, b

y = a*x^b
ln y = ln (a*x^b)
ln y = ln a + ln(x^b)
ln y = ln a + b * ln x

So we have the following: X = ln x, Y = ln y, ITC = ln a, SLP = b.

In the linear regression mode, enter data as (ln x, ln y). Also, a = e^ITC. 

Example data set:
x
y
X = ln x
Y = ln y
1.0
3.95
0
1.3737156
1.3
6.76
0.2623643
1.9110229
1.7
11.56
0.5306283
2.4475509
2.3
21.09
0.8329091
3.048799
2.8
31.40
1.0296194
3.4468079
3.5
49.00
1.252763
3.8918203

Results:
ITC = 1.379195052; a = e^ITC = 3.971703326
SLP = b = 2.007158681
CORR = r = 0.999991481
   

Table of Linear Regression Equivalents to Curve Fitting


Regression

X

Y

ITC

SLP

Logarithmic: y = a + b * ln x

ln x

y

a

b

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

x

ln y

e^a

b

Inverse: y = a + b/x

1/x

y

a

b

Power: y = a*x^b

ln x

ln y

e^a

b

General Exponential: y = a * b^x

x

ln y

e^a

e^b

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

e^(-x)

1/y

a

b

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

x

y^2

a

b

Cosine: y = a + b*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

a

b


Eddie


This blog is property of Edward Shore, 2016

HP 67 Collection: October 2026

HP 67 Collection: Oct ober 2026 All examples are done in FIX 4. (DSP 4) Earth's Gravity at a Specific Latitude The true ea...