Main Content

Solver Takes Too Long

Solvers can take excessive time for various reasons. To diagnose the reason or enable faster solution, use one or more of the following techniques.

Enable Iterative Display

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.

Use Appropriate Tolerances

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 Optimization Options.

Use a Plot Function

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 Predefined Plot Functions.

Use 'lbfgs' HessianApproximation Option

For the fmincon and fminunc solvers, 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 and fminunc 'quasi-newton' algorithm to use a low-memory Hessian approximation. See Solve Nonlinear Problem with Many Variables.

Validate Derivatives Using checkGradients

If you supply derivatives (gradients or Jacobians) to your solver, the solver might fail to converge if the derivatives are inaccurate. For information about using the checkGradients function, see Checking Validity of Gradients or Jacobians.

Use Inf Instead of a Large, Arbitrary Bound

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.

Use an Output Function

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.

Try Different Algorithm Options

Many solvers have options that can change the solution time, but not in easily predictable ways. Typically, the Algorithm option has a significant effect on the solution time.

Other options that affect the solution time include:

  • fmincon 'interior-point' algorithm — Try setting the BarrierParamUpdate option to 'predictor-corrector'.

  • 'SubproblemAlgorithm' option of the 'trust-region' or 'trust-region-reflective' algorithm — Try setting 'SubproblemAlgorithm' to 'factorization' instead of the default 'cg'.

  • coneprog — For a large sparse problem, try setting the LinearSolver option to 'prodchol', 'schur', or 'normal'. For a dense problem, try setting the LinearSolver option to 'augmented'.

  • quadprog 'interior-point-convex' algorithm or lsqlin 'interior-point' algorithm — Try setting the LinearSolver option to 'sparse' or 'dense'.

Use a Sparse Solver or a Multiply Function

Large problems can cause MATLAB to run out of memory or time. Here are some suggestions for using less memory:

Use Parallel Computing

If you have a Parallel Computing Toolbox™ license, your solver might run faster using parallel computing. For more information, see Parallel Computing.