## 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.

### 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 button in the File section on the Home tab.

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

4. For use in entering problem data, insert a new section by clicking the 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 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.

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.

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`