## Set Mesh Options

### Mesh Expansion and Contraction

The `MeshExpansionFactor` and `MeshContractionFactor` options control how much the mesh size is expanded or contracted at each iteration. With the default `MeshExpansionFactor` value of `2`, the pattern search multiplies the mesh size by `2` after each successful poll. With the default `MeshContractionFactor` value of `0.5`, the pattern search multiplies the mesh size by `0.5` after each unsuccessful poll.

You can view the expansion and contraction of the mesh size during the pattern search by setting `@psplotmeshsize` as the `PlotFcn` option. To also display the values of the mesh size and objective function at the command line, set the `Display` option to `'iter'`.

For example, set up the problem described in Constrained Minimization Using patternsearch and Optimize Live Editor Task as follows:

1. Enter the following at the command line:

```x0 = [2 1 0 9 1 0]'; Aineq = [-8 7 3 -4 9 0]; bineq = 7; Aeq = [7 1 8 3 3 3; 5 0 -5 1 -5 8; -2 -6 7 1 1 9; 1 -1 2 -2 3 -3]; beq = [84 62 65 1]; H = [36 17 19 12 8 15; 17 33 18 11 7 14; 19 18 43 13 8 16; 12 11 13 18 6 11; 8 7 8 6 9 8; 15 14 16 11 8 29]; f = [ 20 15 21 18 29 24 ]'; F = @(x)0.5*x'*H*x + f'*x;```
2. Create options to use the `GSSPositiveBasis2N` poll method, give iterative display, and plot the mesh size.

```options = optimoptions('patternsearch',... 'PollMethod','GSSPositiveBasis2N',... 'PlotFcn',@psplotmeshsize,... 'Display','iter');```
3. Run the optimization.

```[x,fval,exitflag,output] = patternsearch(F,x0,... Aineq,bineq,Aeq,beq,[],[],[],options);```

To see the changes in mesh size more clearly, change the y-axis to logarithmic scaling as follows:

1. Select Axes Properties from the Edit menu in the plot window.

2. In the Properties Editor, select the Rulers tab.

3. Set YScale to Log.

Updating these settings in the MATLAB® Property Editor shows the plot in the following figure.

The first 5 iterations result in successful polls, so the mesh sizes increase steadily during this time. You can see that the first unsuccessful poll occurs at iteration 6 by looking at the command-line display.

```Iter f-count f(x) MeshSize Method 0 1 2273.76 1 1 2 2251.69 2 Successful Poll 2 3 2209.86 4 Successful Poll 3 4 2135.43 8 Successful Poll 4 5 2023.48 16 Successful Poll 5 6 1947.23 32 Successful Poll 6 15 1947.23 16 Refine Mesh```

Note that at iteration 5, which is successful, the mesh size doubles for the next iteration. But at iteration 6, which is unsuccessful, the mesh size is multiplied `0.5`.

To see how `MeshExpansionFactor` and `MeshContractionFactor` affect the pattern search, set `MeshExpansionFactor` to `3.0` and set `MeshContractionFactor` to `2/3`.

```options = optimoptions(options,'MeshExpansionFactor',3.0,... 'MeshContractionFactor',2/3); [x,fval,exitflag,output] = patternsearch(F,x0,... Aineq,bineq,Aeq,beq,[],[],[],options);```

The final objective function value is approximately the same as with the previous settings, but the solver takes longer to reach that point.

When you change the scaling of the y-axis to logarithmic, the mesh size plot appears as shown in the following figure.

Note that the mesh size increases faster with `MeshExpansionFactor` set to `3.0`, as compared with the default value of `2.0`, and decreases more slowly with `MeshContractionFactor` set to `2/3`, as compared with the default value of `0.5`.

### Mesh Accelerator

The mesh accelerator can make a pattern search converge faster to an optimal point by reducing the number of iterations required to reach the mesh tolerance. When the mesh size is below a certain value, the pattern search contracts the mesh size by a factor smaller than the `MeshContractionFactor` factor. Mesh accelerator applies only to the GPS and GSS algorithms.

Note

For best results, use the mesh accelerator for problems in which the objective function is not too steep near the optimal point, or you might lose some accuracy. For differentiable problems, this means that the absolute value of the derivative is not too large near the solution.

To use the mesh accelerator, set the `AccelerateMesh` option to `true`.

For example, set up the problem described in Constrained Minimization Using patternsearch and Optimize Live Editor Task as follows:

1. Enter the following at the command line:

```x0 = [2 1 0 9 1 0]; Aineq = [-8 7 3 -4 9 0]; bineq = 7; Aeq = [7 1 8 3 3 3; 5 0 -5 1 -5 8; -2 -6 7 1 1 9; 1 -1 2 -2 3 -3]; beq = [84 62 65 1]; H = [36 17 19 12 8 15; 17 33 18 11 7 14; 19 18 43 13 8 16; 12 11 13 18 6 11; 8 7 8 6 9 8; 15 14 16 11 8 29]; f = [ 20 15 21 18 29 24 ]'; F = @(x)0.5*x'*H*x + f'*x;```
2. Create options, including the mesh accelerator.

```options = optimoptions('patternsearch',... 'PollMethod','GSSPositiveBasis2N',... 'Display','iter','AccelerateMesh',true);```
3. Run the optimization.

```[x,fval,exitflag,output] = patternsearch(F,x0,... Aineq,bineq,Aeq,beq,[],[],[],options);```

`patternsearch` completes in 78 iterations, compared to 84 iterations when the mesh accelerator is not on. You can see the effect of the mesh accelerator in the iterative display. Run the example with and without mesh acceleration. The mesh sizes are the same until iteration 70, but differ at iteration 71. The MATLAB Command Window displays the following lines for iterations 70 and 71 without the accelerator.

```Iter f-count f(x) MeshSize Method 70 618 1919.54 6.104e-05 Refine Mesh 71 630 1919.54 3.052e-05 Refine Mesh```

Note that the mesh size is multiplied by `0.5`, the default value of `MeshContractionFactor`.

For comparison, the Command Window displays the following lines for the same iteration numbers with the accelerator.

```Iter f-count f(x) MeshSize Method 70 618 1919.54 6.104e-05 Refine Mesh 71 630 1919.54 1.526e-05 Refine Mesh```

In this case the mesh size is multiplied by `0.25`.