Skip to Main Content Skip to Search
Product Documentation

Exit Flags and Exit Messages

Exit Flags

When an optimization solver completes its task, it sets an exit flag. An exit flag is an integer that is a code for the reason the solver halted its iterations. In general:

A table of solver outputs in the solver's function reference section lists the meaning of each solver's exit flags.

You obtain an exit flag by calling a solver with the exitflag syntax. This syntax depends on the solver. For details, see the solver function reference pages. For example, for fsolve, the calling syntax to obtain an exit flag is

[x,fval,exitflag] = fsolve(...)

The following example uses this syntax. Suppose you want to solve the system of nonlinear equations

Write these equations as an anonymous function that gives a zero vector at a solution:

myfcn = @(x)[2*x(1) - x(2) - exp(-x(1));
      -x(1) + 2*x(2) - exp(-x(2))];

Call fsolve with the exitflag syntax at the initial point [-5 -5]:

[xfinal fval exitflag] = fsolve(myfcn,[-5 -5])

Equation solved.

fsolve completed because the vector of function values is near 
zero as measured by the default value of the function tolerance, 
and the problem appears regular as measured by the gradient.

xfinal =
    0.5671    0.5671

fval =
  1.0e-006 *
   -0.4059
   -0.4059

exitflag =
     1

In the table for fsolve under Output Arguments, you find that an exit flag value 1 means "Function converged to a solution x." In other words, fsolve reports myfcn is nearly zero at x = [0.5671 0.5671].

Exit Messages

Each solver issues a message to the MATLAB command window at the end of its iterations. This message explains briefly why the solver halted. The message might give more detail than the exit flag.

Many examples in this documentation show exit messages. For example, see Minimizing at the Command Line, or Step 2: Invoke one of the unconstrained optimization routines.. The example in the previous section, Exit Flags, shows the following exit message:

Equation solved.

fsolve completed because the vector of function values is near 
zero as measured by the default value of the function tolerance, 
and the problem appears regular as measured by the gradient.

This message is more informative than the exit flag. The message indicates that the gradient is relevant. The message also states that the function tolerance controls how near 0 the vector of function values must be for fsolve to regard the solution as completed.

Enhanced Exit Messages

Some solvers have exit messages that contain links for more information. There are two types of links:

The fminunc solver has enhanced exit messages:

opts = optimset('LargeScale','off'); % LargeScale needs gradient
[xfinal fval exitflag] = fminunc(@sin,0,opts)

This yields the following results:

Each of the underlined words or phrases contains a link that provides more information.

Exit Message Options

Set the Display option to control the appearance of both exit messages and iterative display. For more information, see Iterative Display. The following table shows the effect of the various settings of the Display option.

Value of the Display OptionOutput to Command Window
Exit messageIterative Display
'none', or the synonymous 'off'NoneNone
'final' (default for most solvers)DefaultNone
'final-detailed'DetailedNone
'iter'DefaultYes
'iter-detailed'DetailedYes
'notify'Default only if exitflag ≤ 0None
'notify-detailed'Detailed only if exitflag ≤ 0None

For example,

opts = optimset('Display','iter-detailed','LargeScale','off');
[xfinal fval] = fminunc(@cos,1,opts);

yields the following display:

  


Free Optimization Interactive Kit

Learn how to use optimization to solve systems of equations, fit models to data, or optimize system performance.

Get free kit

Trials Available

Try the latest version of optimization products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS