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.

For example, suppose you want to minimize the function

$$f(x)=\left(a-b{x}_{1}^{2}+{x}_{1}^{4}/3\right){x}_{1}^{2}+{x}_{1}{x}_{2}+\left(-c+c{x}_{2}^{2}\right){x}_{2}^{2}$$ | (2-3) |

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 *x*_{0} = [0.5 0.5] using `fminunc`

.

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

Call the solver

`fminunc`

with the anonymous function:The following output is displayed in the command window:[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

a = 4; b = 2.1; c = 4; f = @(x)parameterfun(x,a,b,c) Suppose you subsequently change, [x,fval] = fminunc(f,x0) You get the same answer as before, since 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 `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

`lsqcurvefit`

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

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

Accepts

`a`

,`b`

,`c`

, and`x0`

as inputsContains the objective function as a nested function

Calls

`fminunc`

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

Global variables can be troublesome, so 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.

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

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

Was this topic helpful?