I have a set of data for which I am trying to fit a biexponential function. So far, I have done
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import csv
from pylab import genfromtxt
import scipy
twofifty = genfromtxt("t vs delta E (250C).txt");
t = twofifty[:, 0]
de = twofifty[:, 1]*10e3
def func(t, f_1, tau_1, f_2, tau_2):
return f_1*np.exp(-t/tau_1)+f_2*np.exp(-t/tau_2)
guess = (0.7, 100, 0.3, 200)
popt, pcov = curve_fit(func, t, de, guess)
print(popt)
plt.plot(t, de, label="data")
plt.plot(t, func(de, *popt), label="fit")
plt.legend()
plt.show()
But the graph comes out as shown below. I’m not sure what I need to change to get the correct results. I have changed the scale of the y axis since it was in the order of 10^4 and included some guess values which allowed me to get the following results,
[-1.46219808e+02 -4.99772483e+05 2.42937404e+01 -1.94190603e+01]
, but I know they are not the correct values as the fit is not right and they are negative values. As you can tell, from the biexponential equation I am trying to extract the amplitudes (f_1 and f_2) and decay time (tau_1 and tau_2). I know for the amplitudes the sum has to be 1, hence my predictions of 0.7 and 0.3, but as I don’t really know what tau will be, only that they will be of the order of 10^-1 or 10^-2. How can I smooth out the fit line, because as it’s evident, it doesn’t actually fit the data? What can I change to make it fit the data?