ENGG1801 Matlab Study Notes PDF

Title ENGG1801 Matlab Study Notes
Author as afss
Course Engineering Computing
Institution University of Sydney
Pages 16
File Size 508.3 KB
File Type PDF
Total Downloads 55
Total Views 127

Summary

all notes...


Description

ENGG1801‐MATLABStudyNotes Week4:MATLABBasics,selectionstatementsandArrays  a) MATLABBasics  Commandwindowisgoodforquickcalculationsandtoexperimentideas,butyourworkisgone whenyouexittheMATLABprogram.  WriteaMATLABscripttosaveyourcodesinaM‐file.Thisfilecanbeexecutedonanothercomputer thathasMATLABinstalled.  Workspacewindowliststhenamesandvaluesofyourlocalvariables.  Writecommentsinyourcodestoexplainyourcodestoanotherprogrammerwhoisreadingyour file.  MATLABcontainsmanyin‐builtfunctionsthatyouusebygivingtherightinputdatae.g.sqrt(x).  Typehelp inthecommandwindowforinformationonhowtousethe function.Ex:help sqrt  AMATLABprogramexecutessequentially–codes/commandatLine1,codes/commandatLine2, codes/commandatLine3etc. MATLAB %  clc  clear  ; =

Description Percentsignindicatesacomment.CommentsarenotexecutedbyMATLAB. Clearsthecommandwindow Clearstheworkspace Semi‐colon.Suppressesoutputfromdisplayinginthecommandwindow. Assignmentoperator.Assignsavaluetoamemorylocation,notthesameasequality. Righthand‐side(RHS)isthevalue.Left‐hand‐sideisthememorylocation/variable name.Ex:age = 5. Calculatesthesquarerootofx. DefaultvariablenameforresultsofMATLABcalculations.

sqrt(x) ans  b) Selectionstatements  Selection statements allows the programmer to execute one command (or a set of commands) dependingontheconditions.  Conditionscontainbothrelationalandlogicaloperators.Returnsalogicaldatatypeafterevaluation.  Relationaloperatorscomparevalues. Relational Operators

= == ~=

Morethanorequalto Equalto Notequalto        

Description



Logicaloperatorsareusedtocombinecomparisonstatements.

LogicalOperators && || ~

Description AND.ReturnsTRUEonlyifALLstatementsareTRUE.Otherwisereturns FALSE. OR.ReturnsFALSEonlyifALLstatementsareFALSE.Otherwisereturns TRUE. NOT.~TRUE=FALSE.~FALSE=TRUE.

 Threetypesofselectionstatements: o ifstatement‐performsanactioniftheconditionistrueelseskiptheactionifconditionis false. o if…else statement‐performsanactioniftheconditionistrueelseperformsadifferent actioniftheconditionisfalse. o elseif statement‐performsanactioniftheconditionsaretrueelsechecksanother conditionifitistrue.  Cannestanynumberofifstatementsbuteachifstatementmusthaveanendkeyword.  c) Arrays  One‐dimensionalarrayiscalledavector.  Vectorscanbecreatedby: o Enteringthevaluesdirectly.Ex:a = [1 3 5 7 9] o Enteringthevaluesasarangeofnumbers.Ex:a = 1:2:9 o Usinglinspacefunctiontocreateafixnumberofelementsinthevectorbetweentwo values.Ex:a = linspace(1,9,5)  Determinelengthofavectorusingthefunctionlength(a)wherea isavector. Function length() actuallyreturnsthemaximumdimensionofanarray.   a(k) ‐Accesselementatindex‐kinvectora.  a(end)‐Accessthelastelementinvectora.  Two‐dimensionalarraysmusthavethesamenumberofcolumnsforeachrowandsamenumberof rowsforeachcolumn.Ex: A=[1,2,3;4,5] %Error.Firstrowhasthreecolumns,secondrowhas2columns B=[1,2,3;4,5,6] %Initializesa2x3array.  Determinethenumberofrowsandcolumnsinatwo‐dimensionalarrayusingthefunction size(a). Ex:[r, c] = size(a)  isempty(a)–Returnstrueifarraya isanemptyarrayelsereturnsfalse.                

 Week5:RepetitionStatements(Loops)    

Allowsagroupofstatementstobeexecutedmultipletimes. Numberoftimesaloopisexecuteddependsonthecounterortheevaluationofalogicalcondition. Twodifferenttypesofloops:forloopandthewhileloop. forloop: 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 Onceaforloopiscompleted,variableimaintainsthelastvalue.



o Useforloopwhenyouknowhowmanytimestorepeattheloop. o Commonlyusedtoaccesselementsinanarray. whileloop: o Syntax: %initialize variable (counter) to control the loop. count = 1 %count condition to terminate/stop loop while count...


Similar Free PDFs