This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Output Function Syntax

What Are Output Functions?

The Outputfcn field of options specifies one or more functions that an optimization function calls at each iteration. Typically, you might use an output function to plot points at each iteration or to display optimization quantities from the algorithm. Using an output function you can view, but not set, optimization quantities.


intlinprog output functions and plot functions differ from those in other solvers. See intlinprog Output Function and Plot Function Syntax.

To set up an output function, do the following:

  1. Write the output function as a function file or local function.

  2. Use optimoptions to set the value of Outputfcn to be a function handle, that is, the name of the function preceded by the @ sign. For example, if the output function is outfun.m, the command

     options = optimoptions(@solvername,'OutputFcn', @outfun);

    specifies OutputFcn to be the handle to outfun. To specify more than one output function, use the syntax

     options = optimoptions(@solvername,'OutputFcn',{@outfun, @outfun2});
  3. Call the optimization function with options as an input argument.

See Output Functions for an example of an output function.

Passing Extra Parameters explains how to parametrize the output function OutputFcn, if necessary.

Structure of the Output Function

The function definition line of the output function has the following form:

stop = outfun(x,optimValues,state)


  • x is the point computed by the algorithm at the current iteration.

  • optimValues is a structure containing data from the current iteration. Fields in optimValues describes the structure in detail.

  • state is the current state of the algorithm. States of the Algorithm lists the possible values.

  • stop is a flag that is true or false depending on whether the optimization routine should quit or continue. See Stop Flag for more information.

The optimization function passes the values of the input arguments to outfun at each iteration.

Fields in optimValues

The following table lists the fields of the optimValues structure. A particular optimization function returns values for only some of these fields. For each field, the Returned by Functions column of the table lists the functions that return the field.

Corresponding Output Arguments

Some of the fields of optimValues correspond to output arguments of the optimization function. After the final iteration of the optimization algorithm, the value of such a field equals the corresponding output argument. For example, optimValues.fval corresponds to the output argument fval. So, if you call fmincon with an output function and return fval, the final value of optimValues.fval equals fval. The Description column of the following table indicates the fields that have a corresponding output argument.

Command-Line Display

The values of some fields of optimValues are displayed at the command line when you call the optimization function with the Display field of options set to 'iter', as described in Iterative Display. For example, optimValues.fval is displayed in the f(x) column. The Command-Line Display column of the following table indicates the fields that you can display at the command line.

Some optimValues fields apply only to specific algorithms:

  • AS — active-set

  • D — trust-region-dogleg

  • IP — interior-point

  • LM — levenberg-marquardt

  • Q — quasi-newton

  • SQP — sqp

  • TR — trust-region

  • TRR — trust-region-reflective

Some optimValues fields exist in certain solvers or algorithms, but are always filled with empty or zero values, so are meaningless. These fields include:

  • constrviolation for fminunc TR and fsolve TRR.

  • procedure for fmincon TRR and SQP, and for fminunc.

optimValues Fields

OptimValues Field (optimValues.field)DescriptionReturned by FunctionsCommand-Line Display


Attainment factor for multiobjective problem. For details, see Goal Attainment Method.




Number of conjugate gradient iterations at current optimization iteration.

fmincon (IP, TRR), fminunc (TR), fsolve (TRR), lsqcurvefit (TRR), lsqnonlin (TRR)


See Iterative Display.


Maximum constraint violation.

fgoalattain, fmincon, fminimax, fseminf

fminunc TR and fsolve TRR provide blank field values.

Max constraint or Feasibility

See Iterative Display.


Measure of degeneracy. A point is degenerate if

The partial derivative with respect to one of the variables is 0 at the point.

A bound constraint is active for that variable at the point.

See Degeneracy.

fmincon (TRR), lsqcurvefit (TRR), lsqnonlin (TRR)



Directional derivative in the search direction.

fgoalattain, fmincon (AS), fminimax, fminunc (Q), fseminf, fsolve (LM), lsqcurvefit (LM), lsqnonlin (LM)

Directional derivative

See Iterative Display.


First-order optimality (depends on algorithm). Final value equals optimization function output output.firstorderopt.

fgoalattain, fmincon, fminimax, fminunc, fseminf, fsolve, lsqcurvefit, lsqnonlin

First-order optimality

See Iterative Display.


Cumulative number of function evaluations. Final value equals optimization function output output.funcCount.

fgoalattain, fminbnd, fmincon, fminimax, fminsearch, fminunc, fsolve, fzero, fseminf, lsqcurvefit, lsqnonlin

F-count or Func-count

See Iterative Display.


Function value at current point. Final value equals optimization function output fval.

For fsolve, fval is the vector function value, and iterative display f(x) is the squared norm of this vector.

fgoalattain, fminbnd, fmincon, fminimax, fminsearch, fminunc, fseminf, fsolve, fzero


See Iterative Display.


