pythoncurve-fittingexponentiallmfit

Why am I getting a straight line for exponential decay curve best-fit?


enter image description here

I am trying to fit a curve of a double exponential decay function in Python. The code I have used is as follows. Why am I getting a straight line for the fit?

xData = np.array(xData) 
yData = np.array(yData)
def double_exp(x, a1, t1, a2, t2):
    return a1*np.exp(-x/t1) + a2*np.exp(-(x-0.1) / t2)
mod=Model(double_exp)

result=mod.fit(yData,x=xData,a1=0.7,t1=0.5,a2=0.3,t2=10)
result.plot()
plt.grid()
plt.xlabel('Time')
plt.ylabel('Fluoresence Lifetime')

My sample data values for the lists xData and yData is

[![xData is \[2.50783699e-09 5.01567398e-09 7.52351097e-09 1.00313480e-08
 1.25391849e-08 1.50470219e-08 1.75548589e-08 2.00626959e-08
 2.25705329e-08... etc\]
yData is \[ 1025.  1032.  1101.  1138.  1205.  1086.  1692. 13515. 59434. 82067.64293. 52266. 43993. 35335. 30066. 29989. 33773. 30521. 26468. 26338.27233. 26276. 24109....etc\]][1]][1]

Solution

  • You're probably getting a straight line because your initial values are not close enough to be refined.

    When using lmfit, you absolutely must provide realistic initial values for all parameters to be optimized. There are no circumstances under which this can be neglected. If you are remotely unsure if your initial values are reasonable, plot the data and the model with the initial parameter values before trying a fit. When doing a fit, you are asserting that you know the initial values are reasonable values for approximating your data.

    A corollary: it is not a mistake of the algorithms or library that it fails to find a good solution when you give it unreasonable initial values. This should be expected.

    After a fit, always print out and read the fit report. This contains the principle result. A plot of the resulting fit can be useful -- a picture is worth a thousand words, as they say. But the fit results contain the table of the fit results, and a table of numbers is worth a thousand pictures. Let me repeat: ALWAYS PRINT OUT AND ACTUALLY READ THE FIT REPORT. Do not post questions about the results of lmfit where this report is not included.