Message Boards Message Boards

0
|
6723 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Get a better model fit for 3 peaks of data?

Posted 8 years ago

I'm trying to make a model that will fit 3 peaks of data well. I am trying to do Gaussian or Lorentzian. My Gaussian attempt looks like this:

A[x_] := a E^-((x - b)^2/(2 c^2));
B[x_] := d E^-((x - e)^2/(2 f^2));
F[x_] := g E^-((x - h)^2/(2 i^2));
G[x_] := A[x] + B[x] + F[x]  + q;
data = {{0.10026`, 44}, {0.099683`, 42}, {0.09911`, 56}, {0.098537`, 
    41}, {0.097964`, 48}, {0.097392`, 39}, {0.096819`, 
    54}, {0.096246`, 48}, {0.095673`, 51}, {0.0951`, 38}, {0.094527`, 
    40}, {0.093954`, 48}, {0.093381`, 58}, {0.092808`, 
    88}, {0.092235`, 82}, {0.091663`, 87}, {0.09109`, 
    101}, {0.090517`, 97}, {0.089944`, 103}, {0.089371`, 
    115}, {0.088798`, 124}, {0.088225`, 150}, {0.087652`, 
    160}, {0.087079`, 199}, {0.086507`, 194}, {0.085934`, 
    232}, {0.085361`, 275}, {0.084788`, 267}, {0.084215`, 
    322}, {0.083642`, 390}, {0.083069`, 349}, {0.082496`, 
    355}, {0.081923`, 387}, {0.081351`, 408}, {0.080778`, 
    390}, {0.080205`, 343}, {0.079632`, 402}, {0.079059`, 
    372}, {0.078486`, 417}, {0.077913`, 392}, {0.07734`, 
    444}, {0.076767`, 460}, {0.076195`, 459}, {0.075622`, 
    477}, {0.075049`, 513}, {0.074476`, 543}, {0.073903`, 
    554}, {0.07333`, 594}, {0.072757`, 579}, {0.072184`, 
    731}, {0.071611`, 728}, {0.071039`, 779}, {0.070466`, 
    865}, {0.069893`, 960}, {0.06932`, 1029}, {0.068747`, 
    1128}, {0.068174`, 1295}, {0.067601`, 1431}, {0.067028`, 
    1578}, {0.066455`, 1788}, {0.065882`, 2118}, {0.06531`, 
    2296}, {0.064737`, 2476}, {0.064164`, 2603}, {0.063591`, 
    2778}, {0.063018`, 2803}, {0.062445`, 2844}, {0.061872`, 
    2709}, {0.061299`, 2695}, {0.060726`, 2657}, {0.060154`, 
    2741}, {0.059581`, 2918}, {0.059008`, 3059}, {0.058435`, 
    3323}, {0.057862`, 3724}, {0.057289`, 3921}, {0.056716`, 
    4279}, {0.056143`, 4353}, {0.05557`, 4450}, {0.054998`, 
    4174}, {0.054425`, 3660}, {0.053852`, 3139}, {0.053279`, 
    2616}, {0.052706`, 2275}, {0.052133`, 1923}, {0.05156`, 
    1791}, {0.050987`, 1537}, {0.050414`, 1465}, {0.049842`, 
    1367}, {0.049269`, 1249}, {0.048696`, 1186}, {0.048123`, 
    1127}, {0.04755`, 1064}, {0.046977`, 994}, {0.046404`, 
    966}, {0.045831`, 987}, {0.045258`, 957}, {0.044686`, 
    986}, {0.044113`, 921}, {0.04354`, 923}, {0.042967`, 
    950}, {0.042394`, 936}, {0.041821`, 920}, {0.041248`, 
    968}, {0.040675`, 908}, {0.040102`, 977}, {0.039529`, 
    1014}, {0.038957`, 1057}, {0.038384`, 1185}, {0.037811`, 
    1329}, {0.037238`, 1372}, {0.036665`, 1593}, {0.036092`, 
    1857}, {0.035519`, 2273}, {0.034946`, 2917}, {0.034373`, 
    4028}, {0.033801`, 5376}, {0.033228`, 6738}, {0.032655`, 
    6778}, {0.032082`, 4919}, {0.031509`, 3110}, {0.030936`, 
    1698}, {0.030363`, 1339}, {0.02979`, 978}, {0.029217`, 
    601}, {0.028645`, 478}, {0.028072`, 366}, {0.027499`, 
    326}, {0.026926`, 254}, {0.026353`, 250}, {0.02578`, 
    205}, {0.025207`, 188}, {0.024634`, 163}, {0.024061`, 
    141}, {0.023489`, 144}, {0.022916`, 172}, {0.022343`, 
    176}, {0.02177`, 190}, {0.021197`, 168}, {0.020624`, 
    188}, {0.020051`, 210}, {0.019478`, 207}, {0.018905`, 
    241}, {0.018333`, 228}, {0.01776`, 225}, {0.017187`, 
    207}, {0.016614`, 160}, {0.016041`, 127}, {0.015468`, 
    80}, {0.014895`, 25}, {0.014322`, 0}, {0.013749`, 0}, {0.013176`, 
    0}, {0.012604`, 2}, {0.012031`, 0}, {0.011458`, 2}, {0.010885`, 
    2}, {0.010312`, 0}, {0.0097392`, 2}, {0.0091663`, 1}, {0.0085934`,
     0}, {0.0080205`, 2}, {0.0074476`, 0}, {0.0068747`, 
    1}, {0.0063018`, 0}, {0.0057289`, 1}, {0.005156`, 0}, {0.0045831`,
     1}, {0.0040102`, 0}, {0.0034373`, 0}};
