Solvers can take excessive time for various reasons. To diagnose the reason or enable faster solution, 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 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 Interpret Result. For more information, see What to Look For in Iterative Display.
Solvers can fail to converge if tolerances are too small, especially OptimalityTolerance
and StepTolerance
.
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. The Options section of your solver's function reference pages lists the plot functions.
To use a plot function at the MATLAB command line, enter
options = optimoptions('solvername','PlotFcn',{@plotfcn1,@plotfcn2,...});
Call the solver using the options
structure.
For an example of using a plot function, see Use a Plot Function.
'lbfgs' HessianApproximation
OptionFor the fmincon
solver, if you have a problem with many
variables (hundreds or more), then oftentimes you can save time and memory by
setting the HessianApproximation
option to
'lbfgs'
. This causes the fmincon
'interior-point'
algorithm to use a low-memory Hessian
approximation.
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 CheckGradients
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 and Plot Function Syntax.
For an example of using an output function, see Output Functions for Optimization Toolbox™.
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.
Tip
If you use a large-scale algorithm, then 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.