Parameterizing Functions
Overview
这一主题解释了如何存储或访问额外的parameters for mathematical functions that you pass to MATLAB®function functions, such asfzero
orintegral
.
MATLAB function functions evaluate mathematical expressions over a range of values. They are called function functions because they are functions that accept a function handle (a pointer to a function) as an input. Each of these functions expects that your objective function has a specific number of input variables. For example,fzero
andintegral
accept handles to functions that have exactly one input variable.
Suppose you want to find the zero of the cubic polynomialx3+
bx+
cfor different values of the coefficientsbandc. Although you could create a function that accepts three input variables (x,b, andc), you cannot pass a function handle that requires all three of those inputs tofzero
. However, you can take advantage of properties of anonymous or nested functions to define values for additional inputs.
Parameterizing Using Nested Functions
One approach for defining parameters is to use anested function—a function completely contained within another function in a program file. For this example, create a file namedfindzero.m
that contains a parent functionfindzero
and a nested functionpoly
:
functiony = findzero(b,c,x0) y = fzero(@poly,x0);functiony = poly(x) y = x^3 + b*x + c;endend
The nested function defines the cubic polynomial with one input variable,x
. The parent function accepts the parametersb
andc
as input values. The reason to nestpoly
withinfindzero
is that nested functions share the workspace of their parent functions. Therefore, thepoly
function can access the values ofb
andc
that you pass tofindzero
.
To find a zero of the polynomial withb = 2
andc = 3.5
, using the starting pointx0 = 0
, you can callfindzero
from the command line:
x = findzero(2,3.5,0)
x = -1.0945
Parameterizing Using Anonymous Functions
Another approach for accessing extra parameters is to use ananonymous function. Anonymous functions are functions that you can define in a single command, without creating a separate program file. They can use any variables that are available in the current workspace.
For example, create a handle to an anonymous function that describes the cubic polynomial, and find the zero:
b = 2; c = 3.5; cubicpoly = @(x) x^3 + b*x + c; x = fzero(cubicpoly,0)
x = -1.0945
Variablecubicpoly
is a function handle for an anonymous function that has one input,x
. Inputs for anonymous functions appear in parentheses immediately following the@
symbol that creates the function handle. Becauseb
andc
are in the workspace when you createcubicpoly
, the anonymous function does not require inputs for those coefficients.
You do not need to create an intermediate variable,cubicpoly
, for the anonymous function. Instead, you can include the entire definition of the function handle within the call tofzero
:
b = 2; c = 3.5; x = fzero(@(x) x^3 + b*x + c,0)
x = -1.0945
You also can use anonymous functions to call more complicated objective functions that you define in a function file. For example, suppose you have a file namedcubicpoly.m
with this function definition:
functiony = cubicpoly(x,b,c) y = x^3 + b*x + c;end
At the command line, defineb
andc
, and then callfzero
with an anonymous function that invokescubicpoly
:
b = 2; c = 3.5; x = fzero(@(x) cubicpoly(x,b,c),0)
x = -1.0945
Note
To change the values of the parameters, you must create a new anonymous function. For example:
b = 10; c = 25; x = fzero(@(x) x^3 + b*x + c,0);