morse潜在适合使用python和曲线拟合scipy

时间:2016-03-30 14:45:45

标签: python scipy

我正在尝试使用python和scipy来适应莫尔斯潜力。 莫尔斯潜力定义为:

V = D*(exp(-2*m*(x-u)) - 2*exp(-m*(x-u)))

其中Dmu是我需要提取的参数。

不幸的是,你可以看到合适的效果并不令人满意(对不起,我没有10点声望,因此必须点击图像)。有人可以帮帮我吗?我必须说我不是python的最佳程序员。

这是我的代码:

from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt


xdata2=np.array([1.0 ,1.1 ,1.2 ,1.3 ,1.4 ,1.5 ,1.6 ,1.7 ,1.8 ,1.9 ,2.0 ,2.1 ,2.2 ,2.3 ,2.4 ,2.5 ,2.6 ,2.7 ,2.8 ,2.9 ,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 ,4.8 ,4.9 ,5.0 ,5.1 ,5.2 ,5.3 ,5.4 ,5.5 ,5.6 ,5.7 ,5.8 ,5.9])
ydata2=[-1360.121815,-1368.532641,-1374.215047,-1378.090480,-1380.648178,-1382.223113,-1383.091562,-1383.479384,-1383.558087,-1383.445803,-1383.220380,-1382.931531,-1382.609269,-1382.273574,-1381.940879,-1381.621299,-1381.319042,-1381.036231,-1380.772039,-1380.527051,-1380.301961,-1380.096257,-1379.907700,-1379.734621,-1379.575837,-1379.430693,-1379.299282,-1379.181303,-1379.077272,-1378.985220,-1378.903626,-1378.831588,-1378.768880,-1378.715015,-1378.668910,-1378.629996,-1378.597943,-1378.572742,-1378.554547,-1378.543296,-1378.539843,-1378.543593,-1378.554519,-1378.572747,-1378.597945,-1378.630024,-1378.668911,-1378.715015,-1378.768915,-1378.831593]


t=np.linspace(0.1,7)

def morse(q, m, u, x ):
    return (q * (np.exp(-2*m*(x-u))-2*np.exp(-m*(x-u))))

popt, pcov = curve_fit(morse, xdata2, ydata2, maxfev=40000000)

yfit = morse(t,popt[0], popt[1], popt[2])

print popt



plt.plot(xdata2, ydata2,"ro")
plt.plot(t, yfit)

plt.show()

Morse fit 2

在gboffi的评论之前老了 Morse Fit

1 个答案:

答案 0 :(得分:3)

我猜测莫尔斯潜力的确切深度对你不感兴趣。所以我添加了一个额外的参数来上下移动莫尔斯势(v),包括@gboffis评论。此外,函数的第一个参数必须是参数,而不是您想要拟合的参数(参见http://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.optimize.curve_fit.html

此外,这种拟合取决于您的起始位置。以下内容应该可以满足您的需求。

from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt


xdata2=np.array([1.0 ,1.1 ,1.2 ,1.3 ,1.4 ,1.5 ,1.6 ,1.7 ,1.8 ,1.9 ,2.0 ,2.1 ,2.2 ,2.3 ,2.4 ,2.5 ,2.6 ,2.7 ,2.8 ,2.9 ,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 ,4.8 ,4.9 ,5.0 ,5.1 ,5.2 ,5.3 ,5.4 ,5.5 ,5.6 ,5.7 ,5.8 ,5.9])
ydata2=[-1360.121815,-1368.532641,-1374.215047,-1378.090480,-1380.648178,-1382.223113,-1383.091562,-1383.479384,-1383.558087,-1383.445803,-1383.220380,-1382.931531,-1382.609269,-1382.273574,-1381.940879,-1381.621299,-1381.319042,-1381.036231,-1380.772039,-1380.527051,-1380.301961,-1380.096257,-1379.907700,-1379.734621,-1379.575837,-1379.430693,-1379.299282,-1379.181303,-1379.077272,-1378.985220,-1378.903626,-1378.831588,-1378.768880,-1378.715015,-1378.668910,-1378.629996,-1378.597943,-1378.572742,-1378.554547,-1378.543296,-1378.539843,-1378.543593,-1378.554519,-1378.572747,-1378.597945,-1378.630024,-1378.668911,-1378.715015,-1378.768915,-1378.831593]


t=np.linspace(0.1,7)

tstart = [1.e+3, 1, 3, 0]
def morse(x, q, m, u , v):
    return (q * (np.exp(-2*m*(x-u))-2*np.exp(-m*(x-u))) + v)

popt, pcov = curve_fit(morse, xdata2, ydata2, p0 = tstart,  maxfev=40000000)
print popt # [    5.10155662     1.43329962     1.7991549  -1378.53461345]


yfit = morse(t,popt[0], popt[1], popt[2], popt[3])

#print popt
#
#
#
plt.plot(xdata2, ydata2,"ro")
plt.plot(t, yfit)

plt.show()

enter image description here