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:

  • 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 Interpret the 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 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.

Use a Plot Function

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','PlotFcn',{@plotfcn1,@plotfcn2,...});

    Call the solver using the options structure.

For an example of using a plot function, see Using a Plot Function.

Use 'lbfgs' HessianApproximation Option

For 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.

Enable CheckGradients

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.

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 Syntax.

For an example of using an output function, see Example: Using Output Functions.

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.