Main Content

Optimize Using the GPS Algorithm

This example shows how to solve an optimization problem using the GPS algorithm, which is the default for the patternsearch solver. The example uses the Optimize Live Editor task to complete the optimization using a visual approach.

Objective Function

This example uses the objective function ps_example, which is included with Global Optimization Toolbox software. View the code for the function by entering the following command.

type ps_example

This figure shows a plot of the function.

2-D plot of a nonsmooth function

 Code for creating the figure

Find the Minimum of the Function

To find the minimum of ps_example using the Optimize Live Editor task, complete the following steps.

  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.

    Insert Optimize button

    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 objective function.

    x0 = [2.1 1.7];
    fun = @ps_example;
  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 > Nonsmooth button.

  8. Ensure that the selected solver is patternsearch.

  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.

  11. In the Display progress section of the task, select the Best value and Mesh size plots.

    Solver, objective function, initial point, and plots specified

  12. To run the solver, click the options button at the top right of the task window, and select Run Section. The plots appear in a separate figure window and in the task output area.

    Function values decrease to -2, and the mesh size oscillates but converges to 9.53e-7.

The upper plot shows the objective function value of the best point at each iteration. Typically, the objective function values improve rapidly at the early iterations and then level off as they approach the optimal value.

The lower plot shows the mesh size at each iteration. The mesh size increases after each successful iteration and decreases after each unsuccessful iteration. For details, see How Pattern Search Polling Works.

The optimization stopped because the mesh size became smaller than the mesh size tolerance value, defined by the MeshTolerance option. The minimum function value is approximately –2.

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

Optimize returns solution and objectiveValue variables to the workspace

The Optimize task puts the variables solution and objectiveValue in the workspace. View these values by placing a new section below the task, and include this code.


Run the section by pressing Ctrl+Enter.

   -4.7124   -0.0000

See Also


Related Topics