Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Passing Extra Parameters

### Extra Parameters, Fixed Variables, or Data

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

 (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 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;```
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:

```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```
 Note:   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 run `[x,fval] = fminunc(f,x0)`You get the same answer as before, since parameterfun uses a = 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 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 file that

• Accepts a, b, c, and x0 as inputs

• Contains 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```

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

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