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.
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
This figure shows a plot of the function.
Find the Minimum of the Function
To find the minimum of
ps_example using the
Optimize Live Editor task, complete the
Create a new live script by clicking the New Live Script button in the File section on the Home tab.
Insert an Optimize Live Editor task. Click the Insert tab and then, in the Code section, select Task > Optimize.
Click the Solver-based task.
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.
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;
To place these variables into the workspace, run the section by pressing Ctrl + Enter.
In the Specify problem type section of the task, click the Objective > Nonsmooth button.
Ensure that the selected solver is
In the Select problem data section of the task, select Objective function > Function handle and then choose
Select Initial point (x0) > x0.
In the Display progress section of the task, select the Best value and Mesh size plots.
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.
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.
The Optimize task puts the variables
objectiveValue in the
workspace. View these values by placing a new section below the task, and include
Run the section by pressing Ctrl+Enter.