Main Content

Optimization Troubleshooting and Tips

This table describes typical optimization problems and provides recommendations for handling them.

Problem

Recommendation

The solution found by fminbnd or fminsearch is not a global minimum. A global minimum has the smallest objective function value among all points in the search space.

There is no guarantee that a solution is a global minimum unless your problem is continuous and has only one minimum. To search for a global minimum, start the optimization from multiple starting points (or intervals, in the case of fminbnd).

It is impossible to evaluate the objective function f(x) at some points x. Such points are called infeasible.

Modify your function to return a large positive value for f(x) at infeasible points x.

The minimization routine appears to enter an infinite loop or returns a solution that is not a minimum (or not a zero, in the case of fzero).

Perhaps your objective function returns NaN or complex values. Solvers expect only real objective function values. Any other values can cause unexpected results. To determine whether there are NaN or complex values, set

options = optimset('FunValCheck','on')

and call the optimization function with options as an input argument. If an objective function returns a NaN or complex value, this setting causes the solver to throw an error.

The solver takes a long time.

Most optimization problems benefit from good starting points. Try random starting points in a region that might be close to a solution, based on your problem characteristics.

Sometimes you can solve a complicated problem using an evolutionary approach. First, solve problems with a smaller number of independent variables. Use solutions from these simpler problems as starting points for more complicated problems by using an appropriate mapping. Also, you can sometimes speed the solution by using simpler objective functions and less stringent stopping conditions in the initial stages of an optimization problem.

It is unclear what the solver is doing.

To see what the solver is doing as it iterates:

fminsearch fails to reach a solution.

fminsearch can fail to reach a solution for various reasons.

  • Poor scaling. If your problem is not adequately centered and scaled, the solver can fail to converge correctly. Try to have each coordinate give roughly the same effect on the objective function, and ensure that the scale of each coordinate near a possible solution is not too large or small. To do so, edit the objective function and add or multiply appropriate constants for each coordinate.

  • Inappropriate stopping criteria. If the value you specify for TolFun or TolX is too small, fminsearch can fail to realize when it has reached a solution. If the value is too large, fminsearch can halt far from a solution.

  • Poor initial point. Try starting fminsearch from various initial points.

  • Insufficient iterations. If the solver runs out of iterations or gets stuck, try restarting fminsearch from its final point to reach a better solution. Sometimes fminsearch reaches a better solution when you increase the value of the MaxFunEvals and MaxIter options from their default of 200*length(x0).

Note

Optimization solvers apply to real-valued functions. Complex values cannot be optimized, except for a real-valued function of the complex values, such as the norm.

Related Topics