MATLAB-defined symbolic functions (zz)

Source: Internet
Author: User

M file Function)
Inline Function)
Anonymous Function)


1. M file functions
Example
Function C = myadd (A, B)
% Here, you can write the function instructions, starting with %.
% In the workspace, help myadd will display the description here
C = A + B;
% End % not required
The first line of function tells MATLAB that this is a function. A and B are input, C is output, and myadd is function name. The function defined in the M file must be saved as the function name. In the above example, the function should be saved as myadd. M. To use the myadd function, the function must be in the MATLAB search path.
Call method:
After the MATLAB command, enter
A = 1; B = 2;
C = myadd (A, B)
There are still many descriptions about the M file definition function, which are omitted for the moment...


2. Online Functions)
It is usually passed to another function as a parameter. For example, functions such as fminsearch and lsqcurvefit must take functions as parameters.
Online functions create functions from string expressions, for example:
F = inline ('x. ^ 2', 'x ');
F (x) = x ^ 2 is created. To calculate F (3), enter F (3) in the workspace. F ([2 3 4]) calculates the value at x = 2 3 4.
F = inline ('x + y', 'x', 'y ')
A binary function f (x, y) = x + y is created. The input F (2, 3) in the workspace calculates 2 + 3, which is equivalent to feval_r (F, 2, 3 ).


3. Anonymous Function)
An anonymous function uses a function handle to represent an anonymous function. The definition format is
Function handle [email protected] (variable name) function expression
For example:
[Email protected] (x) X. ^ 2
The f (x) = x ^ 2, F (2) function is defined to calculate the value at x = 2.
Anonymous functions can call MATLAB functions or use variables in the workspace, such
A = 2;
[Email protected] (x) X. ^ 2 +
F (2) % variable A is referenced during Calculation
A = 0;
F (2) % Still references a = 2
Anonymous functions can also be created by built-in MATLAB functions or M file functions, such
[Email protected] % f (x) = sin (X)
F (PI/2) % sin (PI/2)
Functions (f) % view function Information
You can use the cell array to create multiple function handles, for example
F ={@ sin @ cos}
F {1} (PI/2) % calculate sin (PI/2)
F {2} (PI) % calculate cos (PI)
Another important feature of the function handle is that it can be used to represent subfunctions, private functions, and nested functions.
We recommend that you replace online functions with anonymous functions after MATLAB 7 !!!
When creating an anonymous function, Matlab records information about the function. When using a handle to call the function, Matlab does not search any more, but immediately executes the function, greatly improving the efficiency.

MATLAB-defined symbolic functions (zz)

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.