| Contents | Index |
| On this page… |
|---|
Example: Writing a Function File |
To use Global Optimization Toolbox functions, you must first write a file (or an anonymous function) that computes the function you want to optimize. This function is called an objective function for most solvers or a fitness function for ga. The function should accept a vector whose length is the number of independent variables, and should return a scalar. 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.
The following example shows how to write a file for the function you want to optimize. Suppose that you want to minimize the function
![]()
The file that computes this function must accept a vector x of length 2, corresponding to the variables x1 and x2, and return a scalar equal to the value of the function at x. To write the file, do the following steps:
Select New > Script (Ctrl+N) from the MATLAB File menu. This opens a new file in the editor.
In the file, 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);
To check that the file returns the correct value, enter
my_fun([2 3]) ans = 31
The ga 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
If you have a nonlinear constraint, be sure to write the constraint in a vectorized fashion. For details, see Vectorized Constraints.
Set the Vectorized option to 'on' with gaoptimset or psoptimset, or set User function evaluation > Evaluate objective/fitness and constraint functions to vectorized in the Optimization Tool. For patternsearch, also set CompletePoll to 'on'. Be sure to pass the options structure to the solver.
For example, to write the objective function of Example: Writing 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 = psoptimset('CompletePoll','on','Vectorized','on');
[x fval] = patternsearch(@my_fun,[1 1],[],[],[],[],[],[],...
[],options);To use my_fun as a vectorized objective function for ga:
options = gaoptimset('Vectorized','on');
[x fval] = ga(@my_fun,2,[],[],[],[],[],[],[],options);For more information on writing vectorized functions for patternsearch, see Vectorizing the Objective and Constraint Functions. For more information on writing vectorized functions for ga, see Vectorizing 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 Writing Objective Functions in Optimization Toolbox documentation. Use optimset to set options so that your solver uses the derivative information:
Local Solver = fmincon, fminunc
| Condition | Option Setting |
|---|---|
| Objective function contains gradient | 'GradObj' = 'on' |
| Objective function contains Hessian | 'Hessian' = 'on' |
| Constraint function contains gradient | 'GradConstr' = 'on' |
| Calculate Hessians of Lagrangian in an extra function | 'Hessian' = 'on', 'HessFcn' = function handle |
For more information about Hessians for fmincon, see Hessian.
Local Solver = lsqcurvefit, lsqnonlin
| Condition | Option Setting |
|---|---|
| Objective function contains Jacobian | 'Jacobian' = 'on' |
Global Optimization Toolbox optimization functions minimize the objective or fitness function. That is, they solve problems of the form
![]()
If you want to maximize f(x), minimize –f(x), because the point at which the minimum of –f(x) occurs is the same as the point at which the maximum of f(x) occurs.
For example, suppose you want to maximize the function
![]()
Write your function file to compute
![]()
and minimize g(x).
![]() | Writing Files for Optimization Functions | Constraints | ![]() |

Learn how to use optimization to solve systems of equations, fit models to data, or optimize system performance.
Get free kit| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |