Part1
Matlab函数的基本概念
1.MATLAB的脚本和函数都是包含MATLAB命令的.m文件。
2.脚本没有参数的输入输出,函数有。
3.使用函数,需要一个脚本或者函数召唤它。
1.MATLABscriptsandfunctionsare.mfilescontainingMATLABcommands.
2.Thescripthasnoinputandoutputparameters,butthefunctionhas.
3.Touseafunction,youneedascriptorfunctiontocallit.
Part2
查看内嵌函数
如查看平均数函数
Suchasviewingtheaveragefunction
通过上述内嵌函数,我们可以看到函数设置的五个要素:
Throughtheaboveembeddedfunction,wecanseethefiveelementsofthefunctionsetting:
1.函数使用的关键字是function
2.文件名与函数名字是一致的
3.(x,dim,flag)是输入,y是输出
4.dim和flag为局部变量
5.MATLAB需要有找到该函数的路径
1.Thekeywordusedbythefunctionisfunction
2.Thefilenameisconsistentwiththefunctionname
3.(x,dim,flag)isinput,yisoutput
4.dimandflagarelocalvariables
5.MATLABneedsapathtofindthefunction
Part3
用户自定义函数
如自定义自由落体位移函数
Suchascustomfreefalldisplacementfunction
首先新建脚本,输入函数,保存为与函数名相同名字的文件。
Firstcreateanewscript,enterthefunction,andsaveitasafilewiththesamenameasthefunction.
注意:元素与元素之间是点乘。
然后就可以调用该函数了。
Note:Thereisadotproductbetweenelements.
Thenyoucancallthefunction.
调用内嵌函数和自定义函数是一致的。例如可以计算两组数据。
Callingbuilt-infunctionsisconsistentwithcustomfunctions.Forexample,twosetsofdatacanbecalculated.
Part4
多输入、多输出函数
如定义下列函数
Suchasdefiningthefollowingfunction
然后调用函数Thencallthefunction
我们会发现结果不对,多输入多输出函数的正确使用方法如下:
Wewillfindthattheresultiswrong.Thecorrectwaytousethemultiple-inputmultiple-outputfunctionisasfollows:
Part5
没有input函数
例如,在命令栏输入一个华氏温度,然后计算成摄氏温度。
Forexample,enteraFahrenheittemperatureinthecommandbar,andthencalculateittoCelsius.
Isempty:判断该函数是否为空
Num2str:将数值转化为字符串
然后运行程序,不停的输入,结果如下:
Isempty:Determinewhetherthefunctionisempty
Num2str:Convertavaluetoastring
Thenruntheprogram,keeptyping,theresultsareasfollows: