The parsing of function handle, feval function and inline function in MATLAB

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_7bff755b010180l3.html

matlab function handle

Function handle is a data type of MATLAB.

Contains the path of the function, the name of the function, the type, and the overloaded methods that may exist;

The function handle is introduced to make the feval and the functional instruction work more reliable, so that "function call" is as convenient and flexible as "variable invocation", and the speed of function call is increased, especially in the case of repeated calls; improve the reusability of software , enlarge the callable range of child function and private function Quickly obtain the position and type information of the overloaded function with the same name.

--------------------------------------

It can be used as easily as a variable: For example, when this directory is running, a handle to a function of this directory is created, and when you go to a different directory, you can call the function of the previous directory directly from the function handle without having to copy that function file. Because the path information is already contained in the created function handle;

Improve function call speed: because MATLAB calls the function every time to search all the paths, from the set path can be seen, the path is very much, so if a function in the program needs to be used frequently, the use of function handles, the speed will be improved;

--------------------------------------

the use of function handles in MATLAB makes it possible for a function to be an input variable and can be easily invoked to improve the usability and independence of the function.

A function handle actually provides a way to indirectly invoke a function.
Creating a function handle requires the operator @

syntax for creating a function handle:
Fhandle = @function_filename

This handle can be called when the function is called, enabling the same functionality.

For example Fhandle = @sin creates a handle to sin, and the input fhandle (x) is actually the function of sin (x).

Such as

% % ------------------------------------------------

[Email protected]

Fhandle (PI)

Sin (pi)

% % ------------------------------------------------

For example:

New M file f1.m

function Y=f1 (X)

X1=x (1); x2=x (2);
y=x1^2+x1*x2;

New M file ftest.m

function Y=ftest (f,x)

Syms X1 x2;
F=f ([x1,x2]);
V=[X1,X2];
Grad=jacobian (F,V);
Y=subs (GRAD,V,X);

In the MATLAB command window, enter Y=ftest (@f1, [2,1]) to get the result:

Y =

5 2

here @f1 is the handle of the function F1, you can use Class (@f1) to determine the type of the variable, the ANS = Function_handle is the function handle. The function ftest is to find the gradient value of a two-dimensional function at a specified point. We can replace the target function as F2 or other functions can also be obtained gradient. This way the function ftest can be applied to any two-dimensional function without having to change it.

How the Feval function understands
The most common application of the Feval function is the following form:
feval_r (' functionname ', parameter), give a simple example:
For example, to calculate sin (2), of course, you can use the command Y=sin (2), using Feval, you can also do this:
Y=feval_r (' sin ', 2); In addition, the function name can also be a function handle, i.e. [email protected];
Y=feval_r (h,2), or directly written Y=feval_r (@sin, 2);

inline functions (inline function)

The inline function is not a function handle, it is another type, it can also be used as a variable to represent a function, and the usage is the same as a function handle. An inline function and a normal function handle, exactly like an anonymous function, can be obtained by a function expression near the function.

When you create a local function in MATLAB, inline is available. The advantage is that it does not have to be stored as a separate file. There are a few restrictions in use: You cannot call another inline function, only one MATLAB expression, and only one variable---it is not allowed to do so [u,v]. Therefore, any application that requires a logical or multiplicative operation to obtain the final result cannot be applied inline.

The syntax format is as follows:

1 Variable name =inline (expression);

2 Variable name =inline (' function expression ', ' variable name 1 ', ' Variable name 2 ', ..., ' variable name n '); (recommended)

For example:

A= ' X+y ';

F=inline (a);
F (2,3)

Ans =

5

Or:

F=inline (' x+y ', ' x ', ' Y ');

F (2,3)

Ans =

5

The inline function can call an array on a variable, such as:

Fofx=inline (' x. ^2*cos (a*x)-B ', ' X ', ' A ', ' B ');

G= FOFX ([Pi/3 pi/3.5],4,1)

The system output is: g=-1.5483-1.7259

Parsing of function handles, feval functions, and inline functions in MATLAB (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.