# NonlinearModelFit - "Encountered a gradient that is effectively zero", all variables are 1, 1, 1 [duplicate]

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!

• 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. Dec 24 '14 at 23:14
• I find the fit works fine if you initialize parameters with reasonable values, e.g., {{a, .1}, {b, 149}, {c, 1}}. Dec 24 '14 at 23:40

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];

Normal[nlm]


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]