| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Genetic Algorithm and Direct Search Toolbox |
| Contents | Index |
| Learn more about Genetic Algorithm and Direct Search Toolbox |
| On this page… |
|---|
Calling patternsearch with the Default Options |
This section describes how to perform a pattern search with the default options.
For an unconstrained problem, call patternsearch with the syntax
[x fval] = patternsearch(@objectfun, x0)
The output arguments are
x — The final point
fval — The value of the objective function at x
The required input arguments are
@objectfun — A function handle to the objective function objectfun, which you can write as an M-file. See Writing Files for Functions You Want to Optimize to learn how to do this.
x0 — The initial point for the pattern search algorithm.
As an example, you can run the example described in Example — Finding the Minimum of a Function Using the GPS Algorithm from the command line by entering
[x fval] = patternsearch(@ps_example, [2.1 1.7])
This returns
Optimization terminated: mesh size less than options.TolMesh. x = -4.7124 -0.0000 fval = -2.0000
If your problem has constraints, use the syntax
[x fval] = patternsearch(@objfun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
where
A is a matrix and b is vector that represent inequality constraints of the form A·x ≤ b.
Aeq is a matrix and beq is a vector that represent equality constraints of the form Aeq·x = beq.
lb and ub are vectors representing bound constraints of the form lb ≤ x and x ≤ ub, respectively.
nonlcon is a function that returns the nonlinear equality and inequality vectors, c and ceq, respectively. The function is minimized such that c(x) ≤ 0 and ceq(x) = 0.
You only need to pass in the constraints that are part of the problem. For example, if there are no bound constraints or a nonlinear constraint function, use the syntax
[x fval] = patternsearch(@objfun,x0,A,b,Aeq,beq)
Use empty brackets [] for constraint arguments that are not needed for the problem. For example, if there are no inequality constraints or a nonlinear constraint function, use the syntax
[x fval] = patternsearch(@objfun,x0,[],[],Aeq,beq,lb,ub)
To get more information about the performance of the pattern search, you can call patternsearch with the syntax
[x fval exitflag output] = patternsearch(@objfun,x0)
Besides x and fval, this returns the following additional output arguments:
exitflag — Integer indicating whether the algorithm was successful
output — Structure containing information about the performance of the solver
See the reference page for patternsearch for more information about these arguments.
You can specify any available patternsearch options by passing an options structure as an input argument to patternsearch using the syntax
[x fval] = patternsearch(@fitnessfun,nvars, ...
A,b,Aeq,beq,lb,ub,nonlcon,options)Pass in empty brackets [] for any constraints that do not appear in the problem.
You create the options structure using the function psoptimset.
options = psoptimset(@patternsearch)
This returns the options structure with the default values for its fields.
options =
TolMesh: 1.0000e-006
TolCon: 1.0000e-006
TolX: 1.0000e-006
TolFun: 1.0000e-006
TolBind: 1.0000e-003
MaxIter: '100*numberofvariables'
MaxFunEvals: '2000*numberofvariables'
TimeLimit: Inf
MeshContraction: 0.5000
MeshExpansion: 2
MeshAccelerator: 'off'
MeshRotate: 'on'
InitialMeshSize: 1
ScaleMesh: 'on'
MaxMeshSize: Inf
InitialPenalty: 10
PenaltyFactor: 100
PollMethod: 'gpspositivebasis2n'
CompletePoll: 'off'
PollingOrder: 'consecutive'
SearchMethod: []
CompleteSearch: 'off'
Display: 'final'
OutputFcns: []
PlotFcns: []
PlotInterval: 1
Cache: 'off'
CacheSize: 10000
CacheTol: 2.2204e-016
Vectorized: 'off'
UseParallel: 'never'
The function patternsearch uses these default values if you do not pass in options as an input argument.
The value of each option is stored in a field of the options structure, such as options.MeshExpansion. You can display any of these values by entering options followed by the name of the field. For example, to display the mesh expansion factor for the pattern search, enter
options.MeshExpansion
ans =
2
To create an options structure with a field value that is different from the default, use the function psoptimset. For example, to change the mesh expansion factor to 3 instead of its default value 2, enter
options = psoptimset('MeshExpansion', 3)
This creates the options structure with all values set to their defaults except for MeshExpansion, which is set to 3.
If you now call patternsearch with the argument options, the pattern search uses a mesh expansion factor of 3.
If you subsequently decide to change another field in the options structure, such as setting PlotFcns to @psplotmeshsize, which plots the mesh size at each iteration, call psoptimset with the syntax
options = psoptimset(options, 'PlotFcns', @psplotmeshsize)
This preserves the current values of all fields of options except for PlotFcns, which is changed to @plotmeshsize. Note that if you omit the options input argument, psoptimset resets MeshExpansion to its default value, which is 2.
You can also set both MeshExpansion and PlotFcns with the single command
options = psoptimset('MeshExpansion',3,'PlotFcns',@plotmeshsize)
As an alternative to creating the options structure using psoptimset, you can set the values of options in the Optimization Tool and then export the options to a structure in the MATLAB workspace, as described in the Importing and Exporting Your Work section of the Optimization Toolbox User's Guide. If you export the default options in the Optimization Tool, the resulting options structure has the same settings as the default structure returned by the command
options = psoptimset
except for the default value of 'Display', which is 'final' when created by psoptimset, but 'none' when created in the Optimization Tool.
You can also export an entire problem from the Optimization Tool and run it from the command line. Enter
patternsearch(problem)
where problem is the name of the exported problem.
![]() | Performing a Pattern Search Using the Optimization Tool GUI | Pattern Search Examples: Setting Options | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |