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
command.
type ps_example
This figure shows a plot of the function.
To find the minimum of ps_example
using the
Optimize Live Editor task, complete the
following steps.
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.
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
patternsearch
.
In the Select problem data section of the task,
select Objective function > Function handle and then
choose fun
.
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
solution
and objectiveValue
in the
workspace. View these values by placing a new section below the task, and include
this code.
disp(solution) disp(objectiveValue)
Run the section by pressing Ctrl+Enter.
disp(solution)
-4.7124 -0.0000
disp(objectiveValue)
-2.0000