优化Using the GPS Algorithm
This example shows how to solve an optimization problem using the GPS algorithm, which is the default for thepatternsearch
solver. The example uses the Optimize Live Editor task to complete the optimization using a visual approach.
目标函数
This example uses the objective functionps_example.
, which is included withGlobal Optimization Toolboxsoftware. View the code for the function by entering the following command.
typeps_example.
该图显示了该功能的曲线图。
找到函数的最小值
To find the minimum ofps_example.
using the优化Live Editor task, complete the following steps.
Create a new live script by clicking theNew Live Scriptbutton in the文件section on theHometab.
Insert an Optimize Live Editor task. Click the Insert tab and then, in the Code section, select Task > Optimize.
Click theSolver-based任务。
有关输入问题数据,请通过单击插入新部分Section Breakbutton on theInserttab. New sections appear above and below the task.
在里面new section above the task, enter the following code to define the initial point and objective function.
x0 = [2.1 1.7];有趣= @ps_example;
要将这些变量放入工作区,请按按键运行该部分Ctrl + Enter..
在里面指定问题类型section of the task, click theObjective > Nonsmoothbutton.
Ensure that the selected solver is
patternsearch
.在里面Select problem datasection of the task, selectObjective function > Function handle和then choose
fun
.Select初始点(x0)> x0.
在任务的显示进度部分中,选择最佳值和网格大小图。
To run the solver, click the options button⁝at the top right of the task window, and selectRun 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, seeHow Pattern Search Polling Works.
优化了因为e the mesh size became smaller than the mesh size tolerance value, defined by theMeshTolerance
选项。最小函数值约为-2。
To see the solution and objective function value, look at the top of the task.
The优化task puts the variablessolution
和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 pressingCtrl+Enter.
disp(solution)
-4.7124 -0.0000
disp(objectiveValue)
-2.0000