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