Here I have defined a function to return the sum of an arbitrary number of Gaussian Distributions:
import numpy
from numpy import *
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def GaussSum(x,*p):
n=len(p)/3
A=p[:n]
w=p[n:2*n]
c=p[2*n:3*n]
return sum([ A[i]*exp(-(x-c[i])**2./(2.*(w[i])**2.))/(2*pi*w[i]**2)**0.5 for i in range(n)])
I then proceed to generate x and y data for a given set of parameters and ask curve_fit to fit this data with the initial parameters matching the generating set. I tried this for many different sets including both single and multiple Gaussians.
params = [1.,1.,-3.]; #parameters for a single gaussian
#params=[1.,1.,1.,2.,-3.,0.]; #parameters for the sum of two gaussians
xdata=arange(-6,6,0.01)
ydata = array([GaussSum(x,*params) for x in xdata])
popt,pcov = curve_fit(GaussSum,xdata,ydata,p0=params)
print popt
print pcov
Every parameter set gives me a non-fits, even though I already supposedly started the fit at the solution. (In the above single Gaussian):
[ 52.18242366 5549.66965192 15678.51803797]
inf
I know that the function itself operates normally, as I have plotted with it and verified its validity.