# [SciPy-User] curve_fit and least squares

Bruce Southey bsouthey@gmail....
Wed Oct 7 08:40:04 CDT 2009

```On Wed, Oct 7, 2009 at 1:19 AM,  <josef.pktd@gmail.com> wrote:
> On Wed, Oct 7, 2009 at 1:36 AM, Kris Maynard <maynard@bu.edu> wrote:
>> I am having trouble with fitting data to an exponential curve. I have an x-y
>> data series that I would like to fit to an exponential using least squares
>> and have access to the covariance matrix of the result. I summarize my
>> problem in the following example:
>>
>> import numpy as np
>> import scipy as sp
>> from scipy.optimize.minpack import curve_fit
>>
>> A, B = 5, 0.5
>> x = np.linspace(0, 5, 10)
>> real_f = lambda x: A * np.exp(-1.0 * B * x)
>> y = real_f(x)
>> ynoisy = y + 0.01 * np.random.randn(len(x))
>>
>> exp_f = lambda x, a, b: a * np.exp(-1.0 * b * x)
>>
>> # this line raises the error:
>>
>>
>> #  actual and predicted relative reductions in the sum of squares
>>
>> #  are at most 0.000000 and the relative error between two
>>
>> #  consecutive iterates is at most 0.000000
>>
>> params, cov = curve_fit(exp_f, x, ynoisy)
>

As you would see, the curve is very poorly defined with those model
parameters and range. So you are asking a lot from your model and
data. At least you need a wider range with those parameters or Josef
says different parameter(s):

> this might be the same as  http://projects.scipy.org/scipy/ticket/984 and
> http://mail.scipy.org/pipermail/scipy-user/2009-August/022090.html
>
> If I increase your noise standard deviation from 0.1 to 0.2 then I do get
> correct estimation results in your example.
>
>>
>> I have tried to use the minpack.leastsq function directly with similar
>> results. I also tried taking the log and fitting to a line with no success.
>> The results are the same using scipy 0.7.1 as well as 0.8.0.dev5953. Am I
>> not using the curve_fit function correctly?
>
> With   minpack.leastsq   error code 2 should be just a warning. If you get
> incorrect parameter estimates with optimize.leastsq, besides the warning, could
> you post the example so I can have a look.
>
> It looks like if you take logs then you would have a problem that is linear in
> (transformed) parameters, where you could use linear least squares if you
> just want a fit without the standard errors of the original parameters
> (constant)

The errors will be multiplicative rather than additive.

Bruce

>
> I hope that helps.
>
> Josef
>
>
>> Thanks,
>> ~Kris
>> --
>> Heisenberg went for a drive and got stopped by a traffic cop. The cop asked,
>> "Do you know how fast you were going?" Heisenberg replied, "No, but I know
>> where I am."
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>

Hi,
```