### Thread: Help needed in least square curve fit equation

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
20
Rep Power
0

#### Help needed in least square curve fit equation

Hi all,

I am trying to solve a least square non linear regression with the following data points:

X 1 2 3 4 5 6 7 8 9 10
Y 2950 2452 2333 2274 2244 2222 2207 2195 2184 2176

The equation which I want to fit is:

Y=a*exp[-X/b]+c

a, b and c are constants defined to fit the curve..

Also, as you notice, the Y value decreases with each X, but it will gradually converge and not decrease after a certain value of X.
So, I want to extrapolate the curve such that, at very high value of X (X = infinity), function Y converges to its asymptotic value..
In other words, with a certain high number of 'X', the "a*exp[-X/b]" term will tend toward 0, where "c" represents the convergence, and that value of "c" is being taken as final result..

Any type of help is appreciated.. Thanks for your consideration
2. The best fit I've found is

Ymodel = 2174+1081.61*Exp[-0.55*X]

with deviations Y-Ymodel =
151.965 _82.0367 _48.7231 _19.8458 0.854986 8.1068 9.98362 7.72071 2.33851 _2.42029
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
20
Rep Power
0
Could you please provide the code??

Originally Posted by b49P23TIvg
The best fit I've found is

Ymodel = 2174+1081.61*Exp[-0.55*X]

with deviations Y-Ymodel =
151.965 _82.0367 _48.7231 _19.8458 0.854986 8.1068 9.98362 7.72071 2.33851 _2.42029
4. I used j software, executable Iverson notation, Ken Iverson's final dialect of APL. First I computed a best fit exponential curve, and graphed Log[y] to the fit. As expected, the fit is terrible. You said the data needed to be shifted before the log fit would work.
Code:
```   X=: 1 2 3 4 5 6 7 8 9 10
Y=: 2950 2452 2333 2274 2244 2222 2207 2195 2184 2176

[POLYNOMIAL_COEFFICIENTS =: (^.Y) %. X^/0 1
7.87859 _0.0239607

NB. Least squares fit is
NB. Log[y] == 7.87859 - 0.0239607 * x

plot X ; (^. Y) ,: POLYNOMIAL_COEFFICIENTS p. X```
After that, I'd like to tell you I did something smart. Not true---I used the method of "guided guessing". We'll need a verb that subtracts the offset from Y, computes and uses best fit coefficients to the log of (Y-offset), then inverts that process and computes the difference. For an optimization I'd choose to minimize the sum of the squared differences.
Code:
```   NB. f is the verb.  In f, y is the offset.
f=: verb def 'Y - y + ^ ((^.Y-y) %. X^/0 1) p. X'"0

<./Y  NB. smallest value in Y
2176

f 2176  NB. Log[0] is undefined.
|NaN error: f
|   Y-y+^((^.Y-y)%.X^/0 1)    p.X

f 2177  NB. complex results---Log[negative_number]
146.42j308.069 _89.8616j107.133 _49.7507j23.6077 _15.6075j_6.43913 7.16772j_13.9134 14.1834j_13.0232 14.6719j_9.85077 10.6891j_6.70989 3.69979j_4.27979 _2.37358j_2.60322

f 2175  NB. offset of 2175 is pretty good.
82.9513 _105.183 _53.0604 _17.5579 4.63099 11.4523 12.3688 9.15869 3.01289 _2.30637

g  NB. g is sum of squares of result of f
mp@f

mp
\$:~ :(+/ .*)

(,. g) 2175 + _1 _0.1 0 0.1 0.2 0.3 0.4    NB. Of these choices, offset of 2175.3 is best
2174 32827.5
2174.9 22439.6
2175   21472
2175.1 20644.2
2175.2   20031
2175.3 19751.9
2175.4 20007.1```
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
3
Rep Power
0
Originally Posted by abhijit.bose
HI am trying to solve a least square non linear regression with the following data points:

X 1 2 3 4 5 6 7 8 9 10
Y 2950 2452 2333 2274 2244 2222 2207 2195 2184 2176

The equation which I want to fit is:

Y=a*exp[-X/b]+c
Using the online User Defined Function curve fitter at zunzun.com, I get these coefficients:

a = 1.9200189591314688E+03
b = 1.0487231973279976E+00
c = 2.2019458236838909E+03

The Python fitting code for the web site is at the Google Code Repository. It is BSD licensed and has many examples.

James
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
3
Rep Power
0
Originally Posted by Alex_Thompson
Any type of help is appreciated.
zunzun.com has a function finder for this purpose.

James
7. Originally Posted by zunzun.com
Using the online User Defined Function curve fitter at zunzun.com, I get these coefficients:

a = 1.9200189591314688E+03
b = 1.0487231973279976E+00
c = 2.2019458236838909E+03

The Python fitting code for the web site is at the Google Code Repository. It is BSD licensed and has many examples.

James
Good to know about the curve fitting code. It's probably a good tool but you need to know how to use it. Your sum of squared differences it 20 times mine. Did you plot the curves?

My
Ymodel = 2174+1081.61*Exp[-0.55*X]
goes through the data---some points above, some below.

a = 1.9200189591314688E+03
b = 1.0487231973279976E+00
c = 2.2019458236838909E+03
has good shape but is offset way above the data.

The difficulty with this fit, which I identified, is that the logarithm of values with offset c near the solution are complex.

Originally Posted by abhijit.bose
and that value of "c" is being taken as final result..
In other words, you failed to find the critical information.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
3
Rep Power
0
Originally Posted by b49P23TIvg
you failed to find the critical information.
"you failed" - well, now I know that I failed.

James