Skip to Main Content Skip to Search
Product Documentation

When the Solver Fails

Too Many Iterations or Function Evaluations

The solver stopped because it reached a limit on the number of iterations or function evaluations before it minimized the objective to the requested tolerance. To proceed, try one or more of the following.

1. Enable Iterative Display
2. Relax Tolerances
3. Start the Solver From Different Points
4. Check Objective and Constraint Function Definitions
5. Center and Scale Your Problem
6. Provide Gradient or Jacobian
7. Provide Hessian

1. Enable Iterative Display

Set the Display option to 'iter'. This setting shows the results of the solver iterations.

To enable iterative display:

For an example of iterative display, see Interpreting the Result.

What to Look For in Iterative Display.  

What to Do.  

2. Relax Tolerances

If TolX or TolFun, for example, are too small, the solver might not recognize when it has reached a minimum; it can make futile iterations indefinitely.

To change tolerances using the Optimization Tool, use the Stopping criteria list at the top of the Options pane.

To change tolerances at the command line, use optimset as described in Setting Options.

The DiffMaxChange and DiffMinChange options can affect a solver's progress. These options control the step size in finite differencing for derivative estimation.

3. Start the Solver From Different Points

See Change the Initial Point.

4. Check Objective and Constraint Function Definitions

For example, check that your objective and nonlinear constraint functions return the correct values at some points. See Check your Objective and Constraint Functions. Check that an infeasible point does not cause an error in your functions; see Iterations Can Violate Constraints.

5. Center and Scale Your Problem

Solvers run more reliably when each coordinate has about the same effect on the objective and constraint functions. Multiply your coordinate directions with appropriate scalars to equalize the effect of each coordinate. Add appropriate values to certain coordinates to equalize their size.

Example: Centering and Scaling.  Consider minimizing 1e6*x(1)^2 + 1e-6*x(2)^2:

f = @(x) 10^6*x(1)^2 + 10^-6*x(2)^2;

Minimize f using the medium-scale fminunc algorithm:

opts = optimset('Display','none','LargeScale','off');
x = fminunc(f,[0.5;0.5],opts)

x =
         0
    0.5000

The result is incorrect; poor scaling interfered with obtaining a good solution.

Scale the problem. Set

D = diag([1e-3,1e3]);
fr = @(y) f(D*y);
y = fminunc(fr, [0.5;0.5], opts)

y =
     0
     0 % the correct answer

Similarly, poor centering can interfere with a solution.

fc = @(z)fr([z(1)-1e6 z(2)+1e6]); % poor centering
z = fminunc(fc,[.5 .5],opts)

z =
  1.0e+005 *
   10.0000  -10.0000 % looks good, but...

z - 1e6 % checking how close z is to 1e6

ans =
  1.0e+006 *
   -0.0000   -2.0000 % reveals quite a distance

fcc = @(w)fc([w(1)+1e6,w(2)-1e6]); % centered

w = fminunc(fcc,[.5 .5],opts)

w =
     0     0 % the correct answer

6. Provide Gradient or Jacobian

If you do not provide gradients or Jacobians, solvers estimate gradients and Jacobians by finite differences. Therefore, providing these derivatives can save computational time, and can lead to increased accuracy.

For constrained problems, providing a gradient has another advantage. A solver can reach a point x such that x is feasible, but finite differences around x always lead to an infeasible point. In this case, a solver can fail or halt prematurely. Providing a gradient allows a solver to proceed.

Provide gradients or Jacobians in the files for your objective function and nonlinear constraint functions. For details of the syntax, see Writing Scalar Objective Functions, Writing Vector and Matrix Objective Functions, and Nonlinear Constraints.

To check that your gradient or Jacobian function is correct, use the DerivativeCheck option, as described in Checking Validity of Gradients or Jacobians.

If you have a Symbolic Math Toolbox license, you can calculate gradients and Hessians programmatically. For an example, see Example: Using Symbolic Math Toolbox Functions to Calculate Gradients and Hessians.

