Group Abstract Group Abstract

Message Boards Message Boards

0
|
30.5K Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How can I fit a gaussian function to data in Mathematica? (no distribution)

Posted 11 years ago
Attachments:
POSTED BY: Thomas H
5 Replies
Posted 11 years ago

Mathematica (I use 10.1) has a function FindPeaks that should provide good starting values for mu`

nlm = NonlinearModelFit[data, 
  A 1/(\[Sigma] Sqrt[2. \[Pi]])
    Exp[-(1./2.) ((x - \[Mu])/\[Sigma])^2], {{\[Sigma], 100.}, {\[Mu],
     FindPeaks[data, 80., 0.2, 5000.][[1, 1]]}, {A, 200}}, x]
POSTED BY: Michael Helmle
Posted 11 years ago

Hello Thomas,

I guess part of the problem is that your data are very well described by a single gaussian and therefore the fit may end up in different local minima. One way to steer which part of the data are well approximated by the model function is to set start values for the parameters to be fitted. I prefer to use NonlinearModelFit

nlm = NonlinearModelFit[data, 
  A 1/(\[Sigma] Sqrt[2. \[Pi]])
    Exp[-(1./2.) ((x - \[Mu])/\[Sigma])^2], {{\[Sigma], 100.}, {\[Mu],
     2000}, {A, 200}}, x]

To visually compare data and fitted model you use

Show[ListPlot[data, PlotStyle -> Red, PlotRange -> All], 
 Plot[nlm[x], {x, 0, 2000}, PlotRange -> All]]

Regards, Michael

POSTED BY: Michael Helmle
Posted 11 years ago

Ah, of course! I did not think about that. Thanks a lot :-)

POSTED BY: Thomas H
Posted 11 years ago

Thanks a lot!

I was not aware that I can set start values. This information is very helpful :-)

How do the start values influence the approximated values by mathematica? I have about 30 of these datasets where I do not expect sigma to change much but mu varies a lot. The point is, I want my notebook to evaluate the data automatically and I have set up everything to this point in that way. I have a formula to predict where mu should be, but if the start value affects the outcome of mu I'd rather use a different way.

POSTED BY: Thomas H
Posted 11 years ago

1st line should read: ... part of the problem is that your data are NOT very well described by a single gaussian

POSTED BY: Michael Helmle
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard