# [SciPy-User] curve_fit and least squares

josef.pktd@gmai... josef.pktd@gmai...
Wed Oct 7 01:19:10 CDT 2009

```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)

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)

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
>
>
```