MATLAB: What does an exitflag of greater than 1 indicate when using the FMINCON function in the Optimization Toolbox

exitfirstorderoptflagOptimization Toolbox

I use the FMINCON function in the Optimization Toolbox to perform an optimization, and FMINCON returns an exitflag of 4. According to the documentation, an exitflag of 4 means the following:
Magnitude of the search direction was less than the specified tolerance and constraint violation was less than options.TolCon.
From the above description, it is not apparent whether the optimization converges successfully or not.

Best Answer

As of MATLAB 7.8 (R2009a), the exit flags for FMINCON and other functions in the Optimization Toolbox have been modified to explain why the function has terminated, and whether or not that termination was successful.
For all versions previous to MATLAB 7.8 (R2009a), please see below:
When FMINCON returns an exitflag of greater than 1, the results returned may not be the true optimal solution, in the sense that the gradient may not meet the specified or default tolerances. The returned results are in the feasible region, but the optimization algorithm has stalled at a certain point and cannot continue.
An exitflag of greater than 1 should not be immediately interpreted as a failure in the optimization. You should inspect the "firstorderopt" field of the output structure returned as the fourth output argument by FMINCON, to see how close the solution is to the true optimizer. Values of output.firstorderopt close to the tolerance "TolFun", indicate converged parameter vectors closer to the true optimizer. Larger values of output.firstorderopt call for more caution in using the converged parameter vector.
First-order optimality in FMINCON is defined as the violation of the Karush-Kuhn-Tucker (KKT) condition, stated as follows:
grad_f = sum lambda(i)*a(i)
lambda_ineq.*c_ineq
where "grad_f" is the gradient of the objective function at the solution, "lambda" are the Lagrange multipliers, "a" are the gradients of the constraints that are active at the solution, "c_ineq" are the inequality constraints (bounds, linear, and nonlinear), and "lambda_ineq" are the corresponding Lagrange multipliers. The "firstorderopt" output is a measure of the violation of the above condition.
If the "firstorderopt" output of your optimization is large, you should try using different initial values, or check whether your optimization has any scaling problems, to see if you can improve the optimization to obtain a better "firstorderopt" output.