Title | ENGG1801 Matlab Study Notes |
---|---|
Author | as afss |
Course | Engineering Computing |
Institution | University of Sydney |
Pages | 16 |
File Size | 508.3 KB |
File Type | |
Total Downloads | 55 |
Total Views | 127 |
all notes...
ENGG1801‐MATLABStudyNotes Week4:MATLABBasics,selectionstatementsandArrays a) MATLABBasics Commandwindowisgoodforquickcalculationsandtoexperimentideas,butyourworkisgone whenyouexittheMATLABprogram. WriteaMATLABscripttosaveyourcodesinaM‐file.Thisfilecanbeexecutedonanothercomputer thathasMATLABinstalled. Workspacewindowliststhenamesandvaluesofyourlocalvariables. Writecommentsinyourcodestoexplainyourcodestoanotherprogrammerwhoisreadingyour file. MATLABcontainsmanyin‐builtfunctionsthatyouusebygivingtherightinputdatae.g.sqrt(x). Typehelp inthecommandwindowforinformationonhowtousethe function.Ex:help sqrt AMATLABprogramexecutessequentially–codes/commandatLine1,codes/commandatLine2, codes/commandatLine3etc. MATLAB % clc clear ; =
Description Percentsignindicatesacomment.CommentsarenotexecutedbyMATLAB. Clearsthecommandwindow Clearstheworkspace Semi‐colon.Suppressesoutputfromdisplayinginthecommandwindow. Assignmentoperator.Assignsavaluetoamemorylocation,notthesameasequality. Righthand‐side(RHS)isthevalue.Left‐hand‐sideisthememorylocation/variable name.Ex:age = 5. Calculatesthesquarerootofx. DefaultvariablenameforresultsofMATLABcalculations.
sqrt(x) ans b) Selectionstatements Selection statements allows the programmer to execute one command (or a set of commands) dependingontheconditions. Conditionscontainbothrelationalandlogicaloperators.Returnsalogicaldatatypeafterevaluation. Relationaloperatorscomparevalues. Relational Operators
= == ~=
Morethanorequalto Equalto Notequalto
Description
Logicaloperatorsareusedtocombinecomparisonstatements.
LogicalOperators && || ~
Description AND.ReturnsTRUEonlyifALLstatementsareTRUE.Otherwisereturns FALSE. OR.ReturnsFALSEonlyifALLstatementsareFALSE.Otherwisereturns TRUE. NOT.~TRUE=FALSE.~FALSE=TRUE.
Threetypesofselectionstatements: o ifstatement‐performsanactioniftheconditionistrueelseskiptheactionifconditionis false. o if…else statement‐performsanactioniftheconditionistrueelseperformsadifferent actioniftheconditionisfalse. o elseif statement‐performsanactioniftheconditionsaretrueelsechecksanother conditionifitistrue. Cannestanynumberofifstatementsbuteachifstatementmusthaveanendkeyword. c) Arrays One‐dimensionalarrayiscalledavector. Vectorscanbecreatedby: o Enteringthevaluesdirectly.Ex:a = [1 3 5 7 9] o Enteringthevaluesasarangeofnumbers.Ex:a = 1:2:9 o Usinglinspacefunctiontocreateafixnumberofelementsinthevectorbetweentwo values.Ex:a = linspace(1,9,5) Determinelengthofavectorusingthefunctionlength(a)wherea isavector. Function length() actuallyreturnsthemaximumdimensionofanarray. a(k) ‐Accesselementatindex‐kinvectora. a(end)‐Accessthelastelementinvectora. Two‐dimensionalarraysmusthavethesamenumberofcolumnsforeachrowandsamenumberof rowsforeachcolumn.Ex: A=[1,2,3;4,5] %Error.Firstrowhasthreecolumns,secondrowhas2columns B=[1,2,3;4,5,6] %Initializesa2x3array. Determinethenumberofrowsandcolumnsinatwo‐dimensionalarrayusingthefunction size(a). Ex:[r, c] = size(a) isempty(a)–Returnstrueifarraya isanemptyarrayelsereturnsfalse.
Week5:RepetitionStatements(Loops)
Allowsagroupofstatementstobeexecutedmultipletimes. Numberoftimesaloopisexecuteddependsonthecounterortheevaluationofalogicalcondition. Twodifferenttypesofloops:forloopandthewhileloop. forloop: o Syntax: for i = array %i must be a variable, array usually a vector %body of loop %commands to execute end %i changes to the next element in array o Onceaforloopiscompleted,variableimaintainsthelastvalue.
o Useforloopwhenyouknowhowmanytimestorepeattheloop. o Commonlyusedtoaccesselementsinanarray. whileloop: o Syntax: %initialize variable (counter) to control the loop. count = 1 %count condition to terminate/stop loop while count...