| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Optimization Toolbox |
| Contents | Index |
| Learn more about Optimization Toolbox |
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.
To pass parameters using anonymous functions:
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;
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:
[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
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 parameterfun 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.8696Now call lsqcurvefit:
% Assume ydata exists x = lsqcurvefit(fh,x,xdata,ydata)
To pass the parameters for Equation 2-1 via a nested function, write a single M-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 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
endThe 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 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.
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;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.
![]() | Writing Constraints | Setting Options | ![]() |

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 |