I have data of the form {149.45, 0.371093}, {149.362, 0.375976}, {149.277, 0.380858}, ... }, where each element of the list is {x, y}. The plot of this, using ListPlot, gives an approximate Gaussian. I'm trying to find a Gaussian fit for this, but this happens:

nlm = NonlinearModelFit[ data, a*Exp[-((x - b)/c)^2], {a, b, c}, x]
NonlinearModelFit::fmgz: Encountered a gradient that is effectively zero. The result returned may not be a minimum; it may be a maximum or a saddle point. >>

It then gives a, b, c as 1, 1, 1. Any inputs appreciated. Thanks!

  • $\begingroup$ Hi ! Please head to the help centre and read about proper code formatting practicies and format your code accordingly. $\endgroup$
    – Sektor
    Dec 24, 2014 at 20:45
  • 2
    $\begingroup$ Use basic facts of statistics here: The mean of the fit Gaussian, b, is the mean of the data and the variance of the Gaussian, c^2, is the variance of the data and the normalization is a = 1/(Sqrt[2 Pi] c). Compute those directly from your data. If you wish, at the very least you can use those computed statistics to serve as initial values for your NonlinearModelFit. $\endgroup$ Dec 24, 2014 at 23:14
  • $\begingroup$ I find the fit works fine if you initialize parameters with reasonable values, e.g., {{a, .1}, {b, 149}, {c, 1}}. $\endgroup$ Dec 24, 2014 at 23:40

1 Answer 1

data = Transpose[
       {xList = RandomVariate[NormalDistribution[149, 2], 50],
        yList = PDF[NormalDistribution[149, 2], #] & /@ xList}

I've recast your Gaussian model to be in a more standard form and put in reasonable starting values:

nlm = NonlinearModelFit[data,
   a Exp[-(x - b)^2/(2 c^2)],
   {{a, .1}, {b, 149}, {c, .5}}, x];


0.199471 E^(-0.125 (-149. + x)^2)

Note that indeed the fit mean is b = 149 (same as the generating model) and c = 2 (same as the generating model).

Your default starting value of b = 1 is so far from the final solution that the gradient needed to find the final value is too small for NonlinearModelFit to give the proper solution.

Plot[nlm[x], {x, 130, 170},
 Epilog :> Point[data], PlotStyle -> {Red, Thick}, PlotRange -> All]

Gaussian fit figure


Not the answer you're looking for? Browse other questions tagged or ask your own question.