For examples using gradients and Jacobians, see Example: Nonlinear Minimization with Gradient and Hessian, Example: Constraints With Gradients, Example: Using Symbolic Math Toolbox Functions to Calculate Gradients and Hessians, Example: Nonlinear Equations with Analytic Jacobian, and Example: Nonlinear Equations with Jacobian.

7. Provide Hessian

Solvers often run more reliably and with fewer iterations when you supply a Hessian.

The following solvers and algorithms accept Hessians:

If you have a Symbolic Math Toolbox license, you can calculate gradients and Hessians programmatically. For an example, see Example: Using Symbolic Math Toolbox Functions to Calculate Gradients and Hessians.

No Feasible Point

The solver was unable to find a point satisfying all constraints to within the TolCon constraint tolerance. To proceed, try one or more of the following.

1. Check Linear Constraints
2. Check Nonlinear Constraints

1. Check Linear Constraints

Try finding a point that satisfies the bounds and linear constraints by solving a linear programming problem.

  1. Define a linear programming problem with an objective function that is always zero:

    f = zeros(size(x0)); % assumes x0 is the initial point
  2. Solve the linear programming problem to see if there is a feasible point:

    xnew = linprog(f,A,b,Aeq,beq,lb,ub);
  3. If there is a feasible point xnew, use xnew as the initial point and rerun your original problem.

  4. If there is no feasible point, your problem is not well-formulated. Check the definitions of your bounds and linear constraints.

2. Check Nonlinear Constraints

After ensuring that your bounds and linear constraints are feasible (contain a point satisfying all constraints), check your nonlinear constraints.

Try relaxing your nonlinear inequality constraints, then tightening them.

  1. Change the nonlinear constraint function c to return c-Δ, where Δ is a positive number. This change makes your nonlinear constraints easier to satisfy.

  2. Look for a feasible point for the new constraint function, using either your original objective function or the zero objective function.

    1. If you find a feasible point,

      1. Reduce Δ

      2. Look for a feasible point for the new constraint function, starting at the previously found point.

    2. If you do not find a feasible point, try increasing Δ and looking again.

If you find no feasible point, your problem might be truly infeasible, meaning that no solution exists. Check all your constraint definitions again.

Problem Unbounded

The solver reached a point whose objective function was less than the objective limit tolerance.

fsolve Could Not Solve Equation

fsolve can fail to solve an equation for various reasons. Here are some suggestions for how to proceed:

  1. Try Changing the Initial Point. fsolve relies on an initial point. By giving it different initial points, you increase the chances of success.

  2. Check the definition of the equation to make sure that it is smooth. fsolve might fail to converge for equations with discontinuous gradients, such as absolute value. fsolve can fail to converge for functions with discontinuities.

  3. Check that the equation is "square," meaning equal dimensions for input and output (has the same number of unknowns as values of the equation).

  4. Change tolerances, especially TolFun and TolX. If you attempt to get high accuracy by setting tolerances to very small values, fsolve can fail to converge. If you set tolerances that are too high, fsolve can fail to solve an equation accurately.

  5. Check the problem definition. Some problems have no real solution, such as x^2 + 1 = 0.

Solver Takes Too Long

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

1. Enable Iterative Display

Set the Display option to 'iter'. This setting shows the results of the solver iterations.

To enable iterative display:

For an example of iterative display, see Interpreting the Result. For more information, see What to Look For in Iterative Display.

2. Enable FunValCheck

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.

3. Use Appropriate Tolerances

Solvers can fail to converge if tolerances are too small, especially TolFun and TolX.

To change tolerances using the Optimization Tool, use the Stopping criteria list at the top of the Options pane.

To change tolerances at the command line, use optimset as described in Setting Options.

4. 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 Tool. The Options section of your solver's function reference pages also lists the plot functions.

To use a plot function:

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

5. Enable DerivativeCheck

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.

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

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

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

8. 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 for Optimization.

  


Free Optimization Interactive Kit

Learn how to use optimization to solve systems of equations, fit models to data, or optimize system performance.

Get free kit

Trials Available

Try the latest version of optimization products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS