Fitting to a combination of gaussian functions

28 views
Skip to first unread message

Jose Guzman

unread,
Oct 5, 2012, 3:38:00 PM10/5/12
to scipy...@googlegroups.com


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) ) )
else:
val = a*exp( -( (x-b)**2/(2*c1**2) ) )
return(val)

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

--
Jose Guzman
http://www.ist.ac.at/~jguzman/


Reply all
Reply to author
Forward
0 new messages