Message Boards Message Boards

0
|
7148 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Fit data with a proper Gaussian using NonlinearModelFit?

Posted 5 years ago

Hi everybody. I need help! I'm trying to fit my data set with a Gaussian, but I was able to get a curve that doesn't look like a Gaussian. I'm noob with this, so you guys please help me! At least I wanna have a hint about how can I handle the parameters to get the best as possible fit. This is what I already have done...

data = Import[
   "/home/leblanc/Documents/Astro/Paper/a.csv", "table", FieldSeparators -> " "];
nlm = NonlinearModelFit[data, a Exp[-(x - b)^2/2 c^2], {a, b, c}, x];
nlm["AdjustedRSquared"];
nlm[x];
nlm["ParameterTable"];
dataplot = ListPlot[data];
fitplot = Plot[nlm[x], {x, 1.8, 2.00}];
Show[dataplot, fitplot];
Attachment

Attachments:
5 Replies

Mariusz did a great job !

But his explanation is somewhat short. Unfortunately I can't access your data, but I would proceed as follows.

Looking at your data-points there seems to be something like a straight line at the right and left end. Get some of the first and some of the last data and do a linear fit, then you will get a first approximation of a linear function to be used as "baseline". Then extract the data in the middle. These could be a Gaussian like

a + b Exp[ w ( x - x0 )^2 ]

a is something around 100, b as well and w has something to do with the half-width, which can be estimated as about 0.02. So find a value for w. Fit it to the extracted data, and you will get starting parameters for the overall fit of all your data to

a + b Exp[ w ( x - x0 )^2 ]  + c x 
POSTED BY: Hans Dolhaine

Hi Mariuz. I appreciated a lot your help with the fitting, I need more details about how you reached that amazing fit, because I need to include in a research paper. If you can help me with to understand how to choose accurate fit parameters, you will save my life. Thanks a lot!

Christian.

I'm use Trial and Error,and knowledge of function properties (like Gaussian,and line),and that's all.

Regards Mariusz.

POSTED BY: Mariusz Iwaniuk

Thank you crack!

Adjustment:

 data = Import["a.csv", "table", FieldSeparators -> " "];
 nlm = NonlinearModelFit[data, 
    d + a*Exp[-(x - b)^2/2 c^2], {{a, 100}, {b, 1.94}, {c, 90}, {d, 
      95}}, x];
 nlm["AdjustedRSquared"];
 nlm[x];
 nlm["ParameterTable"];
 dataplot = ListPlot[data];
 fitplot = 
   Plot[nlm[x], {x, 1.8, 2.10}, PlotStyle -> Red, PlotRange -> All];
 Show[dataplot, fitplot]

enter image description here

A better model:

 data = Import["a.csv", "table", FieldSeparators -> " "];
 nlm = NonlinearModelFit[data, 
    d + a*Exp[-(x - b)^2/2 c^2] + 
     e*x, {{a, 100}, {b, 1.94}, {c, 90}, {d, 95}, {e, 200}}, x];
 nlm["AdjustedRSquared"];
 nlm[x];
 nlm["ParameterTable"];
 dataplot = ListPlot[data];
 fitplot = 
   Plot[nlm[x], {x, 1.8, 2.10}, PlotStyle -> Red, PlotRange -> All];
 Show[dataplot, fitplot]

enter image description here

POSTED BY: Mariusz Iwaniuk
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