Solvers can take excessive time for various reasons. To diagnose the reason, use one or more of the following techniques.
Set the Display
option to 'iter'
.
This setting shows the results of the solver iterations.
To enable iterative display:
Using the Optimization app, choose Level
of display to be iterative
or iterative
with detailed message
.
At the MATLAB® command line, enter
options = optimoptions('solvername','Display','iter');
Call the solver using the options
structure.
For an example of iterative display, see Interpreting the Result. For more information, see What to Look For in Iterative Display.
Sometimes a solver fails because an objective function or nonlinear
constraint function returns a complex value, infinity, or NaN. To
halt solver iterations in these cases, enable the FunValCheck
option.
Using the Optimization app, check the box labeled Error if user-supplied function returns Inf, NaN, or complex in the Function value check pane.
At the MATLAB command line, enter
options = optimoptions('solvername','FunValCheck','on');
Call the solver using the options
structure.
Solvers can fail to converge if tolerances are too small, especially TolFun
and TolX
.
To change tolerances using the Optimization app, use the Stopping criteria list at the top of the Options pane.
To change tolerances at the command line, use optimoptions
as
described in Set and Change Options.
You can obtain more visual or detailed information about solver iterations using a plot function. For a list of the predefined plot functions, see Options > Plot functions in the Optimization app. The Options section of your solver's function reference pages also lists the plot functions.
To use a plot function:
Using the Optimization app, check the boxes next to each plot function you wish to use.
At the MATLAB command line, enter
options = optimoptions('solvername','PlotFcns',{@plotfcn1,@plotfcn2,...});
Call the solver using the options
structure.
For an example of using a plot function, see Using a Plot Function.
If you have supplied derivatives (gradients or Jacobians) to
your solver, the solver can fail to converge if the derivatives are
inaccurate. For more information about using the DerivativeCheck
option,
see Checking Validity of Gradients or Jacobians.
If you use a large, arbitrary bound (upper or lower), a solver
can take excessive time, or even fail to converge. However, if you
set Inf
or -Inf
as the bound,
the solver can take less time, and might converge better.
Why? An interior-point algorithm can set an initial point to the midpoint of finite bounds. Or an interior-point algorithm can try to find a "central path" midway between finite bounds. Therefore, a large, arbitrary bound can resize those components inappropriately. In contrast, infinite bounds are ignored for these purposes.
Minor point: Some solvers use memory for each constraint, primarily
via a constraint Hessian. Setting a bound to Inf
or -Inf
means
there is no constraint, so there is less memory in use, because a
constraint Hessian has lower dimension.
You can obtain detailed information about solver iterations using an output function. Solvers call output functions at each iteration. You write output functions using the syntax described in Output Function.
For an example of using an output function, see Example: Using Output Functions.
Large problems can cause MATLAB to run out of memory or time. Here are some suggestions for using less memory:
Use a large-scale algorithm if possible (see Large-Scale vs. Medium-Scale Algorithms).
These algorithms include trust-region-reflective
, interior-point
,
the fminunc
trust-region
algorithm,
the fsolve
trust-region-dogleg
algorithm,
and the Levenberg-Marquardt
algorithm. In contrast,
the active-set
, quasi-newton
,
and sqp
algorithms are not large-scale.
Use sparse matrices for your linear constraints.
Use a Jacobian multiply function or Hessian multiply function. For examples, see Jacobian Multiply Function with Linear Least Squares, Quadratic Minimization with Dense, Structured Hessian, and Minimization with Dense Structured Hessian, Linear Equalities.
If you have a Parallel Computing Toolbox™ license, your solver might run faster using parallel computing. For more information, see Parallel Computing.