Skip to Main Content Skip to Search
Product Documentation

Computing Objective Functions

Objective (Fitness) Functions

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.

Example: Writing a Function 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:

  1. Select New > Script (Ctrl+N) from the MATLAB File menu. This opens a new file in the editor.

  2. 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);
  3. Save the file in a folder on the MATLAB path.

To check that the file returns the correct value, enter

my_fun([2 3])

ans =
   31

Example: Writing a Vectorized Function

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:

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.

Gradients and Hessians

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

ConditionOption 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

ConditionOption Setting
Objective function contains Jacobian'Jacobian' = 'on'

Maximizing vs. Minimizing

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

  


Free Optimization Interactive Kit

Learn how to use optimization to solve systems of equations, fit models to data, or optimize system performance.

Get free kit

Trials Available

Try the latest version of optimization products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS