Message Boards Message Boards

0
|
29120 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 10 years ago

I am using Mathematica for some time now for evaluating experimental data for my practical physics course but I always had problems with fitting nonlinear functions to data. Currently I'd like to fit a gaussian function to my dataset. I tired several options including "FindFit" and "NonlinearModelFit" but the resulting function not even remotely resembles the curve of the original data. Sometimes the gaussian is centered around a very small, insignificant peak in my dataset but most frequently the height of the peak is way too small.

The attachment contains some data as an example. If plotted, the quite significant peak at the right end is what I am interested in.

For example I used the function FindFit with the following parameters:

FindFit[data, 
 A*1/(\[Sigma]*Sqrt[2 \[Pi]])
   Exp[-(1/2) ((x - \[Mu])/\[Sigma])^2], {\[Sigma], \[Mu], A}, x]

The variable A obviously is used to scale the area under the curve. Even if I cut down all the data but the peak I want to fit the gaussian to, using

Take[data, {1700, 1990}]

I get values like

{\[Sigma] -> 1.74659, \[Mu] -> 2.29208, A -> 1628.23}

Which is obviously no good, considering the highest value in my data-list is 38635 and therefore bigger than A. (mu and sigma are also way off)

I have to stress, that to this point I have never learned how fitting to nonlinear data generally works. I taught myself how to work with Mathematica using the incredibly detailed and well designed documentation of Mathematica. It might be, that I misunderstand something at a very basic level.

Thanks in advance to the community :-)

Thomas

Attachments:
POSTED BY: Thomas H
5 Replies
Posted 10 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 10 years ago

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

POSTED BY: Thomas H
Posted 10 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
Posted 10 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 10 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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract