-1

enter image description here Hi all, I am trying to fit a curve of a double exponential decay function in Python. The code I have used is as follows. I am not sure why I am getting a straight line for the fit. I welcome any help and suggestions to my code, it would be of great help.

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]
Angela Mary
  • 49
  • 1
  • 8
  • It is not possible to reproduce what you did because the data joint to the question is not consistent and incomplete. So one cannot look for the cause of trouble. – JJacquelin Aug 01 '21 at 09:20
  • Could you please explain? I'm unable to follow @JJacquelin – Angela Mary Aug 01 '21 at 15:52
  • They are 9 numbers in your xData. They are 23 numbers in your yData. So it is not consistant. They are much more points on your graph than in the Data. Without your whole Data one cannot reproduce exactly your calculus. One cannot observe the straight line that you get. So one cannot find the cause of the trouble. – JJacquelin Aug 01 '21 at 16:22
  • @JJacquelin no actually they are of same size..I just wanted to show an example of what my list data look like – Angela Mary Aug 01 '21 at 21:49
  • Can you understand that showing a graph and a partial data is not enough. If you don't give a full numerical example I will not answer to the question. – JJacquelin Aug 02 '21 at 03:55
  • A few observations. 1) Always try to scale your data to 1. Here results can be scaled back easily later. 2) The data around (0, 0) does not go well with your model (start-up artifact in the measurement?) and probably should be removed. 3) The manual shift ( x - 0.1) is way outside the data range as this is of order 1e-7. – mikuszefski Aug 02 '21 at 05:59

1 Answers1

3

I apologize to all reading this for answering here a little outside the norms of StackOverflow. This is more like a long comment than an answer, related to the fact that the poster has asked many similar questions about using the lmfit library over the past few months.

@Angela Mary 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.

The emphasis on StackOverflow is on coding. Questions should emphasize how to use the library, not the results it gives. This means that you are not likely to get very meaningful answers about why a model does not match particular data. Pose questions about how to do things.

Since the forum is about coding, ALWAYS include a minimal, complete example that shows the problem. If the phrase "minimal, complete example" is foreign to you, consider that your first homework assignment. The code posted here needs to be complete and runnable by someone else. Output reports need to be included. If you get an exception running code, post the full traceback. Do not edit the results: post them verbatim. These are not suggestions or "if you feel it is appropriate". These are requirements.

If you are fitting data (or doing any other calculation with data), then include that data, either in the code itself or as a readily available data file. Since your example has already been made into a minimal, complete example, the data will not need to be very big.

M Newville
  • 7,486
  • 2
  • 16
  • 29