Main Content

Set Optimization Options

How to Set Options

You can specify optimization parameters using an options structure that you create using the optimset function. You then pass options as an input to the optimization function, for example, by calling fminbnd with the syntax

x = fminbnd(fun,x1,x2,options)

or fminsearch with the syntax

x = fminsearch(fun,x0,options)

For example, to display output from the algorithm at each iteration, set the Display option to 'iter':

options = optimset('Display','iter');

Options Table

OptionDescriptionSolvers

Display

A flag indicating whether intermediate steps appear on the screen.

  • 'notify' (default) displays output only if the function does not converge.

  • 'iter' displays intermediate steps (not available with lsqnonneg). See Optimization Solver Iterative Display.

  • 'off' or 'none' displays no intermediate steps.

  • 'final' displays just the final output.

fminbnd, fminsearch, fzero, lsqnonneg

FunValCheck

Check whether objective function values are valid.

  • 'on' displays an error when the objective function or constraints return a value that is complex or NaN.

  • 'off' (default) displays no error.

fminbnd, fminsearch, fzero

MaxFunEvals

The maximum number of function evaluations allowed. The default value is 500 for fminbnd and 200*length(x0) for fminsearch.

fminbnd, fminsearch

MaxIter

The maximum number of iterations allowed. The default value is 500 for fminbnd and 200*length(x0) for fminsearch.

fminbnd, fminsearch

OutputFcn

Display information on the iterations of the solver. The default is [] (none). See Optimization Solver Output Functions.

fminbnd, fminsearch, fzero

PlotFcns

Plot information on the iterations of the solver. The default is [] (none). For available predefined functions, see Optimization Solver Plot Functions.

fminbnd, fminsearch, fzero

TolFun

The termination tolerance for the function value. The default value is 1.e-4. See Tolerances and Stopping Criteria.

fminsearch

TolX

The termination tolerance for x. The default value is 1.e-4, except for fzero, which has a default value of eps (= 2.2204e-16), and lsqnonneg, which has a default value of 10*eps*norm(c,1)*length(c). See Tolerances and Stopping Criteria.

fminbnd, fminsearch, fzero, lsqnonneg

Tolerances and Stopping Criteria

The number of iterations in an optimization depends on the stopping criteria for the solver. These criteria include several tolerances you can set. Generally, a tolerance is a threshold which, if crossed, stops the iterations of a solver.

Tip

Generally, set the TolFun and TolX tolerances to well above eps, and usually above 1e-14. Setting small tolerances does not guarantee accurate results. Instead, a solver can fail to recognize when it has converged, and can continue futile iterations. A tolerance value smaller than eps effectively disables that stopping condition. This tip does not apply to fzero, which uses a default value of eps for TolX.

  • TolX is a lower bound on the size of a step, meaning the norm of (xi – xi+1). If the solver attempts to take a step that is smaller than TolX, the iterations end. Solvers generally use TolX as a relative bound, meaning iterations end when |(xi – xi+1)| < TolX*(1 + |xi|), or a similar relative measure.

    Plot showing how iterations end when the last step is smaller than TolFun or TolX.

  • TolFun is a lower bound on the change in the value of the objective function during a step. If |f(xi) – f(xi+1)| < TolFun, the iterations end. Solvers generally use TolFun as a relative bound, meaning iterations end when |f(xi) – f(xi+1)| < TolFun(1 + |f(xi)|), or a similar relative measure.

  • MaxIter is a bound on the number of solver iterations. MaxFunEvals is a bound on the number of function evaluations.

Note

Unlike other solvers, fminsearch stops when it satisfies both TolFun and TolX.

Output Structure

The output structure includes the number of function evaluations, the number of iterations, and the algorithm. The structure appears when you provide fminbnd, fminsearch, or fzero with a fourth output argument, as in

[x,fval,exitflag,output] = fminbnd(@humps,0.3,1);

The details of the output structure for each solver are on the function reference pages.

The output structure is not an option that you choose with optimset. It is an optional output for fminbnd, fminsearch, and fzero.

Related Topics