Exponential Curve Fitting With Python
I am trying to convert some Matlab code I have for curve fitting my data into python code but am having trouble getting similar answers. The data is: x = array([ 0. , 12.5 ,
Solution 1:
curve_fit(fitFunc, y, x], p0=init_vals)
should be curve_fit(fitFunc, x,y, p0=init_vals)
ie, x goes before y . fitFunc(A, B, k, t)
should be fitFunc(t,A, B, k)
. The independent variable goes first. See the code below:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
x = np.array([ 0. , 12.5 , 24.5 , 37.75, 54. , 70.25, 87.5 ,
108.5 , 129.5 , 150.5 , 171.5 , 193.75, 233.75, 273.75])
y = np.array([-8.79182857, -5.56347794, -5.45683824, -4.30737662, -1.4394612 ,
-1.58047016, -0.93225927, -0.6719836 , -0.45977157, -0.37622436,
-0.56115757, -0.3038559 , -0.26594558, -0.26496367])
def fitFunc(t, A, B, k):
return A - B*np.exp(-k*t)
init_vals = np.random.rand(1,3)
fitParams, fitCovariances = curve_fit(fitFunc, x, y, p0=init_vals)
print fitParams
plt.plot(x,y)
plt.plot(x,fitFunc(x,*fitParams))
plt.show()
Post a Comment for "Exponential Curve Fitting With Python"