使用给定数据点曲线拟合Python中的指数衰减函数 [英] Curve fit an exponential decay function in Python using given data points

查看:137
本文介绍了使用给定数据点曲线拟合Python中的指数衰减函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SciPy中的 curve_fit 函数,我可以确定代表曲线的系数,如下图所示.

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)

如何在SciPy中应用 curve_fit 以适合数据点?还是我应该使用其他曲线拟合方法?我也不确定用于初始猜测 p0 的值.我只是根据数据选择了一些数字,但这显然不利于拟合.

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.

推荐答案

问题在于, exp(-15000)必须通过 a ,并且问题的扩展性变得很差,因此优化例程失败.

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.

归一化 t 以便它们从0变为1有助于解决缩放问题.合理的初始猜测可以是:tau为1,c的y值最小,a的最大y值和最小y值之差.

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()

找到参数后,可以根据原始t重新计算它们.实际上,到目前为止获得的曲线是

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

可以改写为

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

这篇关于使用给定数据点曲线拟合Python中的指数衰减函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