I was trying to fit some data to a single degree exponential decay function but a*exp(-x*t) and a*exp(-x/t) gives completely different answers with the latter not at all fitting the data well. The code:

def func(x, a1, t1, c):
    return a1 * numpy.exp(-x*t1) + c

interp_x = np.arange(low,high+dx,dx)
popt, pcov = scipy.optimize.curve_fit(func, x, y,maxfev=10000)
curve = func(interp_x,*popt)

This was the data used:

0.050   365.104
0.100   331.764
0.200   299.508
0.500   241.281
0.700   188.579
1.000   144.728
2.000   73.2627
  • 2
    $\begingroup$ What were the initial estimates used in each case? $\endgroup$
    – nicoguaro
    Jun 12 '21 at 19:06
  • $\begingroup$ @nicoguaro I dd not use any. I just gave the scipy.optimize.curve_fit() and the data as input and used values returned by it, I hope I am clear. $\endgroup$ Jun 13 '21 at 11:06
  • $\begingroup$ It may help if you put your full code in the question. $\endgroup$
    – Tyberius
    Jun 13 '21 at 20:12
  • 1
    $\begingroup$ Why do you expect similar fits from both models? $\endgroup$ Jun 14 '21 at 3:23
  • 2
    $\begingroup$ In that case, the difference is most likely do to the initial parameter that the fitting routine starts with. $\endgroup$ Jun 16 '21 at 19:03

Curve fitting can be very sensitive to your initial guess for each parameter. Because you don't specify a guess in your code, all of these parameters start with a value of 1. Comparing with the converged results for the t fitting, while t is actually pretty close to 1, the other parameters are much further away. Its mostly just luck that the t value didn't drift too far away while searching for appropriate values for a and c.

The 1/t fit was not so lucky and the search led it far away from the good solution obtained by the t fit. However a better initial guess can fix this. Using the a and c values from the t fit as an initial guess leads to the same solutions. I hadn't tried this, but I suspect even just providing a better guess for c (say 400) would probably be enough to make both fits match.

In general, you should always try to provide an initial guess for these parameters, especially for cases where there may be multiple distinct sets of parameters that minimize the error of the fit.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.