# NonlinearModelFit::fmgz: Encountered a gradient that is effectively zero

I am trying to solve a nonlinear parameter fitting problem as described by Mathematica code below:

ClearAll["Global*"]
A=Rationalize@1.44928 ;a=Rationalize@0.32161; b=Rationalize@1.27495;
data=Rationalize@{{0.005,0.20021},{0.00778,0.30532},{0.01056,0.42544},{0.01333,0.53054},{0.01622,0.65066},{0.019,0.77078},{0.02178,0.87589},{0.02478,0.98099},{0.02756,1.0861},{0.03033,1.17619},{0.03311,1.28129},{0.03589,1.35637},{0.04156,1.50652},{0.04711,1.65667},{0.05267,1.77679},{0.05822,1.91192},{0.06389,2.01703},{0.06944,2.12213},{0.07489,2.21222},{0.086,2.37739},{0.09133,2.43745},{0.10778,2.64766},{0.12411,2.79781},{0.14044,2.93294},{0.15822,3.05306},{0.17456,3.12814},{0.19089,3.21823},{0.20722,3.27829},{0.22356,3.33835},{0.23989,3.38339},{0.25611,3.42844},{0.27244,3.47348},{0.28878,3.50351},{0.30511,3.51853},{0.32144,3.54856},{0.33778,3.56357},{0.35411,3.57859},{0.37044,3.57859},{0.38667,3.5936},{0.403,3.5936},{0.40544,3.5936},{0.45578,3.60862},{0.50611,3.57859},{0.55656,3.56357},{0.60689,3.53354},{0.65722,3.4885},{0.70756,3.45847},{0.75789,3.42844},{0.80822,3.38339},{0.85867,3.33835}};

σ[x_?NumberQ]:=(2 A (Exp[a(x^2+2/x-3)]-b Log@(x^2+2/x-2))+NIntegrate[4 p1 (Exp[p2 (t^2+2/t-3)]-p3 Log@(t^2+2/t-2))(t-t^-2)Exp[3(x-t)],{t,1,x}])(x^2-1/x)

nlm=NonlinearModelFit[data,σ[x],{p1,p2,p3},x]


but got the following output

Out[8]= FittedModel[σ[x]]

and error message:

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.

I tried to set some initial guesses of the parameters, but got similiar errors.

Is it possible to solve such a problem by NonlinearModelFit? How can I use Differential Evolution in NMinimize without the problem of complex function/evaluation values?

Further error messages like:

evaluated to non-numerical values for all sampling points in the \ region with boundaries NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in t near {t} = {}. NIntegrate obtained and for the integral and error estimates.

• What are your estimated values? I got the code working, but values are way off. – Feyre Nov 5 '16 at 12:17
• When estimating $\sigma(x)$ at specific $p_1,p_2,p_3$ values, for example $p_1=A,p_2=a,p_3=b$, I got such errors: \[Sigma][2] /. {p1 -> A, p2 -> a, p3 -> b} – user6043040 Nov 5 '16 at 12:26
• Right after the ClearAll statement, I assigned values to $A,a,b$, these are constants; while the parameters to fit are $p_1,p_2,p_3$. – user6043040 Nov 5 '16 at 12:29
• Those estimates don't seem to be very accurate. – Feyre Nov 5 '16 at 12:37
• You can implement differential evolution in NonlinearModelFit using ..., Method -> {NMinimize, Method -> {"DifferentialEvolution"}}]. I gave it a try with your data, but was unable to find a good fit. You can modify the method using standard options with ..., Method -> {NMinimize, Method -> {"DifferentialEvolution", "ScalingFactor" -> 0.9, "CrossProbability" -> 0.1}}], or with any other option of "DifferentialEvolution". – Marchi Nov 5 '16 at 16:25

This is more of an extended comment. First one notices that p2 is the only non-linear parameter. Because of that one can set p2 to some value and then just use linear regression to estimate p1 and p13=p1*p3 conditional on the value of p2. The only reason for doing this is to avoid (at least potentially) convergence issues with NonlinearModelFit as there is an explicit formula for the linear regression parameter estimates and no starting values are needed.

One would create new variables z1, z2, and z3 as the numerical integration would only have to be done once for each value of p2:

x = data[[All, 1]];
p2 = -100
z1 = 2 A (Exp[a (x^2 + 2/x - 3)] - b Log@(x^2 + 2/x - 2)) (x^2 - 1/x);
z2 = Table[(x[[i]]^2 - 1/x[[i]]) NIntegrate[Exp[p2 (t^2 + 2/t - 3)] (t - t^-2) Exp[3 (x[[i]] - t)],
{t, x[[i]], 1}], {i, Length[x]}];
z3 = Table[-4 (x[[i]]^2 - 1/x[[i]]) NIntegrate[
Log@(t^2 + 2/t - 2) (t - t^-2) Exp[3 (x[[i]] - t)], {t, x[[i]], 1}],
{i, Length[x]}];
data2 = Transpose[{z1, z2, z3, data[[All, 2]]}];
lm = LinearModelFit[data2, {x1, x2, x3}, {x1, x2, x3}, IncludeConstantBasis -> False]
lm["RSquared"]


If a value for p2 could be found that maximizes RSquared or minimizes AIC, then that value and the associated values could be plugged in as starting values for NonlinearModelFit.

However, a value of p2 = -500 still doesn't maximize the RSquared value (which suggest that p2 is even smaller than -500) so either I've made some mistake in translating the formulas, the original formulas are wrong, one or more of the constants A, a, and b` are wrong, or (which many times happens in practice) the data is not generated from the expected form of the model.