matlab函数指令(Matlab函数的基本使用)

matlab函数指令(Matlab函数的基本使用)(1)

Part 1

Matlab函数的基本概念

1.MATLAB的脚本和函数都是包含MATLAB命令的.m文件。

2.脚本没有参数的输入输出,函数有。

3.使用函数,需要一个脚本或者函数召唤它。

1. MATLAB scripts and functions are .m files containing MATLAB commands.

2. The script has no input and output parameters, but the function has.

3. To use a function, you need a script or function to call it.

Part 2

查看内嵌函数

如查看平均数函数

Such as viewing the average function

matlab函数指令(Matlab函数的基本使用)(2)

matlab函数指令(Matlab函数的基本使用)(3)

通过上述内嵌函数,我们可以看到函数设置的五个要素:

Through the above embedded function, we can see the five elements of the function setting:

1.函数使用的关键字是function

2.文件名与函数名字是一致的

3.(x,dim,flag)是输入,y是输出

4.dim和flag为局部变量

5.MATLAB需要有找到该函数的路径

1. The keyword used by the function is function

2. The file name is consistent with the function name

3. (x, dim, flag) is input, y is output

4.dim and flag are local variables

5. MATLAB needs a path to find the function

Part 3

用户自定义函数

如自定义自由落体位移函数

Such as custom free fall displacement function

matlab函数指令(Matlab函数的基本使用)(4)

首先新建脚本,输入函数,保存为与函数名相同名字的文件。

First create a new script, enter the function, and save it as a file with the same name as the function.

matlab函数指令(Matlab函数的基本使用)(5)

注意:元素与元素之间是点乘。

然后就可以调用该函数了。

Note: There is a dot product between elements.

Then you can call the function.

matlab函数指令(Matlab函数的基本使用)(6)

调用内嵌函数和自定义函数是一致的。例如可以计算两组数据。

Calling built-in functions is consistent with custom functions. For example, two sets of data can be calculated.

matlab函数指令(Matlab函数的基本使用)(7)

Part 4

多输入、多输出函数

如定义下列函数

Such as defining the following function

matlab函数指令(Matlab函数的基本使用)(8)

matlab函数指令(Matlab函数的基本使用)(9)

然后调用函数Then call the function

matlab函数指令(Matlab函数的基本使用)(10)

我们会发现结果不对,多输入多输出函数的正确使用方法如下:

We will find that the result is wrong. The correct way to use the multiple-input multiple-output function is as follows:

matlab函数指令(Matlab函数的基本使用)(11)

Part 5

没有input函数

例如,在命令栏输入一个华氏温度,然后计算成摄氏温度。

For example, enter a Fahrenheit temperature in the command bar, and then calculate it to Celsius.

matlab函数指令(Matlab函数的基本使用)(12)

Isempty:判断该函数是否为空

Num2str:将数值转化为字符串

然后运行程序,不停的输入,结果如下:

Isempty: Determine whether the function is empty

Num2str: Convert a value to a string

Then run the program, keep typing, the results are as follows:

matlab函数指令(Matlab函数的基本使用)(13)

参考资料:百度百科、B站郭彦甫Matlab教程、谷歌翻译

本文由LearningYard学苑原创,如有侵权,请联系删除。

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页