nlm = NonlinearModelFit[data, {G[x]},
  {a, b, c, d, e, f, g, h, i, q}, x, ConfidenceLevel -> .95, 
  MaxIterations -> Infinity]
Normal[nlm]
Show[ListPlot[data], Plot[nlm[x], {x, 0, .1}]]

This seems to get a general shape of the data, but it fits one of the peaks well, then combined the other two and uses the third Gaussian as a very wide Gaussian. I want to try to force the program to be near the approximate area of these peaks. Does anyone have any idea how to set better constraints when I try to change the variable or constraint part my computer will sit "running" for long periods of time with no result.

Thank you

POSTED BY: Nicholas Breslin
4 Replies
Posted 8 years ago

Also, the Gaussian fits aren't great. I have a feeling you may want to try Voigt profiles and something like a cubic background. Unfortunately, in my experience, Mathematica isn't great with Voigt fittings. There are better programs for peak fitting, but I'm not sure if we are allowed to recommend other programs on this forum.

POSTED BY: Will Smith
Posted 8 years ago
peaks = Part[data, FindPeaks[data[[All, 2]], 1, 0, 2000][[All, 1]]];
p1 = ListPlot[data, 
   Epilog -> {Red, PointSize[0.02], Point[peaks]}];
nlm = NonlinearModelFit[
   data, {G[x]}, {{a, peaks[[1, 2]]}, {b, peaks[[1, 1]]}, {c, 
     0.004}, {d, peaks[[2, 2]]}, {e, peaks[[2, 1]]}, {f, 0.004}, {g, 
     peaks[[3, 2]]}, {h, peaks[[3, 1]]}, {i, 0.004}, {q, 0}}, x, 
   ConfidenceLevel -> .95, MaxIterations -> Infinity];
nlm["ParameterTable"]
GraphicsRow[{Show[p1, Plot[nlm[x], {x, 0, .1}, PlotStyle -> Green], 
   Frame -> True, PlotLabel -> "data and fit"], 
  ListPlot[Transpose[{data[[All, 1]], nlm["FitResiduals"]}], 
   Frame -> True, PlotLabel -> "Residuals"]}, ImageSize -> Large]

Hope that helps.

POSTED BY: Will Smith

For initial parameters you can catch the peaks as

ts = TimeSeriesResample[TimeSeries[data]];
peaks = FindPeaks[ts];
ListPlot[{ts, peaks}, Joined -> {True, False}, Filling -> {2 -> 0}]
POSTED BY: Vitaliy Kaurov

In the documentation for NonlinearModelFit, under Details and Options, the 4th bullet describes how to give the function starting values for the parameters to be fitted. That may help you get the fit you want.

NonlinearModelFit[data, form, {par1,p1},...},vars] starts the search with a fit with par1 -> p1, ...

POSTED BY: Frank Kampas
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