[SciPy-User] Fitting to a combination of gaussian functions

Jose Guzman sjm.guzman@gmail....
Fri Oct 5 14:03:28 CDT 2012

Dear colleagues,

I wanted to fit some data to a function that contains the combination of 
2 gaussian functions of different widths (the same height and position 
of the peak). For that I created the following function:

def gaussian_func(x, a, b, c1, c2):
     a is the height of curve peak
     b is the position of the center of the peak
     c1 is the width for negative values of x
     c2 is the width for positive values of x
     if x>0:
         val = a*exp( -( (x-b)**2/(2*c2**2) ) )
         val = a*exp( -( (x-b)**2/(2*c1**2) ) )

But when I try to fit the data with scipy.optimize.curve_fit i get the 
following error:

"The truth value of an array with more than one element is ambiguous. 
Use a.any() or a.all()"

For example:

xdata = np.array([21, 36, 53, 67,60,66, 30,36, 19])
ydata = np.array([-100.  -50.  -20.  -10.    0.   10.   20.   50.  100.])

curve_fit(gaussian_func, xdata, ydata)

I guess this is because the function is vectorized. Is there any way to 
avoid this behaviour or any other way to fit these data ?

Thanks in advance


Jose Guzman

More information about the SciPy-User mailing list