【发布时间】:2020-05-05 09:59:29
【问题描述】:
我正在尝试使用curve_fit 拟合对数曲线,假设它遵循Y=a*ln(X)+b,但拟合的数据仍然看起来不正确。
现在我正在使用以下代码:
from scipy.optimize import curve_fit
X=[3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3, 4.4,
4.5, 4.6, 4.7]
Y=[-5.890486683, -3.87063815, -2.733484754, -2.104972457, -1.728190699,
-1.477976987, -1.285589215, -1.120224363, -0.968576581, -0.82492453,
-0.688457731, -0.559780327, -0.440437932, -0.331886009, -0.235162505,
-0.150572236, -0.078157925, -0.01718885]
#plot Y against X
fig = plt.figure(num=None, figsize=(9, 7),facecolor='w', edgecolor='k')
ax2=fig.add_subplot(111)
ax2.scatter(X,Y)
#fit using curve_fit
popt, pcov = curve_fit(Hyp_func, X, Y,maxfev=10000)
print(' fit coefficients:\n', popt)
#fit coefficients:
#[9.51543579 -14.10114674]
#plot Y_estimated against X
Y_estimated=[popt[0]*np.log(i)+popt[1] for i in X]
ax2.scatter(X,Y_estimated, c='r')
def Hyp_func(x, a,b):
return a*np.log(x)+b
拟合曲线(红色)看起来仍然不像读取曲线(蓝色)那样“弯曲”。任何帮助,将不胜感激。
【问题讨论】:
-
curve_fit是scipy.optimize.curve_fit? -
@WilliamMiller 请看我对这个问题的回答。
标签: python numpy matplotlib curve-fitting logarithm