To use Global
Optimization Toolbox functions,
first write a file (or an anonymous function) that computes the function
you want to optimize. This is called an objective function for most
solvers, or fitness function for `ga`

. The function
should accept a vector, whose length is the number of independent
variables, and return a scalar. For `gamultiobj`

,
the function should return a row vector of objective function values.
For vectorized solvers, the function should accept a matrix, where
each row represents one input vector, and return a vector of objective
function values. This section shows how to write the file.

This example shows how to write a file for the function you want to optimize. Suppose that you want to minimize the function

$$f(x)=\mathrm{exp}\left(-\left({x}_{1}^{2}+{x}_{2}^{2}\right)\right)\left({x}_{1}^{2}-2{x}_{1}{x}_{2}+6{x}_{1}+4{x}_{2}^{2}-3{x}_{2}\right).$$

The file that computes this function must accept a vector `x`

of
length 2, corresponding to the variables *x*_{1} and *x*_{2},
and return a scalar equal to the value of the function at `x`

.

Select

**New > Script**(**Ctrl+N**) from the MATLAB^{®}**File**menu. A new file opens in the editor.Enter the following two lines of code:

function z = my_fun(x) z = x(1)^2 - 2*x(1)*x(2) + 6*x(1) + 4*x(2)^2 - 3*x(2);

Save the file in a folder on the MATLAB path.

Check that the file returns the correct value.

my_fun([2 3]) ans = 31

For `gamultiobj`

, suppose you have three
objectives. Your objective function returns a three-element vector
consisting of the three objective function values:

function z = my_fun(x) z = zeros(1,3); % allocate output z(1) = x(1)^2 - 2*x(1)*x(2) + 6*x(1) + 4*x(2)^2 - 3*x(2); z(2) = x(1)*x(2) + cos(3*x(2)/(2+x(1))); z(3) = tanh(x(1) + x(2));

The `ga`

, `gamultiobj`

, `paretosearch`

,
`particleswarm`

, and `patternsearch`

solvers optionally compute the
objective functions of a collection of vectors in one function call. This method can take
less time than computing the objective functions of the vectors serially. This method is
called a vectorized function call.

To compute in vectorized fashion:

Write your objective function to:

Accept a matrix with an arbitrary number of rows.

Return the vector of function values of each row.

For

`gamultiobj`

or`paretosearch`

, return a matrix, where each row contains the objective function values of the corresponding input matrix row.

If you have a nonlinear constraint, be sure to write the constraint in a vectorized fashion. For details, see Vectorized Constraints.

Set the

`UseVectorized`

option to`true`

using`optimoptions`

, or set**User function evaluation > Evaluate objective/fitness and constraint functions**to`vectorized`

in the Optimization app. For`patternsearch`

or`paretosearch`

, also set`UseCompletePoll`

to`true`

. Be sure to pass the options to the solver.

For example, to write the objective function of Write a Function File in a vectorized fashion,

function z = my_fun(x) z = x(:,1).^2 - 2*x(:,1).*x(:,2) + 6*x(:,1) + ... 4*x(:,2).^2 - 3*x(:,2);

To use `my_fun`

as a vectorized objective function
for `patternsearch`

:

options = optimoptions('patternsearch','UseCompletePoll',true,'UseVectorized',true); [x fval] = patternsearch(@my_fun,[1 1],[],[],[],[],[],[],... [],options);

To use `my_fun`

as a vectorized objective function
for `ga`

:

options = optimoptions('ga','UseVectorized',true); [x fval] = ga(@my_fun,2,[],[],[],[],[],[],[],options);

For `gamultiobj`

or `paretosearch`

,

function z = my_fun(x) z = zeros(size(x,1),3); % allocate output z(:,1) = x(:,1).^2 - 2*x(:,1).*x(:,2) + 6*x(:,1) + ... 4*x(:,2).^2 - 3*x(:,2); z(:,2) = x(:,1).*x(:,2) + cos(3*x(:,2)./(2+x(:,1))); z(:,3) = tanh(x(:,1) + x(:,2));

To use `my_fun`

as a vectorized objective function
for `gamultiobj`

:

options = optimoptions('ga','UseVectorized',true); [x fval] = gamultiobj(@my_fun,2,[],[],[],[],[],[],options);

For more information on writing vectorized functions for `patternsearch`

,
see Vectorize the Objective and Constraint Functions. For more
information on writing vectorized functions for `ga`

,
see Vectorize the Fitness Function.

If you use `GlobalSearch`

or `MultiStart`

,
your objective function can return derivatives (gradient, Jacobian,
or Hessian). For details on how to include this syntax in your objective
function, see Including Gradients and Hessians (Optimization Toolbox). Use `optimoptions`

to set options so that your
solver uses the derivative information:

**Local Solver = fmincon, fminunc**

Condition | Option Setting |
---|---|

Objective function contains gradient | `'SpecifyObjectiveGradient' = true` ; see How to Include Gradients (Optimization Toolbox) |

Objective function contains Hessian | `'HessianFcn' = 'objective'` or a function
handle; see Including Hessians (Optimization Toolbox) |

Constraint function contains gradient | `'SpecifyConstraintGradient' = true` ; see Including Gradients in Constraint Functions (Optimization Toolbox) |

**Local Solver = lsqcurvefit, lsqnonlin**

Condition | Option Setting |
---|---|

Objective function contains Jacobian | `'SpecifyObjectiveGradient' = true` |