[SciPy-User] Three-term gaussian fit to gaussian data using scipy

Otto Ngeke lieemlieem at gmail.com
Wed Apr 5 09:16:22 EDT 2017


Hi ALl,

I am trying to a three-term gaussian with a specific form to fit a data
set. The fit isn't working out as I nicely. Could you help me out? Here
attached is my code and the data, along with the fit I obtain:

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]

std_dev=np.std(data)

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, p0=[50, std_dev, 50, std_dev],
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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170405/bf7804b7/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: V_lambda_n.dat
Type: application/octet-stream
Size: 7680 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170405/bf7804b7/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gaussian_fit.png
Type: image/png
Size: 23436 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170405/bf7804b7/attachment-0001.png>


More information about the SciPy-User mailing list