Current gradient of objective function — either analytic gradient if you provide it or finite-differencing approximation. Final value equals optimization function output grad.

fgoalattain, fmincon, fminimax, fminunc, fseminf, fsolve, lsqcurvefit, lsqnonlin



Iteration number — starts at 0. Final value equals optimization function output output.iterations.

fgoalattain, fminbnd, fmincon, fminimax, fminsearch, fminunc, fsolve, fseminf, fzero, lsqcurvefit, lsqnonlin


See Iterative Display.


The Levenberg-Marquardt parameter, lambda, at the current iteration. See Levenberg-Marquardt Method.

fsolve (LM), lsqcurvefit (LM), lsqnonlin (LM)



Actual step length divided by initially predicted step length

fmincon (AS, SQP), fminunc (Q)

Steplength or Line search steplength or Step-size

See Iterative Display.


Maximum function value




0 if algorithm detects negative curvature while computing Newton step.

1 otherwise.

fmincon (TRR), fminunc (TR), fsolve (TRR), lsqcurvefit (TRR), lsqnonlin (TRR)



Procedure messages.

fgoalattain, fminbnd, fmincon (AS), fminimax, fminsearch, fseminf, fzero

fmincon TRR and SQP, and fminunc provide blank field values.


See Iterative Display.


Ratio of change in the objective function to change in the quadratic approximation.

fmincon (TRR), fminunc (TR), fsolve (TRR), lsqcurvefit (TRR), lsqnonlin (TRR)



The residual vector.

lsqcurvefit, lsqnonlin,


See Iterative Display.


2-norm of the residual squared.

lsqcurvefit, lsqnonlin


See Iterative Display.


Search direction.

fgoalattain, fmincon (AS, SQP), fminimax, fminunc (Q), fseminf, fsolve (LM), lsqcurvefit (LM), lsqnonlin (LM)



Status of the current trust-region step. Returns true if the current trust-region step was successful, and false if the trust-region step was unsuccessful.

fsolve (D)



Current step size (displacement in x). Final value equals optimization function output output.stepsize.

fgoalattain, fmincon, fminimax, fminunc, fseminf, fsolve, lsqcurvefit, lsqnonlin

Step-size or Norm of Step

See Iterative Display.


Radius of trust region.

fmincon (IP, TRR), fminunc (TR), fsolve (D, TRR), lsqcurvefit (TRR), lsqnonlin (TRR)

Trust-region radius

See Iterative Display.


The value of the field degenerate, which measures the degeneracy of the current optimization point x, is defined as follows. First, define a vector r, of the same size as x, for which r(i) is the minimum distance from x(i) to the ith entries of the lower and upper bounds, lb and ub. That is,

r = min(abs(ub-x, x-lb))

Then the value of degenerate is the minimum entry of the vector r + abs(grad), where grad is the gradient of the objective function. The value of degenerate is 0 if there is an index i for which both of the following are true:

  • grad(i) = 0

  • x(i) equals the ith entry of either the lower or upper bound.

States of the Algorithm

The following table lists the possible values for state:



The algorithm is in the initial state before the first iteration.


The algorithm is in some computationally expensive part of the iteration. In this state, the output function can interrupt the current iteration of the optimization. At this time, the values of x and optimValues are the same as at the last call to the output function in which state=='iter'.


The algorithm is at the end of an iteration.


The algorithm is in the final state after the last iteration.

The 'interrupt' state occurs only in the fmincon 'active-set' algorithm and the fgoalattain, fminimax, and fseminf solvers. There, the state can occur before a quadratic programming subproblem solution or a line search.

The following code illustrates how the output function might use the value of state to decide which tasks to perform at the current iteration:

switch state
    case 'iter'
          % Make updates to plot or guis as needed
    case 'interrupt'
          % Probably no action here. Check conditions to see  
          % whether optimization should quit.
    case 'init'
          % Setup for plots or guis
    case 'done'
          % Cleanup of plots, guis, or final plot

Stop Flag

The output argument stop is a flag that is true or false. The flag tells the optimization function whether the optimization should quit or continue. The following examples show typical ways to use the stop flag.

Stopping an Optimization Based on Data in optimValues

The output function can stop an optimization at any iteration based on the current data in optimValues. For example, the following code sets stop to true if the directional derivative is less than .01:

function stop = outfun(x,optimValues,state)
stop = false;
% Check if directional derivative is less than .01.
if optimValues.directionalderivative < .01
    stop = true;

Stopping an Optimization Based on GUI Input

If you design a GUI to perform optimizations, you can make the output function stop an optimization when a user clicks a Stop button on the GUI. The following code shows how to do this, assuming that the Stop button callback stores the value true in the optimstop field of a handles structure called hObject:

function stop = outfun(x,optimValues,state)
stop = false;
% Check if user has requested to stop the optimization.
stop = getappdata(hObject,'optimstop');