Five representations of MATLAB custom functions (top 2 focus)

Source: Internet
Author: User

1. command file/function file + function file: multiple m files

2. function file + sub-function: one m file

3. Inline: no m file required

4. Symbolic expression +subs mode: no M file required

5. String +subs mode: no M file required

First Type: Command file/function file + function file

% command/function file: myfile1.m

Clear

For T=1:10

Y=MYLFG (t);

fprintf ('%4d^ (1/3) =%6.4f\n ', t,y);

End

% function File: mylfg.m

Function y = MYLFG (x)

y=x^ (1/3);

Note:

1. Pay attention to the matching of real participation parameters when function calls

2. Function must be written separately in one file!

No, you can write to the command file in the same file.

3. The function name is best consistent with the file name.

Second way: Function file + sub-function

% function File: funtry2.m

fumction [] = Funtry2 ()

For t= 1:10

Y=LFG2 (t);

fprintf ('%4d^ (1/3)%6.4f\n ', t,y);

End

Percent of the child function

Function y = lfg2 (x)

Y =x^ (1/3);

Note:

A function file can define one or more child functions, at which point we call the function the primary function, and the child function can only be called by the main function or other functions in the same function file.

Question: How does the function file in mode one differ from the main function of mode two???? Is it a concept???

Mode three: inline+ command/function file

The 1.inline command can be used to define an inline function

f = inline (' function expression ', ' Variable 1 ', ' Variable 2 ', ...)

2. Call Mode: y=f (Value list)

The order in which the values are brought into the list should match the order of the variable names when defined

eg:f = inline (' x^2+y ', ' x ', ' Y ');

Z = f (2,3);

Z =7

Advantages: Fast Operation speed

Cons: 1.2 more memory. Symbolic operations that can only be brought into a numeric value and cannot be derivative

Mode four: Syms + Subs

Syms defines a symbolic expression that is invoked with the subs command

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)

Pros: can be replaced with symbols

Cons: Low computational speed

Mode five: string + Subs

Define a string directly, complete the call with the subs command

F= ' 1/(1+x^2) '

Z=subs (F, ' X ', 2)

g = Subs (F, ' X ', ' y^2 ')

Advantages: Low memory consumption, easy to format and free

Five representations of MATLAB custom functions (top 2 focus)

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.