Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng...

52
Matlab I: Ge+ng Started Division of Sta7s7cs and Scien7fic Computa7on Instructor: Yla Tausczik ([email protected])

Transcript of Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng...

Page 1: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabI:Ge+ngStarted

DivisionofSta7s7csandScien7ficComputa7on

Instructor:YlaTausczik([email protected])

Page 2: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons
Page 3: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Agenda

Session1

–  MatlabBackground•  WhatisMatlab?History•  MatlabaccessatUT

–  MatlabGUIandNaviga7on–  Commandlineprac7ce–  Impor7ngdata–  Expor7ngData

–  Break

Session2

–  Matlabnota7on

–  Syntaxandopera7ons–  Surveyoffunc7onality

•  Func7ons,computa7ons

•  M‐files,programming

•  Simpleplo+ngprocedures

•  Trivialsimula7on

–  Q&A

Page 4: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Session1

•  Introduc7on,history,accessatUT

•  Ge+ngfamiliarwiththeinterface–  Layout–  Pulldownmenus–  Windowpanels

•  Crea7ngandmanipula7ngobjects–  Scalars,vectors,matrices,textstrings

•  Datatransport–  Impor7ngintotheworkspace–  Expor7ngfromtheworkspace

Page 5: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

SlidesandFiles

h[p://homepage.psy.utexas.edu/homepage/students/Tausczik/Yla/

•  PowerpointSlides•  PlanetFiles

Page 6: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

WhatisMatlab?

•  MATLABderivedfromMATrixLABoratory – Applica7onkernel,e.g.,matlab‐7.6

•  Computa7onalengine

•  Built‐inlowlevelalgorithms(nosourcecode)

•  Includesavarietyofgraphicalcapabili7es

– Suitewithcontemporaryreleases,e.g.R2008a

– Programminglanguage

Page 7: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

WhatisMatlab?

•  Designedtosolveproblemsnumerically–  Par7cularlygoodformatrixopera7ons

–  Usesfinite‐precisionarithme7c–  Typicallyproducesapproximatedigitalsolu7ons

•  Notdesignedassymboliccomputa7onsocware–  DifferentfromMaple,Mathema7ca

•  Easytolearnanduse–  SimpliersyntaxrulesthanFortranorC

–  Shorterdevelopment7meforprogramming

Page 8: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

WhatisMatlab?

•  Auxiliaries–usuallybundledwithkernel– Simulink(dynamicsystemsmodeling)– Stateflow(eventandlogic‐drivenmodeling)

•  Toolboxes‐designedforspecificapplica7ons– Specializedcollec7onsofMATLABsourcefiles

•  DevelopedorsupportedbyMathWorksInc.–  SignalProcessing,Op7miza7on,ControlSystem,etc.

•  Downloadablethirdpartyfreeware– Mul7pleprecisionarithme7c,robo7cs,etc.

Resource:h1p://www.mathworks.com/matlabcentral/

Page 9: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

BriefHistoryofMatlab

•  Originallywri[eninFortran– Author:CleveMoler,Univ.ofNewMexico

•  Mathworks,Inc.foundedtofurtherdevelopit.–  Incorporatedin1984– Coreiswri[eninC– Graphicalinterfaceiswri[eninJava– Runsonmanyplakorms

•  Unix–Solaris,SGI,AIX,DigitalUnix•  Linux•  Windows•  MacOS

Page 10: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

AccesstoMatlabatUT‐Aus7n

•  StudentMicrocomputerFacility(SMF)–  SecondflooroftheFlawnAcademicCenter(FAC212)–  MusthaveaUTEIDoranITS‐issuedaccount

h1p://www.utexas.edu/its/account/index.html

•  CNSPCLab(ACA1.126)learningandinstruc=onuseonly•  CNSMacLab(ACA1.124)licensedoesnotpermitresearch

•  SSCservers–  MatlabisinstalledontheSSCWindowsTerminalServer–  ToaccessMatlabontheseserversyoumusthaveUTdiskspace

•  $5peryearforUTstudentsandstaff

h1p://ssc.utexas.edu/so?ware/stat‐apps‐server

Page 11: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

AccesstoMatlabatUT‐Aus7n

•  ComputerStore(FAC109firstfloor)– Studentversion,musthaveastudentID– Fullfunc7onality,modestcost($97)

•  PersonallicensefromMathWorksInc.– Specialacademicpricing– Studentversionwithdiscountpricing($99)

•  h1p://www.mathworks.com/store/index.html

Page 12: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

DefaultMatlab7DesktopGUI

Page 13: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

GUILayout

•  Windowstypeinterface

– Topnaviga7onbarwithpull‐downmenus•  File•  Edit•  Debug•  Desktop•  Window•  Help

– Toolbarwithicons

Page 14: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Naviga7onBar

•  File– New(createnewm‐file,figure,variable)–  ImportData

– SetPath(folders,directoriestosearch)– Preferences→CommandWindow

(numberformat,scrollbuffersize)

•  Edit

– Find,Delete,Copy,Paste,etc.

Page 15: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Naviga7onBar

•  Desktop– Chooselayoutfordisplayofwindows– Modifydefaultifdesired

•  Help– LaunchMatlabHelpWindow

– Demos

Page 16: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

DesktopNaviga7on

•  FixedPlaceWindows(undockable)– TheCommandWindowrightsidepanel– TheWorkspaceWindowupperle?panel

– TheCurrentDirectoryWindowupperle?panel– TheCommandHistoryWindowlowerle?panel

•  Floa7ngWindows(dockable)– TheHelpWindow– TheFigureWindow

– TheEditorWindow

Page 17: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

CommandWindowOpera7ons

•  Execu7ngcommandsatcommandline– Crea7ngobjects

>> a = 2

– Modifyingobjects >> a = a + 1

– Usingu7li7es(e.g., >> save a

•  Runningprogramscripts(m‐files)

Page 18: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Crea7ngObjects‐Rules

•  Everyobjectisamatrix– Ascalarisa1x1matrix– Arowvectorisa1xnmatrix– Acolumnvectorisanmx1matrix

•  Everymatrixelementhasanumericalvalue–  Non‐digitelementslinkedtoASCIIcodedigits

A→65,B→66,etc.a→97,b→98,etc.‐→45,_→95,etc

–  Logicaltruthvaluesassignedtodigits(true→1,false→0)

Page 19: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Crea7ngObjects‐Rules

•  Squarebrackets[]areobjectdelimiters–  Impliedforscalars,butcanbepresent

•  Example:6or[6]–  Impliedforcharacterstringrowvectors

•  Example:'matlab'or['matlab']

•  Acolon:istheseparatorforranges–  Syntaxis<boundary>:<spacing>:<boundary>–  boundaryandspacingcanbeintegerorfrac7onal–  spacingcanbeincrementalordecremental–  []delimiterisimplied–  defaultstructureisarowvector

–  Examples•  6.32:2.18:83.447or[6.32:2.18:83.447]•  100:‐2:0or[100:‐2:0]

Page 20: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Crea7ngObjects‐Rules

•  Separatorsforcolumnelementscommaor<space>

[1,2]→12

[345]→345

•  Separatorsforrowelementssemicolonor<return>

[6;7]→67

[889→910]10

Page 21: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Crea7ngObjects‐Rules

•  Assigningnames–  Firstcharactermustbeupperorlowercasele[er

•  BasicLa7nle[ersonly(noé,ç,å,ô,ü,ñ…)–  Addi7onalcharacters–le[ers,digits,orunderscore–  Le[ersarecasesensi7ve–  Onlythefirst31charactersrecognized–  Assignmentoperatorisasingle=character

•  Example:my_0bj3cT = 123.456

•  Conven=ons(notenforced)–  Scalar/vectornamesbeginwithalowercasele[er–  Matrixnamesbeginwithanuppercasele[er

Page 22: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

CommandWindowU7li7es

•  >><command>%→'%'symbolstartscommentareaforaline

•  >>clear%clearsallobjectsinworkspace

•  >>clearxy%clearsvaluesofobjectsxandy

•  >>clc%clearscommandwindowscrollbuffer

•  >>which<filename>%findsfirstoccurrenceinsearchpath

•  >>who%listsallobjectsintheworkspace

•  >><command>;%semicolon→%execute,nodisplayincommandwindow

•  >><control>c%stopsprogramexecu=on

•  >><control>q%stopsexecu=onandexitsMatlab

Page 23: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

CommandWindowPrac7ce

•  Createascalarwithnamea–  Forexample,yourhomefivedigitZIPcode

•  Createarowvectorwithnameb–  Forexample,elementsfromyourphonenumberdigits

•  Createacolumnvectorwithnamec–  Forexampleleapyearsduringyourlife

•  CreatearectangularmatrixwithnameD–  Variablesincolumns–forexamplebirthmonth,day,year–  Casesinrows–forexamplefather,mother

•  Createacharacterstringwithnamee–  Forexample,yourfirstname(needstobeinsinglequotes)

Page 24: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

WorkspaceWindowOpera7ons

•  Determineproper7esofavectorormatrix–  byinspec7on–  usingtoolbarView > Choose Columns

•  Openanobject(vector,matrix)intheArrayEditor–  Changethevalueofanelement

•  Makeasimpleplotofaworkspaceobject(vectorormatrix)–  Lineplot,stemplot,pie(objectmusthavenumericalelements)

•  CreateanewmatrixobjectandassignavalueintheArrayEditor–  Lecmostworkspacetoolbaricon–  Re‐nametheobjectfromthedefaultnameunnamedtoF–  ConfirmaccesstonewobjectintheCommandWindow

Page 25: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Impor7ngData

•  Directassignmentfromkeyboard

•  ImportWizard–  File‐>ImportData–  >>uiimport

Example:>> uiimport planetsize.txt

•  dlmread('filename','<delimiter>')Example:>> planets2 = dlmread('planets2.txt', ';')

•  load'filename'Example:>> load 'planets3.txt'

Page 26: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Impor7ngData

•  xlsread:ReadfilesfromExcelExample:>> planets6 = xlsread('planets6.xls')

•  imread:Readgraphicsfile(severalformats)Example:>> planets7 = imread('planets7.jpg'); viewwith:>> imshow(planets7)

•  Otherspecialreadfunc7ons– aviread[aviaudio/visualfiles]– textread[readfromtextfile]

–  fscanf[readbyformat,similartoClanguagefunc7on]

Page 27: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Expor7ngData

•  diary:textfileofcommandwindowoutput >> diary <filename.txt> …. >> diary off

•  save:saveworkspaceobjectsortexttodisk>> save <filename>

•  Binaryfile<filename>.mat

>> save <filename>.txt variablename –ascii -tabs •  Textfile<filename>.txt•  Matrixcolumnelementsseparatedbytabswith-tabs

Page 28: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Session2

•  Matlabnota7on– Syntax– Opera7ons

•  Surveyoffunc7onality– Func7ons,computa7ons– m‐files,programming– Simpleplo+ngprocedures

•  AbrieflookatSimulink

Page 29: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabNota7on

•  Numericalvalues

– Sequenceofbase10digits•  Binary,hexrepresenta7onsarecharacterstrings

– Periodasradixpointforfloa7ngpointnumbers

– Lowercaseeforpowerof10exponent

Example:>>x=123.456e‐07

→x=0.0000123456[not(123.456)*(2.71828,,,)‐7]

Page 30: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabNota7on

•  Complexnumbers–  iandjini7allysettothesquarerootof‐1

Imaginaryparthasiorjappended

(1+1i)isasinglecomplexexpression

(1+1*i)isasumofoneandcurrentvalueofi

•  Matrices– Rectangulararraysofelements

•  Indexedbyrow,thenbycolumn

Example:>> A = [ a(1,1) a(1,2); a(2,1) a(2,2)]

Page 31: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabNota7on

•  Booleanlogicals(Bernoullivariables)–  TruthValues

1fortrue;0forfalse

–  Primarylogicsymbols&logicalAND==logicalequal|logicalinclusiveOR~logicalNOT

–  Compoundlogicsymbols<=>=~=xor(p,q)logicalexclusiveOR

Equivalentto(p&~q)|(~p&q)

Page 32: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabNota7on

•  Separatorsanddelimiters[]squarebrackets‐‐vectorandmatrixdelimiters{}curlybrackets‐‐celldelimiter()parentheses

•  Groupingincompoundexpressions•  Vectorandmatrixelementindices•  Func7onargument

:colon‐‐indexrangeseparator;semicolon‐‐matrixrowseparator<space>matrixcolumnseparator,comma‐‐matrixindex,func7onargumentseparator‐‐rowvectorelementseparator''singlequotes‐‐demarca7onofcharacterstrings

Page 33: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabOpera7ons

•  Arithme7cOpera7ons–precedingperiod– Example–arithme7cmatrixmul7plica7on(.*)– Arithme7cOpera7onsareelementbyelement

a11a12a13b11b12b13c11c12c13

a21a22a23.*b21b22b23=c21c22c23a31a32a33b31b32b33c31c32c33

Note:c23=(a23*b23)

Page 34: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabOpera7ons

•  Matrix/Vectoropera7ons–noprecedingperiod–  Example:matrixmul7plica7on(*)

–  Innerproductof(lecsiderow)with(rightsidecolumn)

a11a12a13b11b12b13c11c12c13

a21a22a23*b21b22b23=c21c22c23a31a32a33b31b32b33c31c32c33

Note:c23=(a21*b13)+(a22*b23)+(a23*b33)

Page 35: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabOpera7ons

•  Valueassignmentopera7on=assignsavaluetotheantecedent<return>acerexpressionassignsvaluetoans

•  Unarymatrixandelementopera7ons.'Matrixtranspose

–  Rowsandcolumnsreversedwithsameordering

'Matrixcomplextranspose–  Signsofimaginarypartsofelementsreversed

.^Raisingmatrixelementstoapower^Raisingamatrixtoapower

Page 36: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

MatlabOpera7ons

•  Binarymatrixandelementopera7ons+matrixorelementaddi7on‐matrixorelementsubtrac7on.*elementbyelementmul7plica7on*matrixmul7plica7on./elementbyelementdivision/rightmatrixdivision

•  sameasrightmul7plica7onbydivisorinverse

\lecmatrixdivision•  Sameaslecmul7plica7onbydivisorinverse

Page 37: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

SurveyofMatlabFunc7onality

 Computa7onandProgramming GraphicsandDataVisualiza7on Introduc7ontoSimulink

Page 38: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Compu7ngandProgramming

•  Computa7ons– Combina7onsofelementaryopera7ons

– Built‐inconstants• Scalars:pi(numericalπ);i,j(√‐1)

• Matrices:zeros(m,n);ones(m,n);eye(m,n)

– Built‐infunc7ons• Numerical:sin;cos;exp;log

• Logical:isprime;isreal

Page 39: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Compu7ngandProgramming

•  Batchcommands(macros)canbeputinscripts

•  Scriptsarecreatedintheeditorwindow File>New>M‐File

•  Scriptsaresavedinm‐fileswitha‘.m’extension

•  Codeissequen7allis7ngofcommandsSameasstepbystepinthecommandwindow

Page 40: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Compu7ngandProgramming

•  Elementaryflowcontrolinscripts

– Condi7onalif..elseif..[elseif..elseif..]else

–  Loopfor

– Condi7onalLoopwhile

Page 41: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Compu7ngandProgramming

•  ifcommand

if{condi=on}commandblock;elseif{condi=on}commandblock;

elseif{condi=on}commandblock;

elsecommandblock;end;

Page 42: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Compu7ngandProgramming

•  forloop

for{indexrange}commandblock;end;

•  whilecondi7onalloop

while{condi=on}commandblock;end;

Page 43: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Compu7ngandProgramming

•  OpenanewM‐filefromFile‐>New

•  Createsomenewobjectsinyourcode–  Ascalarfromafunc7on,e.g.,x=exp(1);–  Aarowvectorrange,e.g.,g=[0:1:100];–  Afunc7onvector,e.g.,h=sin(g./10);–  A3x3func7onmatrix,

•  e.g.,J=log([111213;141516;171819]);–  Aflowcontrolstructure

•  e.g.,ifelementofhisposi7vesquareitsvalueotherwisecubeitsvalue

•  SavetheM‐file,e.g.,asmyfile.m

•  RunmyfileintheCommandWindow

Page 44: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Compu7ngandProgramming

•  Convertyourfunc7onrowvectortoacolumn•  Example:>> k = h.'

•  Makeacompoundexpression•  Example:>>m = x + x.^3 - log(x)

•  Operateonamatrix:•  Example:>>N=inv(J)%producestheinverseofthematrix

Page 45: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

GraphicsandDataVisualiza7on

• Makea2‐Dplot– Example:plot(g,h)

– Customizeplotbyedi7nginFigureWindow• Changeaxisproperty• Changelinestyleandcolor• Changebackgroundcolor• Addaxislabelsand7tle

Page 46: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

GraphicsandDataVisualiza7on

• Makea3‐Dlineplot– Create3same‐lengthvectors,e.g.,

>>p = [0:0.1:10];%rangevector>> q = p./2;%samelengthrangevector>> r = sin(p).*cos(q);%func=onvector

– Plotthe3‐Dcurve–Example:>> plot3(p,q,r)

– Rotatethecurvein3‐Dusingtoolbaricon

Page 47: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

GraphicsandDataVisualiza7on

• Makea3‐Dsurfaceplot– Createamatrixoffunc7onvalues

Example:>> S = (sin(p))'*(cos(q));

– Plotasurfaceofmatrixvalues>> surf(S)%polygonalfacets>> mesh(S)%wiremesh

– Rotatetheplotin3‐Dusingtoolbaricon

Page 48: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Introduc7ontoSimulink

•  Asimula7ontoolfordynamicsystems

•  SimulinklibraryBrowser:– Collec7onofsources,systemmodules,sinks

System Input Output

Page 49: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Introduc7ontoSimulink

•  Openanewmodelwindow– ExpandSimulinkonMatlabStartbu[on– SelectLibraryBrowser– ClickonNewModelicon(upperlec)

•  Draginsources,operators,andsinks– Chooseasource,e.g.,SineWave

•  Editaparameter,e.g.,Amplitude

– Chooseasink,e.g.,Scope– Chooseanoperator,e.g.,ConZnuous‐>DerivaZve

Page 50: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Introduc7ontoSimulink

•  Connectthemodelcomponents– Arrowsfromsourcestooperators• Outputfromonesourcecanbeinputforanother

– Arrowsfromoutputstosinks(e.g.,scopes)

•  Runthesimula7on– OpenScopewindows– ClickontheRunicon– Observethescopedisplay

Page 51: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

ThanksforComing

•  Addi7onalSSCMatlabShortCourses

– MatlabII‐‐Compu7ngandProgramming• Tutorialsec7ons5and6

•  TutorialsOnline– h[p://ssc.utexas.edu/socware/socware‐tutorials#matlab

Page 52: Matlab I: Geng Started - Carnegie Mellon School of ...ylataus/matlab/MatlabI.pdf · Matlab I: Geng Started ... – Scalars, vectors, matrices, text strings ... – Specialized collecons

Survey

h[ps://www.surveymonkey.com/s/LRJ6WJK