Three-term gaussian fit to gaussian data (python)

我是研究僧i 提交于 2019-12-12 03:45:55

问题


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()

Here is the result:

How may I fix this code to improve the fit? Thanks


回答1:


With the help of friends from scipy-user forum, I tried as initial guess the following:

p0=[V.max(), std_dev, V.max(), 2]

The fit got a lot better. The new fit is as shown

enter image description here

I hope the fit could get better than this.



来源:https://stackoverflow.com/questions/43225835/three-term-gaussian-fit-to-gaussian-data-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!