MATLAB: How to quickly call a function with variable parameters-parameterized functions (parameterization of nested functions, parameterization of anonymous functions)

Parameterized function

  • Overview This topic shows you how to store or access additional parameters passed to mathematical functions MATLAB® function (such as fzero or integral).

  • The MATLAB function function calculates mathematical expressions based on a range of values. These functions are called function
    functions because they are functions that accept function handles (pointers to functions) as input. Each of these functions requires the objective function to have a certain number of input variables. For example, fzero and
    integral accept handles to functions that have exactly one input variable.

    Suppose you need to calculate the zero point of the cubic polynomial x3 + bx + c when the coefficients b and c have different values. Although you can create a function that accepts three input variables (x, b, and c), you cannot pass a function handle that requires all three inputs to fzero. However, you can use the attributes of anonymous functions or nested functions to define other input values.

1. Use nested function parameterization

One way to define parameters is to use nested functions-functions that are completely contained within another function in the program file. For this example, a file named findzero.m will be created that contains the parent function findzero and the 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

This nested function defines a cubic polynomial with an input variable x. The parent function accepts parameters b and c as input values. The reason for nesting poly within findzero is that nested functions share the workspace of their parent function. Therefore, the poly function can access the values ​​of b and c that you pass to findzero.

Requires the zero point of the polynomial when b = 2 and c = 3.5. If you use the starting point x0 = 0, you can call findzero from the command line:

x = findzero(2,3.5,0)
x =
   -1.0945

2. Use anonymous functions for parameterization
Another way to access additional parameters is to use anonymous functions. Anonymous functions are functions that can be defined in a single command without creating a separate program file. These functions can use any variable available in the current workspace.

For example, create a handle to an anonymous function describing a cubic polynomial and find the zero point:

b = 2;
c = 3.5;
cubicpoly = @(x) x^3 + b*x + c;
x = fzero(cubicpoly,0)
x =
   -1.0945

The variable cubicpoly is a function handle of an anonymous function with an input x. The input of the anonymous function is shown in parentheses and immediately after the @ sign used to create the function handle. Since b and c are in the workspace when you create the cubicpoly, the anonymous function does not require the input of these coefficients.

There is no need to create an intermediate variable cubicpoly for anonymous functions. The entire definition of the function handle can be included in the call to fzero:

b = 2;
c = 3.5;
x = fzero(@(x) x^3 + b*x + c,0)
x =
   -1.0945

You can also use anonymous functions to call more complex target functions defined in the function file. For example, suppose there is a file named cubicpoly.m with the following function definitions:

function y = cubicpoly(x,b,c)
y = x^3 + b*x + c;
end

On the command line, define b and c, and then use an anonymous function that calls cubicpoly to call fzero:

b = 2;
c = 3.5;
x = fzero(@(x) cubicpoly(x,b,c),0)
x =
   -1.0945

note

To change the value of the parameter, a new anonymous function must be created. E.g:

b = 10;
c = 25;
x = fzero(@(x) x^3 + b*x + c,0);

Guess you like

Origin blog.csdn.net/qq_40797015/article/details/108071700