Clear Filters
Clear Filters

fmincon and the Objective function returned NaN warning

19 views (last 30 days)
I minize the negative of a log-likehood function to estimate the parameters of a mixed logit regression model. I use fmincon to estimate the parameters which are the minizing values of the function. Please see the code below.
options = optimset('MaxFunEvals',10000,'TolFun',1e-9,'TolX',1e-9,'Algorithm','sqp','GradObj','off','DerivativeCheck','on','Display','iter');
[theta_hat,fval,exitflag,output,~,grad,hessian] = fmincon(obj,theta_ig,[],[],[],[],lb,ub,[],options);
During the iterations of fmincon, I see the warning "Objective function returned NaN..." few times. At the end, the minimizing values are found. But I still wonder whether I should take the warning as a sign of something going wrong. Am I supposed to never see this warning during the iterations or would it be fair to say that this can happen and does not necesserily point to a problem. In short, how much should I be concerned about the warning?
Objective function returned NaN; trying a new point...
3 93 1.320737e+04 0.000e+00 8.235e-02 2.297e+01 5.692e+04
4 99 1.176385e+04 0.000e+00 1.000e+00 2.141e+01 1.676e+04
5 105 1.162947e+04 0.000e+00 1.000e+00 2.265e+00 2.635e+03
6 111 1.162639e+04 0.000e+00 1.000e+00 4.153e-01 5.914e+01
7 117 1.162633e+04 0.000e+00 1.000e+00 1.436e-01 8.179e+01
8 123 1.162604e+04 0.000e+00 1.000e+00 8.893e-01 4.767e+02

Accepted Answer

Matt J
Matt J on 13 Oct 2021
Edited: Matt J on 13 Oct 2021
The sqp algorithm can recover from NaNs and Infs if your objective and constraint functions are finite on an open domain of x (I don't know about closed domains). The warning is probably just to alert you that the algorithm strayed outside the non-NaN domain of the objective and had to backtrack.
Snoopy on 15 Oct 2021
Let me consider two cases. In the first case the error is displayed in the first couple of itrations. In this case the given answer to the question is intitutive because probably the initial guess for the minimum is far from where the minimum is. In the second case the error is displayed somewhere mid way of all iterations. This case, which in fact applies to my case, looks more worrisome to me. Because over the iterations the search algorithm should be getting closer to a mnimum but all of a sudden the objective function returns an a NaN. Is my reasoning plausible? If yes, then I should worry that my objective function has a problem.
Matt J
Matt J on 15 Oct 2021
Edited: Matt J on 15 Oct 2021
There are no conclusions that can be drawn knowing only when in the iteration sequence the NaNs occur. We would need to see the objective function.
However, you say that the minimum is reached regardless of when the NaNs occur. That seems to suggest that the solver is doing its job, and demonstrating the capability that it advertises of recovering from NaNs.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!