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

 [email protected] ;[email protected]; [email protected];

 σ[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)


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.

  • $\begingroup$ What are your estimated values? I got the code working, but values are way off. $\endgroup$
    – Feyre
    Nov 5, 2016 at 12:17
  • $\begingroup$ 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} $\endgroup$ Nov 5, 2016 at 12:26
  • $\begingroup$ 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$. $\endgroup$ Nov 5, 2016 at 12:29
  • $\begingroup$ Those estimates don't seem to be very accurate. $\endgroup$
    – Feyre
    Nov 5, 2016 at 12:37
  • 1
    $\begingroup$ 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". $\endgroup$
    – Marchi
    Nov 5, 2016 at 16:25

1 Answer 1


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]

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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