|On this page…|
20This topic explains how to store or access extra parameters for mathematical functions that you pass to MATLAB® function functions, such as fzero or integral.
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 and integral accept handles to functions that have exactly one input variable.
Suppose you want to find the zero of the cubic polynomial x3 + bx + c for different values of the coefficients b and c. Although you could create a function that accepts three input variables (x, b, and c), you cannot pass a function handle that requires all three of those inputs to fzero. However, you can take advantage of properties of anonymous or nested functions to define values for additional inputs.
One approach for defining parameters is to use a nested function—a function completely contained within another function in a program file. For this example, create a file named findzero.m that contains a parent function findzero and a nested function poly:
function y = findzero(b,c,x0) y = fzero(@poly,x0); function y = poly(x) y = x^3 + b*x + c; end end
The nested function defines the cubic polynomial with one input variable, x. The parent function accepts the parameters b and c as input values. The reason to nest poly within findzero is that nested functions share the workspace of their parent functions. Therefore, the poly function can access the values of b and c that you pass to findzero.
To find a zero of the polynomial with b = 2 and c = 3.5, using the starting point x0 = 0, you can call findzero from the command line:
x = findzero(2,3.5,0)
x = -1.0945
Another approach for accessing extra parameters is to use an anonymous 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
Variable cubicpoly 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. Because b and c are in the workspace when you create cubicpoly, 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 to fzero:
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 named cubicpoly.m with this function definition:
function y = cubicpoly(x,b,c) y = x^3 + b*x + c; end
At the command line, define b and c, and then call fzero with an anonymous function that invokes cubicpoly:
b = 2; c = 3.5; x = fzero(@(x) cubicpoly(x,b,c),0)
x = -1.0945