Matlab
six ways to customize a function
N1, function file + Call Function (command) file: m file to define a custom function separately;
N2, function file + sub-function: Defines an m file with multiple custom functions;
N3,Inline: No m file, direct definition;
N4, anonymous function;
N5,syms+subs: No m file, direct definition;
N6, String +subs: No m file, directly defined.
------------
1 , function Files + calling function Files: defining multiple M file
% Call Function file : MYFILE.M
Clear
Clc
For T=1:10
Y=MYLFG (t);
fprintf (' m^ (1/3) =%6.4f\n ', t,y);
End
% Custom Function file : mylfg.m
function Y=MYLFG (x)% Note: The functions name (MYLFG) must match the file name (mylfg.m)
y=x^ (1/3);
Note: This method requires that the custom function must write an m file separately and cannot be written in the same m file as the calling command file .
2 , function Files + Sub-function: Defines a function that has multiple child functions. M file
% command file:funtry2.m
function []=funtry2 ()
For T=1:10
Y=LFG2 (t)
fprintf (' m^ (1/3) =%6.4f\n ');
End
function Y=lfg2 (x)
y= x^ (1/3);
% Note: multiple child function functions can be defined in a custom function file funtry2.m . Child function Lfg2 can only be called by the main function and other child functions in the main function.
Note: function definition Format
In matlab should be made of M file, the file name and the function name in your files after the same, in a new m-file m-file Edit the function, the format is:
function [Output argument table ]= function name (input real parameter )
Comments section
function Body Statement
Return statement (can have no )
If it is a child function in a file, you can name it arbitrarily, or you can define multiple child functions in the same file:
Example: function [Max,min]=mymainfun (x)% main function
N=length (x);
MAX=MYSUBFUN1 (x,n);
MIN=MYSUBFUN2 (x);
function r=mysubfun1 (x,n)% sub-functions 1
X1=sort (x);
R=X1 (n);
function r=mysubfun2 (x)% sub-functions 2
X1=sort (x);
R=X1 (1);
See the following article for details: matlab methods for defining child functions in the same file
"description" You can write a main function and a number of sub-functions in a m file, the file name is the main function name, the first function is the main function, followed by the function is the function of the need to call the child function or sub-sub-function. That is, the file must be a function file that begins with a function, and cannot be preceded by functions, followed by a string of functions. Emphasize that, before the command is added to the function is not possible, must be before the function! Instead of a command, you can define these commands as a parameterless function directly.
3 , Inline: without M file, directly defined
The%inline command is used to define an inline function:f=inline (' function expression ', ' variable 1', ' variable 2 ',...... ).
Call mode:y=f (Value list )% Note: The order of the values to be entered should be consistent with the order of the variable names defined by the inline ().
For example:
F=inline (' x^2+y ', ' x ', ' Y ');
Z=f (2,3)
Ans=7
Note: This function is defined in such a way that it is called as an intrinsic function. The characteristic is that it is based on the numerical computing kernel of MATLAB, so it has a faster operation and higher program efficiency. The disadvantage is that the method can only substitute numeric values, does not support symbol generation, and the defined function can not be derivative and other symbolic operations.
Cases:
Clear
Clc
f= ' x^2 ';
Syms x G;
g=x^2;
H=inline (' x^2 ', ' X ');
4. anonymous functions
Using the matlab function handle operator @, you can define function handles that point to MATLAB built-in functions and user-defined functions, and function handles can be used like functions. For example:
>>x=-pi:0.1:pi;
>> fh={@cos, @sin};
FH =
@cos @sin
>>plot (Fh{2} (x))
5 , syms+subs: without M file , Direct Definition
Use syms to define a symbolic expression, called with Subs:
Syms f x% definition symbol
F=1/(1+x^2); % definition Symbol expression
Subs (F, 'X ', in place of the value or symbol of x )
Note: For symbolic variables that have already been defined in Syms, single quotes can be omitted when overridden in subs. However, if you re-define another type after syms, you must enclose it in single quotation marks, otherwise you cannot replace it.
The feature of this function definition method is that it can be replaced with a symbol
Syms f X
F=1/(1+x^2);
Subs (F, ' X ', ' y^2 ')
ans=
1/(1+ (y^2) ^2)
Note: The disadvantage of this method is that the operation speed is greatly reduced due to the use of the symbolic operation kernel.
6 , String +subs : No M file , Direct Definition
Define a string directly and invoke it with the Subs command. For example:
f= '1/(1+x^2) ' % definition string
Z=subs (F, ' X ', 2)
G=subs (F, ' X ', ' y^2 ')
Note: The advantage is that the memory footprint is minimal and the format is free to define.
The disadvantage is that characters cannot be converted to symbols.
When the symbol you want to replace has a numeric definition before the call, you can call it directly:subs (f). For example:
f= ' X^2*y ';
x=2;y=3;
Subs (f)
Ans=12
-------------------------------------------
Details: Matlab methods for defining child functions in the same file
Sometimes a child function may be short, just a few lines, and you may want to put multiple function definitions in the same . m file, where there is a definition of a sub-function.
in the matlab in one . M there can be multiple sub-functions in a file, but only one main function can be used, and M the file name must be the same as the main function
there are usually two ways to define a sub-function in an M file:
1. Nesting Definitions
function MainFunc ()
...
A = Myfunc1 ();
...
function Myfunc1 ()
...
End
End
2. non-nested definitions
function MainFunc ()
...
A = Myfunc1 ();
...
End
function Myfunc1 ()
...
End
in the second case, myfunc1 and the main function is two separate functions, and write them in two separate files
There is no difference in. Variable name how to get up, there will be no conflict.
and in the first case, myfunc1 the variable name is shared with the main function. In this case, use the same variable name, such as
If it is not intentional, it is inappropriate.
for nested functions, the function definition End essential.
Example:
Here's what's in a. m script file (son_function.m file):
% %-----------------------------------------------------------------------
function son_function ()% main functions must be at the top
% Sub-function Example
Max1=find_max (a)
Max2=find_max (7,3,9)
function Max=find_max (a,b,c)% sub-functions
if (a>=b) & (A>=c)
Max=a;
ElseIf (b>=a) & (B>=c)
Max=b;
Else
Max=c;
End
% %-----------------------------------------------------------------------
Attention:
In M files, it is not allowed to write a function call directly into the M script file, and it must also be written in the form of a function, or as a separate m file for a child function .
If the first line in the previous program is masked out,
% function Son_function ()
The following error will appear
Function definitions is not permitted on the prompt or in scripts.
---------------------------------
The following paragraph of text is excerpted from matlab help:End in function use
End also marks the termination of a m-file function, although in most cases, it's optional. End statements are required Only in M-files this employ one or more nested functions. Within such an m-file, every function (including primary, nested, private, and subfunctions) must is terminated with an en D statement. You can terminate any function type with end, but doing so are not required unless the m-file contains a nested function
Although theuse of end in function is optional in most cases, it can also be used to mark the end of an M file function. The End statement is required only if one or more nested functions are used. In M-Files of such nested functions, each function (including the keynote function, nested functions, private functions, and child functions) must end with an end statement. You can use end to end either function type, but doing so except for M-Files containing a nested function is not necessary in other cases.
function definition method in MATLAB