matlab study notes 13_2 anonymous function

Learn together matlab-matlab function study notes 13

13_2 anonymous function

Find useful, welcome to discuss mutual learning together - Follow Me

Reference
https://ww2.mathworks.cn/help/matlab/matlab_prog/anonymous-functions.html?searchHighlight=%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0&s_tid = doc_srchtitle

What is an anonymous function

  • Anonymous function is not stored in the program file, but with the type of data is a function of variable function_handle related. Anonymous functions can accept input and return output, just like standard functions. However, they may contain only one executable statement.
  • For example, create an anonymous function is used to calculate the number of square handle:
sqr = @(x) x.^2;
  • Variable sqr is a function handle. @ Operator to create a handle, back @ parentheses operator () including the input parameters. The anonymous function accepts a single input x, and an explicit return a single output, i.e., the size of the square value of x contains the same array.
  • Calculating the square of this value is transmitted by a specific value (5) to function handle, you pass input parameters to the function as a standard.
a = sqr(5)
a =
   25
  • Many MATLAB® functions accept as an input function handle, so that the function can be calculated within a specific range of values. You can create a function handle for an anonymous function or program file. The advantage of using an anonymous function is not necessary to require only a short-defined functions to edit and maintain files.
    • By the transfer function handle integral to function, the function calculating sqr range from 0 to 1. Points:
      Q = integral (sqr, 0,1);
    • No need to create a variable in the workspace to store the anonymous function. You can create a temporary function handle in the expression, such as the call to the integral function:
      q = integral (. @ (The X-) the X-^ 2,0,1);

Variables in the expression

  • Function handle can be stored expression, the expression can store not only variable computing needs.
    • For example, the need to create a function handle coefficients a, b and c anonymous functions.
    a = 1.3;
    b = .2;
    c = 30;
    parabola = @(x) a*x.^2 + b*x + c;
    • Because of a, b and c are available when you create a parabola, the function handle includes these values. Even if you clear variables, these values ​​still persisted in the function handle:
    clear a b c
    x = 1;
    y = parabola(x)
    y =
     31.5000
    • To provide different values ​​for these coefficients do not need to create a new function handle
    a = -3.9;
    b = 52;
    c = 0;
    parabola = @(x) a*x.^2 + b*x + c;
    
    x = 1;
    y = parabola(1)
    y =
     48.1000
  • Function handle may be stored in their associated values ​​MAT file, then save and load functions MATLAB load them in subsequent sessions. save myfile.mat parabola

Note: Use only explicit variables when constructing an anonymous function. If anonymous access any function or variable nested functions are not explicitly referenced in the parameter list or body, the MATLAB throws an error when you call the function. Implicit Variable and function calls are commonly encountered in eval, evalin, assignin and load and other functions. Avoid using these functions in the body of the anonymous function

Multiple anonymous function

  • Anonymous function expression can contain other anonymous function. This can be used to pass parameters to the different function calculation in a certain range of values. For example, the following equation can be solved for various values ​​of c, is used in combination two anonymous functions:
    Here Insert Picture Description
g = @(c) (integral(@(x) (x.^2 + c*x + 1),0,1));
  1. Integrand will be written as anonymous functions,
    @(x) (x.^2 + c*x + 1)
  2. Transmitted to the integral calculation in the range of from 0 to 1 by the function handle function,
    integral(@(x) (x.^2 + c*x + 1),0,1)
  3. Through the entire equation configured to provide an anonymous function value c,
    g = @(c) (integral(@(x) (x.^2 + c*x + 1),0,1));
  4. The final function can be used to solve the equation for any value of c. E.g:
g(2)
  ans =
 2.3333

Without the function of the inputs

  • If the function does not require any input, the input space in the anonymous function definitions and calls parentheses
t = @() datestr(now);
d = t()
d =
26-Jan-2012 15:11:47
  • But do not put parentheses is the wrong wording, which creates another function handle, and does not perform the function
d = t
d =
    @() datestr(now)

Without the function of the inputs

  • A plurality of input anonymous function requires explicit designation of a plurality of spaced apart input by commas.
myfunction = @(x,y) (x^2 + y^2 + x*y);

x = 1;
y = 10;
z = myfunction(x,y)
z = 111
  • But it does not explicitly define the output parameters when creating an anonymous function, if the expression of the function to return multiple outputs, you can request them when the function is called. A plurality of output variables enclosed in square brackets.
c = 10;
mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y));
[x,y] = mygrid(pi,2*pi);

An array of anonymous functions

  • While most of the basic MATLAB data type support multidimensional arrays, but the function handle must be marked () (single element). It may be used cellular array or an array of structures storing a plurality of function handle. The most common way is to use a cellular array, e.g.
f = {@(x)x.^2;
     @(y)y+10;
     @(x,y)x.^2+y+10};
  • When you create a cellular array, MATLAB will remember that spaces be interpreted as the column delimiter. As shown in the code above, omitting spaces expression, or expression enclosed in parentheses, e.g.
    • Note the spaces and brackets
      f = {@(x) (x.^2); @(y) (y + 10); @(x,y) (x.^2 + y + 10)};
  • Use curly braces to access cellular content. For example, f {1} returns the first function handle. To perform this function, pass the input values ​​in parentheses after the braces
x = 1;
y = 10;

f{1}(x)
f{2}(y)
f{3}(x,y)
ans =
     1

ans =
    20

ans =
    21

Guess you like

Origin www.cnblogs.com/cloud-ken/p/11788724.html