Products & Services Solutions Academia Support User Community Company

Learn more about Optimization Toolbox   

Passing Extra Parameters

Sometimes objective or constraint functions have parameters in addition to the independent variable. There are three methods of including these parameters:

Global variables are troublesome because they do not allow names to be reused among functions. It is better to use one of the other two methods.

For example, suppose you want to minimize the function

(2-1)

for different values of a, b, and c. Solvers accept objective functions that depend only on a single variable (x in this case). The following sections show how to provide the additional parameters a, b, and c. The solutions are for parameter values a = 4, b = 2.1, and c = 4 near x0 = [0.5 0.5] using fminunc.

Anonymous Functions

To pass parameters using anonymous functions:

  1. Write an M-file containing the following code:

    function y = parameterfun(x,a,b,c)
    y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + ...
        (-c + c*x(2)^2)*x(2)^2;
    
  2. Assign values to the parameters and define a function handle f to an anonymous function by entering the following commands at the MATLAB prompt:

    a = 4; b = 2.1; c = 4; % Assign parameter values
    x0 = [0.5,0.5];
    f = @(x)parameterfun(x,a,b,c)
  3. Call the solver fminunc with the anonymous function:

    [x,fval] = fminunc(f,x0)

    The following output is displayed in the command window:

    Warning: Gradient must be provided for trust-region method;
      using line-search method instead. 
    > In fminunc at 356
    
    Local minimum found.
    
    Optimization completed because the size of the gradient is less than
    the default value of the function tolerance.
    
    x =
       -0.0898    0.7127
    
    fval =
       -1.0316

You can create anonymous functions of more than one argument. For example, to use lsqcurvefit, first create a function that takes two input arguments, x and xdata:

fh = @(x,xdata)(sin(x).*xdata +(x.^2).*cos(xdata));
x = pi; xdata = pi*[4;2;3];
fh(x, xdata)

ans =

    9.8696
    9.8696
   -9.8696

Now call lsqcurvefit:

% Assume ydata exists
x = lsqcurvefit(fh,x,xdata,ydata)

Nested Functions

To pass the parameters for Equation 2-1 via a nested function, write a single M-file that

Here is the code for the M-file for this example:

function [x,fval] =  runnested(a,b,c,x0) 
[x,fval] = fminunc(@nestedfun,x0);
% Nested function that computes the objective function     
    function y = nestedfun(x)
        y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) +...
            (-c + c*x(2)^2)*x(2)^2;     
    end
end

The objective function is the nested function nestedfun, which has access to the variables a, b, and c.

To run the optimization, enter:

a = 4; b = 2.1; c = 4;% Assign parameter values
x0 = [0.5,0.5];
[x,fval] = runnested(a,b,c,x0)

The output is the same as in Anonymous Functions.

Global Variables

Global variables can be troublesome, it is better to avoid using them. To use global variables, declare the variables to be global in the workspace and in the functions that use the variables.

  1. Write an M-file containing code for your function:

    function y = globalfun(x)
    global a b c
    y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + ...
        (-c + c*x(2)^2)*x(2)^2;
  2. In your MATLAB workspace, define the variables and run fminunc:

    global a b c;
    a = 4; b = 2.1; c = 4; % Assign parameter values
    x0 = [0.5,0.5];
    [x,fval] = fminunc(@globalfun,x0)

The output is the same as in Anonymous Functions.

  


Recommended Products

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