MATLAB_简介_3_MATLAB_m-file_程式与多项式函数.ppt
上传人:sy****28 上传时间:2024-09-15 格式:PPT 页数:52 大小:267KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

MATLAB_简介_3_MATLAB_m-file_程式与多项式函数.ppt

MATLAB_简介_3_MATLAB_m-file_程式与多项式函数.ppt

预览

免费试读已结束,剩余 42 页请下载文档后查看

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

Whatisanm-file?Useanordinarytexteditorandsavethefileasfilename.mChoose’newfile’undertheFileMenuInthefirstcasethefunctioninv(A)isusedtofindtheinverse.Thisisthenmultipliedbyb.TheMatlabcodeforthisoperationisExamplesolvetime.mA=rand(1000,1000);%Createsarandom%matrixAb=rand(1000,1);%Createsarandom%vectorbdet(A)%CalculatesthedeterminantofAtic,%Startsthetime-watchx=inv(A)*b;%Solvesthesystemtoc%Stopsthewatchtic,y=A\b;toc%Solvesandtimesthe%systemwithleftdivisionlapsed_time=1.2820elapsed_time=0.5310以下的tutex1.m档是一个简易绘图程式做为示范使用M-fileWhenyouwritesomethinginMatlab’scommandwindow,thefollowingthingsarecheckedinturn:1.Istheresuchavariableintheworkspace?2.Isthereanm-filecalledthatthinginthecurrentdirectory?3.Isthereanm-filecalledthatsomewhereelse?WhenMatlabsearchesforthem-fileitlooksinspecificfolders,itssearchpath.IfthefolderisnotaddedtothepathMatlabcannotrunyourscript.HowtomakesureMatlabfindsyourm-file>>cd\wufile\my_work%切换至目录\wufile\my_work>>cd%如果只用cd则会显示目前的目录c:\WUFILE\MY_WORK>>dir%列出目录下的档案.tutex1.mtutex2.m..test.txt>>deletetest.txt%删除test.txt>>path(path,'c:\wufile\my_work')%将自己的目录\wufile\my_work加在%MATLAB的搜寻路径之后>>path('c:\wufile\my_work',path)%将自己的目录\wufile\my_work加在%MATLAB的搜寻路径之前WhatisafunctionAfunctionisanm-filebeginningwiththeword’function’Afunctionhasauser-specifiedinputandoutputAfunctionhasitsown’local’workspace.Variablesdefinedinthefunctionarenotstoredintheordinaryworkspace.Norareyourworkspacevariablesalteredifgivenanewvalueinthe’local’workspace.myfile.mfunctionc=myfile(a,b)c=sqrt((a.^2)+(b.^2));Afunctiondoesnotassignanyvaluestoanything.Exceptiontothisruleisifyoudefineaglobalvariable.Type’helpglobal’formoreinformationYoucanhaveseveralfunctionsstoredinonefile.The’subfunctions’workexactlyasanormalfunction,butcannotbeaccesseddirectlyfromthecommandwindow(unlikeotherlanguages).Examplespir.mfunction[x,y]=spir(t)x=cos(20*t).*exp(-t.ˆ2);y=sin(20*t).*exp(-t.ˆ2);spirplot.mfunction[x,y]=spirplot(t)x=cos(20*t).*exp(-t.ˆ2);y=sin(20*t).*exp(