How to fix "overflow encounter in exp" when curve fitting data in Scipy?

I'm using Python 3 and I'm trying to find the best fit of the following data set with the exponential function

xdata = [329.14, 339.43, 344.13, 347.02, 350.79, 353.54, 355.62, 360.51, 362.36, 364.89,
 366.66, 369.0,371.87, 372.91]
ydata = [13.03, 20.53, 25.08, 28.38, 33.18, 36.93, 40.13, 48.23, 51.98, 57.23, 60.98, 66.43,
 73.23, 76.28]

And then I execute the code below:

opt.curve_fit(lambda t, a, b: a*np.exp(b/t), xdata, ydata, p0=[P0, p[0]])

where P0, p[0] = 76.28, -4957.925919691658. But I receive the following error

<ipython-input-67-64582d269012>:3: RuntimeWarning: overflow encountered in exp
  opt.curve_fit(lambda t, a, b: a*np.exp(b/t), xdata, ydata, p0=[76.3, p[0]])

I'm pretty sure this problem has to do with p0 in particular P0 since if I remove it I obtain

(array([ 4.33524091e+07, -4.94111729e+03]),
 array([[ 1.93745891e+12, -1.62915424e+07],
        [-1.62915424e+07,  1.37067431e+02]]))

But I don't really satisfy this since I am expecting an exponential fitting curve that can provide a around P0.

I wonder how can I apply an exponential fitting to the data above so that a can be around P0. I can accept whatever method on python even though it is not using opt.curve_fit.

Thanks.


Solution 1:

The problem is the small value for a. The minimization process tries to compensate via b resulting in an overflow. I get good results with starting values p0=( 3.2e6, -4000 ) Alternatively, you can define the function to be exp( a - b / t ) which the coverges well with p0=( 15, -4000 ) or even without providing a p0