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

josef.pktd@gmai... josef.pktd@gmai...
Sat Oct 6 09:30:48 CDT 2012

```On Fri, Oct 5, 2012 at 3:03 PM, Jose Guzman <sjm.guzman@gmail.com> wrote:
> 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:

this doesn't work if x is an array, you need to assign

mask = (x>0)
val[mask] = ...
val[~mask] = ...

Josef

>          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/
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
```

More information about the SciPy-User mailing list