Sometimes objective or constraint functions have parameters in addition to the independent variable. The extra parameters can be data, or can represent variables that do not change during the optimization. There are three methods of passing 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.
Generally, for problem-based optimization, you pass extra parameters in a natural manner. See Pass Extra Parameters in Problem-Based Approach.
For example, suppose you want to minimize the function
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
To pass parameters using anonymous functions:
Write a 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;
Assign values to the parameters and define a function
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);
Call the solver
the anonymous function:
[x,fval] = fminunc(f,x0)
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
The parameters passed in the anonymous function are those that exist at the time the anonymous function is created. Consider the example
a = 4; b = 2.1; c = 4; f = @(x)parameterfun(x,a,b,c)
Suppose you subsequently change,
a to 3 and
[x,fval] = fminunc(f,x0)
You get the same answer as before, since
= 4, the value when
f was created.
To change the parameters that are passed to the function, renew the anonymous function by reentering it:
a = 3; f = @(x)parameterfun(x,a,b,c)
You can create anonymous functions of more than one argument.
For example, to use
first create a function that takes two input arguments,
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
% Assume ydata exists x = lsqcurvefit(fh,x,xdata,ydata)
To pass the parameters for Equation 1 via a nested function, write a single file that
x0 as inputs
Contains the objective function as a nested function
Here is the code for the function 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
nestedfun, which has access to the variables
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)
Global variables can be troublesome, so it is better to avoid using them. Also, global variables fail in parallel computations. See Factors That Affect Results.
To use global variables, declare the variables to be global in the workspace and in the functions that use the variables.
Write a function file:
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;
In your MATLAB workspace, define the variables and run
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.