[SciPy-User] Trouble using curve_fit

klo uo klonuo@gmail....
Mon Jan 9 12:56:50 CST 2012


Example provided at the bottom of this page:
scipy.optimize.curve_fit<http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html#scipy.optimize.curve_fit>seemed
perfect for what I hoped I need, but result was bad. I don't know if
I misuse it or I don't have enough data, but as can be seen from attached
screen-shot, I did not expect problem for finding obvious exponential
function fit.

Here is snippet:
========================================
import numpy as np
from scipy.optimize import curve_fit

def func(x, a, b, c):
    return a*np.exp(-b*x)+c

x = np.array([ 0.8, 11., 12., 16., 32., 37.8, 44., 48., 56., 64., 88., 96.,
112., 128., 144., 176., 192. ])
y = np.array([0.94597685600279, 0.95856916599601, 0.96009142950541,
0.96454515552826, 0.97938932735214, 0.98252400815195, 0.98500175787242,
0.98621192462708, 0.98816995007392, 0.98964101933472, 0.99247255046129,
0.99308203517541, 0.99406737810867, 0.99480702681278, 0.99538268958706,
0.99622916581118, 0.99653501465135])

popt, pcov = curve_fit(func, x, y)
popt

Out[]: array([ 164.57396829  206.83406927    0.98251156])
========================================

I then found this page:
wolfram<http://mathworld.wolfram.com/LeastSquaresFittingExponential.html>and
quickly

========================================
def lsq_exp(x, y):
#   return A and B in y=A*exp(B*x)

    from numpy import exp, log, sum

    n = len(x)
    a_num = (log(y)).sum() * (x**2).sum() - x.sum() * (x*log(y)).sum()
    b_num = n*(x*log(y)).sum() - x.sum() * (log(y)).sum()
    den = n*(x**2).sum() - (x.sum())**2

    A = exp(a_num/den)
    b = b_num/den

    return A,b
========================================
result coefficients: (0.96677003283750318, 0.00021665429178716782)

What is wrong with this approach?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120109/de1377e7/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 25256 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20120109/de1377e7/attachment-0001.png 


More information about the SciPy-User mailing list