N 1. Function file + call command file: Define an M file for a custom function separately;
N 2. Function file + sub-function: Define an M file with multiple user-defined functions;
N 3. inline: directly defined without M files;
N 4. Syms + Subs: directly defined without M files;
N 5. String + Subs: directly defined without M files.
1. Function file + call function file: define multiple M files:
% Call function file: myfile. m
Clear
CLC
For t = 1:10
Y = mylfg (t );
Fprintf ('% 4D ^ (1/3) = % 6.4f \ n', T, y );
End
% User-Defined Function file: mylfg. m
Function Y = mylfg (x) % Note: The function name (mylfg) must be consistent with the file name (mylfg. m ).
Y = x ^ (1/3 );
Note: This method requires that the user-defined function must write an M file separately and cannot be written in the same M file as the called command file.
2. Function file + sub-function: Define an M file with multiple sub-functions
% Command file: funtry2.m
Function [] = funtry2 ()
For t = 1:10
Y = lfg2 (t)
Fprintf ('% 4D ^ (1/3) = % 6.4f \ n ');
End
Function Y = lfg2 (X)
Y = x ^ (1/3 );
% Note: multiple sub-functions can be defined in the UDF file funtry2.m. The sub-function lfg2 can only be called by the main function and other sub-functions in the main function.
3. inline: directly defined without M files;
The % inline command is used to define an inline function: F = inline ('function expression', 'variable 1', 'variable 2 ',......).
Call method: Y = f (Value List) % note: the order of the substituted values should be the same as that defined in inline.
For example:
F = inline ('x ^ 2 + y', 'x', 'y ');
Z = f (2, 3)
Ans = 7
Note: This function is defined as an internal function call. The feature is that it is based on the MATLAB numerical operation kernel, so it is faster, the program efficiency is higher. The disadvantage is that this method can only substitute numeric values,Symbol substitution is not supported.And the Defined FunctionSymbol operations such as derivation cannot be performed.
Example:
Clear
CLC
F = 'x ^ 2 ';
Syms x g;
G = x ^ 2;
H = inline ('x ^ 2', 'x ');
4. Syms + Subs: directly defined without M files;
DefineSymbol Expression, Called with Subs:
Syms f x % definition symbol
F = 1/(1 + x ^ 2); % defines the Symbol Expression
Subs (F, 'x', instead of the value or symbol of X)
Note: For symbol variables that have been defined in Syms, single quotation marks can be omitted in subs replacement. However, if it is redefined as another type after Syms, it must be enclosed in single quotation marks. Otherwise, it cannot be replaced.
The feature of this function definition method is that it can be replaced with symbols.
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 symbol operation kernel.
5. String + Subs: directly defined without M files.
Define a string and use the subs command to call it. For example:
F = '1/(1 + x ^ 2) '% defines the string
Z = Subs (F, 'x', 2)
G = Subs (F, 'x', 'y ^ 2 ')
Note: The advantage is that the memory usage is minimal and the Definition Format is free.
The disadvantage is that characters cannot be converted into symbols.
When the symbol to be replaced already has a numerical value before calling, you can directly call: Subs (f). Example:
F = 'x ^ 2 * y ';
X = 2; y = 3;
Subs (f)
Ans = 12
[Transfer] http://apps.hi.baidu.com/share/detail/23406825