本文介绍了高斯数据的三项高斯拟合(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使高斯数据适合特定的三项高斯(其中一项的幅度等于下一项的标准偏差的两倍).这是我的尝试:
I am trying to fit a gaussian data to a specific three-term gaussian (in which the amplitude in one term is equal to twice the standard deviation of the next term). Here is my attempt:
import numpy as np
#from scipy.optimize import curve_fit
import scipy.optimize as optimize
import matplotlib.pyplot as plt
#r=np.linspace(0.0e-15,4e-15, 100)
data = np.loadtxt('V_lambda_n.dat')
r = data[:, 0]
V = data[:, 1]
def func(x, ps1, ps2, ps3, ps4):
return ps1*np.exp(-(x/ps2)**2) + ps2*np.exp(-(x/ps3)**2) + ps3*np.exp(-(x/ps4)**2)
popt, pcov = optimize.curve_fit(func, r, V, maxfev=10000)
#params = optimize.curve_fit(func, ps1, ps2, ps3, ps4)
#[ps1, ps2, ps2, ps4] = params[0]
p1=plt.plot(r, V, 'bo', label='data')
p2=plt.plot(r, func(r, *popt), 'r-', label='fit')
plt.xticks(np.linspace(0, 4, 9, endpoint=True))
plt.yticks(np.linspace(-50, 150, 9, endpoint=True))
plt.show()
这是结果:
如何修复此代码以提高匹配度?谢谢
How may I fix this code to improve the fit? Thanks
推荐答案
在scipy-user论坛的朋友的帮助下,我尝试了以下尝试:
With the help of friends from scipy-user forum, I tried as initial guess the following:
p0 = [V.max(),std_dev,V.max(),2]
p0=[V.max(), std_dev, V.max(), 2]
适合度提高了很多.新的拟合如图所示
The fit got a lot better. The new fit is as shown
我希望比以前更好.
这篇关于高斯数据的三项高斯拟合(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!