如何通过指数曲线拟合数据

How to fit data by exponential curve

我的项目有点问题,因为我有一组数据,我绘制它以获得 2 条曲线,我想用指数曲线拟合这些图。

我看了这个 post : fitting exponential decay with no initial guessing。 但是我的例子有点不同。

这是我得到的数据:

我的脚本如下:

mask_G = np.bitwise_and( tbdata['G'] < 99.99, tbdata['GERR'] < 0.2)
mask_R = np.bitwise_and( tbdata['R'] < 99.99, tbdata['RERR'] < 0.2)

G_corrected = tbdata[mask_G]
R_corrected = tbdata[mask_R]


fig13 = plt.gcf()
fig13.set_size_inches(16, 9)


fig13, (ax1,ax2) = plt.subplots(1,2)

fig_error_g = ax1.plot(G_corrected['G'], G_corrected['GERR'], '.')
ax1.set_xlabel('G')
ax1.set_ylabel('GERR')
ax1.set_title('Evolution de GERR en fonction de G')

fig_error_r = ax2.plot(R_corrected['R'], R_corrected['RERR'], '.')
ax2.set_xlabel('R')
ax2.set_ylabel('RERR')
ax2.set_title('Evolution de RERR en fonction de R')

fig13.tight_layout() 

plt.savefig('graphique.png')

plt.show()

我试着写那个,它基于 scipy 文档 :

def exponential(x,a,b,c) :
    return a * np.exp(-b * x) + c

xdata = G_corrected['G']
y = G_corrected['GERR']
ydata = y + 0.2 * np.random.normal(size=len(xdata))

popt, pcov = curve_fit(exponential, xdata, ydata)

但我得到:

/home/user/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.py:601: OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)

你知道我该如何处理吗?

非常感谢 ;)

编辑:

我试着像那样适应我的情节:

mask_G = np.bitwise_and( tbdata['G'] < 99.99, tbdata['GERR'] < 0.2)
mask_R = np.bitwise_and( tbdata['R'] < 99.99, tbdata['RERR'] < 0.2)

G_corrected = tbdata[mask_G]
R_corrected = tbdata[mask_R]


params = np.polyfit(G_corrected['G'], np.log(G_corrected['GERR']),1)
a = params[0]
A = np.exp(params[1])


fig13 = plt.gcf()
fig13.set_size_inches(16, 9)


fig13, (ax1,ax2) = plt.subplots(1,2)

fig_error_g = ax1.plot(G_corrected['G'], (G_corrected['GERR']), '.')
fig_error_g = ax1.plot(G_corrected['G'], (A*np.exp(a*G_corrected['G'])),'.')

ax1.set_xlabel('G')
ax1.set_ylabel('GERR')
ax1.set_title('Evolution de GERR en fonction de G')

fig_error_r = ax2.plot(R_corrected['R'], np.log(R_corrected['RERR']), '.')
ax2.set_xlabel('R')
ax2.set_ylabel('RERR')
ax2.set_title('Evolution de RERR en fonction de R')

fig13.tight_layout() 

plt.savefig('graphique.png')

plt.show()

我得到:

你觉得这个结果怎么样?

最简单的方法是对图应用对数缩放。正如您肯定知道的 log(exp(x)) = x,即如果您将 log() 应用于您的 y 值并绘制您应该得到的线性图。一旦你拥有它,你就可以将它与你的线性工具箱 (Gaussian Least Square method) 相匹配。得到的斜率是 exp(ax) 中的预因子,您尝试获得它。

如果您在 x 轴上有其他依赖关系,则制作数据的双对数图以找出所有依赖关系可能会有所帮助。