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

Daπid davidmenhur at gmail.com
Wed Apr 5 11:41:05 EDT 2017


On 5 April 2017 at 17:24, Otto Ngeke <lieemlieem at gmail.com> wrote:

> I still get the same fit as before. However, I am worried because my
> Gaussians are such that the amplitude of the second Gaussian term is the
> standard deviation of the first: some kind of coupling between the terms. I
> don't know if this new function  definition actually represents that
> property.


It should be:

def func(x, params):
    ps1, ps2, ps3, ps4 = params
    return ps1*np.exp(-(x/ps2)**2) + ps2*np.exp(-(x/ps3)**2) +
ps3*np.exp(-(x/ps4)**2)


Note that in your initialisation you have a symmetry between the first and
the third Gaussian:

popt, pcov = optimize.curve_fit(func, r, V, p0=[50, std_dev, 50, std_dev],
maxfev=10000)

So unless curve_fit adds some random noise, the two are going to be always
the same because you cannot break the symmetry. Instead, give them
different values, for example:

[50, 1.2 * std_dev, 30, std_dev / 1.2]

(I have no idea if this particular values make sense, but they do will
allow you to get two independent Gaussians, not too far from your initial
guess.)


For hard fits (and arbitrary fits are surprisingly hard!), iminuit is a
quite powerful option.


/David.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170405/1d7b4e00/attachment.html>


More information about the SciPy-User mailing list