Main Content

Minimize Function with Many Local Minima

De Jong's Fifth Function

This example shows how to find a local minimum of a function using simulated annealing. The example presents two approaches for minimizing: working at the command line and using the Optimize Live Editor task.

De Jong's fifth function is a two-dimensional function with many (25) local minima. In the following plot, it is unclear which of these local minima is the global minimum.


Figure contains an axes object. The axes object contains 2 objects of type surface, contour.

Many standard optimization algorithms become stuck in local minima. Because the simulated annealing algorithm performs a wide random search, the chance of being trapped in a local minimum is decreased.

Note:Because simulated annealing uses random number generators, each time you run this algorithm you can get different results. See Reproduce Your Results for more information.

Minimize at the Command Line

To run the simulated annealing algorithm without constraints, call simulannealbnd at the command line using the objective function in dejong5fcn.m, referenced by the anonymous function @dejong5fcn in the following code.

rng(10,'twister') % for reproducibility
fun = @dejong5fcn;
[x,fval] = simulannealbnd(fun,[0 0])
Optimization terminated: change in best function value less than options.FunctionTolerance.

x =
  -16.1292  -15.8214

fval =

In the results:

  • x is the final point returned by the algorithm.

  • fval is the objective function value at the final point.

Minimize Using the Optimize Live Editor Task

You can also run the minimization using the Optimize Live Editor task, which provides a visual approach.

  1. Create a new live script by clicking the New Live Script button in the File section on the Home tab.

    New Live Script button

  2. Insert an Optimize Live Editor task. Click the Insert tab and then, in the Code section, select Task > Optimize.

    Inserting Optimize Live Editor task

    Optimize task in Live Editor: Choose between problem-based (recommended) and solver-based

  3. Click the Solver-based task.

    Optimize Live Editor task

  4. For use in entering problem data, insert a new section by clicking the Section Break button on the Insert tab. New sections appear above and below the task.

  5. In the new section above the task, enter the following code to define the initial point and the objective function.

    x0 = [0 0];
    fun = @dejong5fcn;
  6. To place these variables into the workspace, run the section by pressing Ctrl+Enter.

  7. In the Specify problem type section of the task, click the Objective > Nonlinear button.

  8. Select Solver > simulannealbnd - Simulated annealing algorithm.

  9. In the Select problem data section of the task, select Objective function > Function handle and then choose fun.

  10. Select Initial point (x0) > x0.

    "Solver, objective function, and initial point specified

  11. In the Display progress section of the task, select the Best value plot.

  12. To run the solver, click the options button at the top right of the task window, and select Run Section. The plot appears in a separate figure window and in the task output area. Note that your plot might be different from the one shown, because simulannealbnd is a stochastic algorithm.

    Plot showing best function value decreasing to 10.7632 in about 1300 iterations

  13. To see the solution and best objective function value, look at the top of the task.

    Optimize returns solution and objectiveValue variables

    The Optimize Live Editor task returns variables named solution and objectiveValue to the workspace.

  14. To view the values these variables, enter the following code in the section below the task.

  15. Run the section by pressing Ctrl+Enter.

      -32.0285   -0.1280

Both the Optimize Live Editor task and the command line allow you to formulate and solve problems, and they give identical results. The command line is more streamlined, but provides less help for choosing a solver, setting up the problem, and choosing options such as plot functions. You can also start a problem using Optimize, and then generate code for command line use, as in Solve a Constrained Nonlinear Problem, Solver-Based.

See Also

Related Topics