MATLAB实验报告.docx
- 文档编号:25829368
- 上传时间:2023-06-15
- 格式:DOCX
- 页数:23
- 大小:91.80KB
MATLAB实验报告.docx
《MATLAB实验报告.docx》由会员分享,可在线阅读,更多相关《MATLAB实验报告.docx(23页珍藏版)》请在冰豆网上搜索。
MATLAB实验报告
四川师范大学MATLAB语言实验报告1
系 级 班 年 月 日
实验名称:
Intro,Expressions,Commands
姓名 学号 指导教师 成绩
1Objective
TheobjectiveofthislabistofamiliarizeyouwiththeMATLABprogramdevelopmentenvironmentandtodevelopyourfirstprogramsinthisenvironment.
2UsingMATLAB
2.1StartingMATLAB
LogontoyourcomputerandstartMATLABbydouble-clickingontheicononthedesktoporbyusingtheStartProgramsmenu.MATLABDesktopwindowwillappearonthescreen.
Thedesktopconsistsofseveralsub-windows.Themostimportantonesare:
●CommandWindow(ontherightsideoftheDesktop)isusedtodocalculations,entervariablesandrunbuilt-inandyourownfunctions.
●Workspace(ontheupperleftside)consistsofthesetofvariables(arrays)createdduringthecurrentMATLABsessionandstoredinmemory.
●CommandHistory(onthelowerleftside)logscommandsenteredintheCommandWindow.Youcanusethiswindowtoviewpreviouslyrunstatements,andcopyandexecuteselectedstatements.
YoucanswitchbetweentheLaunchPadwindowandtheWorkspacewindowusingthemenutabsunderthesub-windowontheupperleftside.Similarly,youcanswitchbetweentheCommandHistoryandCurrentDirectorywindowsusingthemenutabsunderthesub-windowonthelowerleftside.
2.2ExecutingCommands
YoucantypeMATLABcommandsatthecommandprompt“>>”ontheCommandWindow.
Forexample,youcantypetheformulacos(π/6)2sin(3π/8)as
>>(cos(pi/6)^2)*(sin(3*pi/8))
Trythiscommand.Afteryoufinishtyping,pressenter.ThecommandwillbeinterpretedandtheresultwillbedisplayedontheCommandWindow.
TrythefollowingbyobservinghowtheWorkspacewindowchanges:
>>a=2;(Makenoteoftheusageof“;”)
>>b=3;
>>c=a^4∗b∗5+pi^3
Youcanseethevariablesa,bandcwiththeirtypesandsizesontheWorkspacewindow,andcanseethecommandsontheCommandHistorywindow.
Spendafewminutestopracticedefiningarrayvariables(i.e.vectorsandmatrices)using
thesquarebracket(“[]”)andcolon(“:
”)operators,andzeros()andones()functions.
>>ar=[12345];
>>br=[123;456];
>>cr=[1:
3:
15];
•Setdrtofirst3elementsofar.
•Setertosecondrowofbr.
•Setarto[drer].Findthenumberofelementsofar.
2.3GettingHelp
ThereareseveralwaystogethelponcommandsandfunctionsinMATLAB.FirstofallyoucanusetheHelpmenu.Youcanalsousethe“?
”button.Trytofindinformationontheplotfunctionfromthehelpindex.Alsotrytogetinformationonthesamefunctionusingthehelpcommand(i.e.typehelpplot).Finally,experimentwiththelookforcommand.Thiscommandlooksforothercommandsrelatedtoagivenkeyword.
2.4SomeUsefulCommands
Trythefollowingcommandsandobservetheirresults:
Which:
Versionandlocationinfo
Clear:
Clearstheworkspace
Clc:
Clearsthecommandwindow
who,whos:
Listscontentoftheworkspace
3Exercises
PleasesolvethefollowingproblemsinMATLAB.Donotforgettokeepadiaryofyourcommandsandtheiroutputs.
(1)Definethevariablesxyandzas7.6,5.5and8.1,respectively,andevaluate:
(2)Computetheslopeofthelinethatpassesthroughthepoints(1,-2)and(5,8).
(3)Quiz1.1:
5
(4)1.6Exercises:
1.1,1.4
(5)2.15Exercises:
2.6,2.9,2.11
4QuittingMATLAB
Typingquitonthecommandwindowwillclosetheprogram.DonotforgettosendyourdiaryfileandM-filetoyourTA.
DonotforgettodeleteyourfilesfromtheharddiskofthePCyouusedinthelabattheendofthelabsession.
四川师范大学MATLAB语言实验报告2
系 级 班 年 月 日
实验名称:
Programming,RelationalandLogicalExpressions
姓名 学号 指导教师 成绩
1Objective
TheobjectiveofthislabistofamiliarizeyouwiththeMATLABscriptfiles(M-files),subarrays,relationalandlogicaloperators.
2ScriptFiles
ScriptfilesarecollectionsofMATLABstatementsthatarestoredinafile.InsteadoftypingcommandsdirectlyintheCommandWindow,aseriesofcommandsmaybeplacedintoafileandtheentirefilemaybeexecutedbytypingitsnameintheCommandWindow.SuchfilesarecalledscriptfilesthatarealsoknownasM-filesbecausetheyhaveanextensionof.m.Whenascriptfileisexecuted,theresultisthesameasitwouldbeifallofthecommandshadbeentypeddirectlyintotheCommandWindow.AllcommandsandscriptfilesexecutedintheCommandWindowshareacommonworkspace,sotheycanallsharevariablesintheworkspace.Notethatiftwoscriptfilesareexecutedsuccessively,thesecondscriptfilecanusethevariablescreatedbythefirstscriptfile.Inthisway,scriptfilescancommunicatewithotherscriptfilesthroughthedataleftbehindintheworkspace.AnEditWindowisusedtocreatenewM-filesortomodifyexistingones.TheEditWindowisaprogrammingtexteditor,withthefeaturesofMATLABlanguagehighlightedindifferentcolors.YoucancreateanewM-filewiththeFile/New/M-fileselectionandyoucanopenanexistingM-filewiththeFile/OpenselectionfromthedesktopmenuofMATLAB.
(1)Createanewworkingdirectoryunderthecurrentdirectoryandchangethecurrentdirectoryto‘TA’ssuggest’.
3Subarrays
ItispossibletoselectandusesubsetsofMATLABarrays.Toselectasubsetofanarray,justincludealistoftheelementstobeselectedintheparenthesesafterthearrayname.MATLABhasaspecialfunctionnamedendthatisusedtocreatearraysubscripts.Theendfunctionalwaysreturnsthehighestvaluetakenonbyagivensubscript.Itisalsopossibletousesubarraysontheleft-handsideofanassignmentstatementtochangeonlysomeofthevaluesinanarray.Ifvaluesareassignedtoasubarray,onlythosevaluesarechangedbutifvaluesareassignedtoanarray,theentirecontentsofthearrayarereplacedbythenewvalues.
(1)Definethefollowing5x5arrayarr1inMATLAB.
(2)WriteaMATLABstatementtoselectasubsetofarr1andreturnthesubarraycontainingthevaluesasshown.
(3)WritetwoMATLABstatementstoselectthelastrowandlastcolumnofarr1,separately.
(4)WriteMATLABstatementstoobtainthefollowingarrayfromarr1.
4RelationalandLogicalOperators
Relationalandlogicaloperatorsarethetwotypesofoperatorsthatproducetrue/falseresultsinMATLABprograms.MATLABinterpretsazerovalueasfalseandanynonzerovalueastrue.Relationaloperators(==,=,>,>=,<,<=)areoperatorswithtwooperandsthatproduceeitheratrue
(1)orafalse(0)result,dependingonthevaluesoftheoperands.Relationaloperatorscanbeusedtocompareascalarvaluewithanarray.Theycanalsobeusedtocomparetwoarraysortwostringsonlyiftheyhavethesamesize.Becarefulnottoconfusetheequivalencerelationaloperator(==)withtheassignmentoperator(=).Logicoperators(&,|,xor,~)areoperatorswithoneortwooperandsthatyieldalogicalresultsuchas0or1.Therearethreebinarylogicoperators:
AND(&),OR(|),andexclusiveOR(xor);andoneunaryoperator:
NOT(~).Inthehierarchyofoperations,logicoperatorsareevaluatedafterallarithmeticandrelationaloperatorshavebeenevaluated.Theoperatorisevaluatedbeforeotherlogicoperators.
(1)Definethefollowing4x5arrayarr4inMATLAB.
(2)Writeanexpressionusingarr4andarelationaloperatortoproducethefollowingresult.
(3)Writeanexpressionusingarr4andarelationaloperatortoproducethefollowingresult.
(4)WriteaMATLABprogramwhichwillgeneratean(n-1)x(n-1)matrixfromagivennxnmatrixwhichwillbeequaltogivenmatrixwithfirstrowandfirstcolumndeleted.
(5)Generalizeyourprogramabovesothattheprogramshouldasktherowandcolumnnumberstobedeletedandthengeneratenew(n-1)x(n-1)matrix.
(6)Quiz3.1
5QuittingMATLAB
Typingquitonthecommandwindowwillclosetheprogram.DonotforgettosendyourdiaryfileandM-filetoyourTA.
DonotforgettodeleteyourfilesfromtheharddiskofthePCyouusedinthelabattheendofthelabsession.
四川师范大学MATLAB语言实验报告3
系 级 班 年 月 日
实验名称:
BranchesandLoops,LogicalArrays.
姓名 学号 指导教师 成绩
1Objective
TheobjectiveofthislabistofamiliarizeyouwiththeMATLABBranchesandLoops,LogicalArrays.
2Exercises
Donotforgettoaddsufficientdocumentationandproperindentationtoallprogramsyouwrite.
(1)Writeaprogramthatcalculatesfollowequationwithforandwhileloop,andwriteaprogramwithoutloop.
(2)Writeaprogramthatacceptsavectorofintegersasinputandcountsthenumberofintegersthataremultiplesof3inthatvector.Youcanassumethattheinputcontainsonlyintegervalues.Anexamplerunofyourprogramcanbeasfollows:
Enteravectorofintegers:
[1328056]
Thenumberofmultiplesof3is2
(3)Therootmeansquareisawayforcalculatingameanforasetofnumbers.Thermsaverageofaseriesofnumbersisgivenas:
Writeaprogramthatwillacceptanarbitrarynumberofinputvaluesandcalculatethermsaverageofthenumbers.Theprogramshouldasktheuserforthenumberofvaluestobeentered.Testyourprogramwith4and10setofnumbers.
(4)3.8exercises:
3.4
(5)4.6Exercises:
4.14.2
3QuittingMATLAB
Typingquitonthecommandwindowwillclosetheprogram.Donotforg
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- MATLAB 实验 报告