Update: solved! It is producing parameters with the correct signs now, and they do fit the curve. The problem was defining func(a,b,c,x) but curve_fit needs to read x first: func(x,a,b,c). Thanks everyone for all the help! I'll have quantitative analysis when I meet with my boss today :)
Here's some of the new fits: http://imgur.com/NHnzR2A
(I still get a run-time error:
RuntimeWarning: overflow encountered in power
return a*(math.e**(b*(math.e**(c*x))))
)
Can anyone help me figure out what's wrong with this code? I am new to scipy. I am trying to model bacterial growth with the Gompertz equation, but my code produces a curve_fit that's completely wrong. You can see images of my real data, the model equation, and the fit this code produces in this imgur album Thanks!
Fixed code:
#!/usr/bin/python
from numpy import *
from scipy.optimize import curve_fit
values = numpy.asarray(values)
y = values[:2000//5].astype(numpy.float)
y - y[0] #subtracting blank value
x = numpy.arange(len(y))*5
def Function(x,a,b,c):
#a = upper asymptote
#b = negative = x axis displacement
#c = negative = growth rate
return a*(math.e**(b*(math.e**(c*x))))
parameters, pcov = curve_fit(Function, x, y, p0=[0.1,-1300,-0.0077])
#Graph data and fit to compare
yaj = Function( numpy.asarray(x), parameters[0], parameters[1], parameters[2] )
figure(1, figsize=(8.5,11))
subplot(211)
plot(x,y,'g-')
xlim(min(x),max(x))
ylim(min(y),max(y))
subplot(212)
plot(x,yaj,'r-')
xlim(min(x),max(x))
ylim(min(yaj),max(yaj))
savefig('tempgraph.pdf')
return parameters