微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在对数轴中拟合指数函数的稳健方法

如何解决在对数轴中拟合指数函数的稳健方法

我有以下脚本来拟合 loglog 轴中的指数函数

start_exp = 21  # Start of fit
end_exp   = 40  # end of fit

# fix the parameters of  exponential Y = exponent1 *a*exp(exponent2 *b*t)
exponent1    = 3.      
exponent2    = 2.    


### The original data to perform the fit ###

bin_centers = np.array([9.31939514e-01,1.71773809e+00,2.58227857e+00,3.16611120e+00,3.88194374e+00,4.75962032e+00,5.83573259e+00,7.15514527e+00,8.77286667e+00,1.07563420e+01,1.31882653e+01,1.61700271e+01,1.98259415e+01,2.43084291e+01,2.98043714e+01,3.65429025e+01,4.48049619e+01,5.49350072e+01,6.73553750e+01,8.25838892e+01,1.01255449e+02,1.24148500e+02,1.52217488e+02,1.86632650e+02,2.28828807e+02,2.80565179e+02,3.43998732e+02,4.21774108e+02,5.17133876e+02,6.34053729e+02,7.77408231e+02,9.53174043e+02,1.16867911e+03])

norm_counts = np.array([7.47683145e-02,2.44802278e-02,6.40790461e-02,4.24335863e-02,2.91339699e-02,2.34427016e-02,1.63884469e-02,2.19590994e-02,1.42326906e-02,1.64066907e-02,1.03597068e-02,1.03504798e-02,9.22668847e-03,6.22943476e-03,6.37955519e-03,5.30701726e-03,3.61688252e-03,3.48879003e-03,2.46230484e-03,1.91634352e-03,1.45802119e-03,9.72116428e-04,8.17790259e-04,4.57538385e-04,3.66533958e-04,2.17783386e-04,1.36803586e-04,7.28849304e-05,4.84365929e-05,2.93293649e-05,2.24564621e-05,1.19448771e-05,4.87111765e-06])

def expo(x,a,b) :
    return a*np.exp(-b*x)

x_exp = bin_centers[start_exp:end_exp]
y_exp = norm_counts[start_exp:end_exp]

a_exp,b_exp = sc.optimize.curve_fit(lambda t,b: exponent1*a*np.exp(-b*t*(exponent2)),x_exp,y_exp,p0=(0.0005,0.0058))

expon       = (expo(bin_centers[start_exp:end_exp],a_exp[0],a_exp[1]))

plt.plot(bin_centers[start_exp:end_exp],expon,'-.',color='black',linewidth='0.8',label='${\propto} $ $e^{%.4f}$' %(-a_exp[1] ))#,'r-',label='fit: a=%5.3f,b=%5.3f,c=%5.3f' % tuple(popt))
plt.legend()

plt.plot(bin_centers,norm_counts)
plt.yscale('log')
plt.xscale('log')

我得到的结果非常好。

enter image description here

但是,我每次都必须非常努力地找到合适的参数。而且我必须对许多曲线进行拟合。

有没有办法让这个过程自动化?一种更强大的算法,可以让我估计最佳拟合?

解决方法

为了阐明图表上的符号,定义下一个新变量:

enter image description here

人们看到,当 Y 被绘制为 X^2 的函数时,会发生完全线性化。

因此一个正确的线性模型可能是:Y=a+b.X^2

最小均方拟合非常简单:

enter image description here

作为结论,您不应使用该软件拟合数据 (x,y),而是拟合转换后的数据 (X=log(x),Y=log(y))。

当然,您可以使用自然对数代替以 10 为底的对数。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。