[Numpy-discussion] lsq problem

Keith Goodman kwgoodman@gmail....
Wed Feb 14 21:35:08 CST 2007


On 2/14/07, Tommy Grav <tgrav@mac.com> wrote:
>
> On Feb 14, 2007, at 10:11 PM, Keith Goodman wrote:
>
> > On 2/14/07, Tommy Grav <tgrav@mac.com> wrote:
> >> I need to fit a gaussian profile to a set of points and would like to
> >> use scipy (or numpy) to
> >> do the least square fitting if possible. I am however unsure if the
> >> proper routines are
> >> available, so I thought I would ask to get some hints to get going in
> >> the right direction.
> >>
> >> The input are two 1-dimensional arrays x and flux, together with a
> >> function
> >>
> >> def Gaussian(a,b,c,x1):
> >>         return a*exp(-(pow(x1,2)/pow(c,2))) - c
> >>
> >> I would like to find the values of (a,b,c), such that the difference
> >> between the gaussian
> >> and fluxes are minimalized.
> >
> > You left b out of your function:  a*exp(-power((x - b),2) / c).
> >
> > If it is a one-off example you can always use brute force---just
> > search a grid:
> >
> > for a in as:
> >     for b in bs:
> >         for c in cs:
> >
> > But if you have a lot of data or you need to do many fits I bet scipy
> > has what you need.
>
> I actually butchered the function, as it should read a*exp(-pow(x,2)/
> b)) - c.
> (I do not need to fit the center as that has already been when creating
> the x array :).

Not having to fit the center is great because now you only have two
parameters to optimize. So

for a in as:
    for b in bs:


More information about the Numpy-discussion mailing list