Curve fit an exponential decay function in Python using given data points

℡╲_俬逩灬. 提交于 2021-02-07 19:19:26

问题


With the curve_fit function in SciPy I'm able to determine the coefficients that represent the curve shown in the plot below.

def func2(t, tau):
    return np.exp(-t / tau)

t2 = np.linspace(0, 4, 50)
y2 = func2(t2, 1.2)

y2_noise = 0.2 * np.random.normal(size=t2.size)
y2_curve_noise = y2 + y2_noise

popt2, pcov2 = curve_fit(func2, t2, y2_curve_noise)

tau2, = popt2
y2_fit = func2(t2, tau2)

I would like to use a similar function to represent some data points. However, I'm unable to use this approach to fit the data points as shown below.

def func4(t, a, tau, c):
    return a * np.exp(-t / tau) + c

t4 = np.array([15445.1, 15445.6, 15446.1, 15446.6, 15447.1, 15447.6, 15448.1,
               15448.6, 15449.1, 15449.6, 15450.1, 15450.6, 15451.1, 15451.6,
               15452.1, 15452.6, 15453.1, 15453.6, 15454.1, 15454.6, 15455.1,
               15455.6, 15456.1, 15456.6, 15457.1, 15457.6, 15458.1, 15458.6,
               15459.1, 15459.6, 15460.1, 15460.6, 15461.1, 15461.6, 15462.1,
               15462.6, 15463.1, 15463.6, 15464.1, 15464.6, 15465.1, 15465.6,
               15466.1, 15466.6, 15467.1, 15467.6, 15468.1, 15468.6, 15469.1,
               15469.6, 15470.1, 15470.6, 15471.1, 15471.6, 15472.1, 15472.6,
               15473.1, 15473.6, 15474.1])

y4 = np.array([4.129, 4.125, 4.123, 4.121, 4.119, 4.118, 4.116, 4.115, 4.114,
               4.113, 4.112, 4.11, 4.11, 4.109, 4.108, 4.108, 4.106, 4.105,
               4.105, 4.104, 4.103, 4.102, 4.102, 4.101, 4.1, 4.1, 4.099,
               4.098, 4.098, 4.097, 4.097, 4.096, 4.095, 4.095, 4.094, 4.094,
               4.093, 4.092, 4.092, 4.091, 4.091, 4.09, 4.09, 4.089, 4.089,
               4.088, 4.088, 4.087, 4.087, 4.086, 4.086, 4.085, 4.085, 4.084,
               4.084, 4.084, 4.083, 4.083, 4.082])

popt4, pcov4 = curve_fit(func4, t4, y4, p0=(4.129, 1.2, 4.082))

a4, tau4, c4 = popt4
y4_fit = func4(t4, a4, tau4, c4)

How can I apply the curve_fit in SciPy to fit the data points? Or is there a different curve fitting method I should use? I'm also not sure what values to use for the initial guess p0. I just chose some numbers based on the data but obviously this didn't help with the fit.


回答1:


The problem is that exp(-15000) has to be balanced off by ridiculously large values of a, and the problem becomes really badly scaled, so the optimization routine fails.

Normalizing t so that they go from 0 to 1 helps with the scaling issue. The reasonable initial guesses then can be: 1 for tau, the smallest of y-values for c, and the difference of largest and smallest y-values for a.

t4_norm = (t4 - t4[0])/(t4[-1] - t4[0])   # normalized

c_0 = y4[-1]
tau_0 = 1
a_0 = (y4[0] - y4[-1])

popt4, pcov4 = curve_fit(func4, t4_norm, y4, p0=(a_0, tau_0, c_0))

a4, tau4, c4 = popt4
y4_fit = func4(t4_norm, a4, tau4, c4)
plt.plot(t4, y4, 'r.')
plt.plot(t4, y4_fit, 'b')
plt.show()

After the parameters are found, they can be recalculated in terms of the original t. Indeed, the curve obtained so far is

y = a*exp(- (t4 - t4[0])/(t4[-1] - t4[0]) / tau) + c

which can be rewritten as

y = a*exp(t4[0]/(t4[-1] - t4[0]) / tau) * exp(-t4/(t4[-1] - t4[0]) / tau) + c

This means the parameters in terms of original variable are

a_orig = a*exp(t4[0]/(t4[-1] - t4[0]) / tau)
tau_orig = (t4[-1] - t4[0]) * tau
c_orig = c



回答2:


To balance the fact that you're taking the exponent of a very large number, I've added a t0 term to your equation:

def func4(t, a, t0, tau, c):
    return a * np.exp(-(t-t0)/ tau) + c

# Initial guess
p0 = np.array([4.0, 15400., 6.e2, 0.], dtype=np.float64)
y4_initial = func4(t4, *p0)

# Fit
popt4, pcov4 = curve_fit(func4, t4, y4, p0=p0)
y4_fit = func4(t4, *popt4)

I'm getting

popt4 = [1.20102494e+00 1.53854910e+04 1.91852716e+01 4.07136089e+00]



来源:https://stackoverflow.com/questions/49565152/curve-fit-an-exponential-decay-function-in-python-using-given-data-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!