lecture 01: getting started - nc state universityscroggs/ma591matlab/lecturenotes/l01.pdf ·...

38
Lecture 01: Getting Started Goal: start using Matlab as quickly as possible Goal: understand the way the course is run 8/19/10 1 Intr Matlab for FM, Fall 2010

Upload: doanphuc

Post on 31-Aug-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Lecture01:GettingStarted

Goal:startusingMatlabasquicklyaspossible

Goal:understandthewaythecourseisrun

8/19/10 1IntrMatlabforFM,Fall2010

Page 2: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Today

1.  StartMatlab2.  Touroftheinterface3.  Rundemo

4.  UseMatlabasacalculator5.  Scriptfiles6.  Functionfiles

8/19/10 2IntrMatlabforFM,Fall2010

Page 3: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

StartingMatlab1.ChooseAdminLauncher2.ClickonStartMATAB

1

2

8/19/10 3IntrMatlabforFM,Fall2010

Page 4: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

MatlabEnvironment

8/19/10 4IntrMatlabforFM,Fall2010

Page 5: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

MATLABopenstoadefaultwindowconfiguration

8/19/10 5IntrMatlabforFM,Fall2010

Page 6: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

•  MATLABusesastandardwindowsmenubar

•  ToexitMATLABusethecloseicon

8/19/10 6IntrMatlabforFM,Fall2010

Page 7: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Command Window Enter commands at the prompt Current Directory

MATLABWindowsCommand History

Workspace Window

8/19/10 7IntrMatlabforFM,Fall2010

Page 8: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

8/19/10 IntrMatlabforFM,Fall2010 8

Page 9: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Windows

1.  Command2.  CommandHistory

3.  Workspace

4.  CurrentDirectory5.  Editor6.  Figure

8/19/10 9IntrMatlabforFM,Fall2010

Page 10: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Youcanusethecommandwindowmuchlikeyou’duseacalculator

Thestandardorderofoperationrulesapply

8/19/10 10IntrMatlabforFM,Fall2010

Page 11: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

•  Similartoascratchpad•  Onceyouhitenter,youcan’teditanycommands

•  Youcanretypethemorusethearrowkeystoretrievecommandsandeditthembeforehittingenteragain

CommandWindow

8/19/10 11IntrMatlabforFM,Fall2010

Page 12: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

CommandHistory

•  Recordsthecommandsyouissueinthecommandwindow

•  Whenyouexitthecommandwindow,orwhenyouissuetheclccommand,thecommandwindowiscleared

•  Butthecommandhistoryremains

CommandHistory

8/19/10 12IntrMatlabforFM,Fall2010

Page 13: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

CommandHistory

•  Youcantransfercommandsfromthecommandhistorytothecommandwindow– Doubleclickonacommand– Clickanddrag

8/19/10 13IntrMatlabforFM,Fall2010

Page 14: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

RepeatthesecalculationsbydraggingthemfromtheCommandHistory

1.  squarerootof652.  sin(31)3.  (55+17)/3

8/19/10 14IntrMatlabforFM,Fall2010

Page 15: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

WorkspaceWindow

•  Keepstrackofthevariablesyou’vedefined–  Name–  Value–  Class–  Size–  Bytes

•  Youmayneedtoclickonthenamebarandselectsizeandbytesinordertoseetheseparameters

WorkspaceWindow

8/19/10 15IntrMatlabforFM,Fall2010

Page 16: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

8/19/10 16IntrMatlabforFM,Fall2010

Page 17: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Whenyoudefinevariablesinthecommandwindow,theyarelistedintheworkspacewindow

8/19/10 17IntrMatlabforFM,Fall2010

Page 18: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Definethefollowingvariables

Aisthevalueofthesquarerootof65Bisthevalueofsin(31)

Cisthevalueof(55+17)/3

8/19/10 18IntrMatlabforFM,Fall2010

Page 19: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

DocumentWindow

•  IfyoudoubleclickonanyvariableintheworkspacewindowMATLABlaunchesadocumentwindowcontainingthearrayeditor

•  Youcaneditvariablesinthearrayeditor

8/19/10 19IntrMatlabforFM,Fall2010

Page 20: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

DocumentWindow

8/19/10 20IntrMatlabforFM,Fall2010

Page 21: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

CurrentDirectoryWindow

•  Aviewofthefilesandfoldersofthecurrentworkingdirectory

8/19/10 21IntrMatlabforFM,Fall2010

Page 22: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

CurrentDirectory

•  Thecurrentdirectorywindowisalistoffiles

•  Whenyoutrytoloadinformationfromafileortrytosaveinformation–MATLABusesthecurrentdirectory

8/19/10 22IntrMatlabforFM,Fall2010

Page 23: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

FigureWindow

•  WhenFiguresarecreatedanewwindowopens

•  It’sextremelyeasytocreategraphsinMATLAB

8/19/10 23IntrMatlabforFM,Fall2010

Page 24: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Thesemicolonsuppressestheoutputfromeachcommand

8/19/10 24IntrMatlabforFM,Fall2010

Page 25: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

8/19/10 25IntrMatlabforFM,Fall2010

Page 26: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

EditingWindow

•  Thiswindowallowsyoutotypeandsaveaseriesofcommandswithoutexecutingthem

•  Thereareseveralwaystoopenaneditingwindow– Fromthefilemenu

– Withthenewfileicon

8/19/10 26IntrMatlabforFM,Fall2010

Page 27: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Openaneditingwindowfromthefilemenuorwiththenewfileicon

8/19/10 27IntrMatlabforFM,Fall2010

Page 28: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

SaveandRun

Writeyourcodeintheeditingwindow,thenrunitusingtheSaveandRunicon

8/19/10 28IntrMatlabforFM,Fall2010

Page 29: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

OrganizeFolders&RunDemo

•  Keepalltheworkforasingleassignmentinitsownfolder

•  Everybodyshouldhaveacopyoffolderncm

•  Donotworkinfolderncm

•  Makenochangestofilesinfolderncm

•  Createfoldertemp0fortoday’swork

8/19/10 IntrMatlabforFM,Fall2010 29

Page 30: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

OrganizeFolders&RunDemoCreatefoldertemp0andsetupto

workinit

8/19/10 IntrMatlabforFM,Fall2010 30

1.  Clickonellipses

2.createfoldertemp0

Page 31: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

OrganizeFolders&RunDemoCreatefoldertemp0andsetupto

workinit

8/19/10 IntrMatlabforFM,Fall2010 31

Note:foldertemp0isatthesamelevelasfolderncm

Forthisclassallworkingfolderswillbeatthesamelevelasfoldertemp0

Page 32: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

OrganizeFolders&RunDemoCreatefoldertemp0andsetupto

workinit

8/19/10 IntrMatlabforFM,Fall2010 32

Page 33: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

OrganizeFolders&RunDemoCreatefoldertemp0andsetupto

workinit

8/19/10 IntrMatlabforFM,Fall2010 33

Page 34: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Whatyouneedduringclass1.  Laptopcomputer2.  withMatlabinstalled3.  thathasabrowserinterfacingwiththe

website

TheNCSUlaptopsmeettheseconditions

Ifyoubringyourownlaptop,yourMatlabinterfacemaybedifferent.

8/19/10 34IntrMatlabforFM,Fall2010

Page 35: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Beforeclassstarts1.  Startyourlaptop2.  StartMatlab3.  Movetothecurrentworkingdirectory4.  Setthepathsothesamplefileswillexecute

DuringClass1.  Followalongusingyourlaptop2.  Interactwithyourfellowstudents3.  Askquestionsoftheinstructor4.  Whenitgetstooloud

8/19/10 35IntrMatlabforFM,Fall2010

Page 36: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

ScriptFilesandFunctions

Classexercise–createandtestscriptfilep3.m

•  Runthecommandsonpages3and4ofthetextbookintheCommandWindow

•  Openfilep3.mwiththeeditor•  CopythecommandsfromtheCommandHistoryWindowtofilep3.m

•  Runthescriptfilep3.m

Workwiththepeoplearoundyoutoaccomplishthis

Page 37: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

Comments

•  Besuretocommentyourcodesoweknowwhatishappening.

•  Best:usemeaningfulvariablenames

The % sign starts the comments

Page 38: Lecture 01: Getting Started - Nc State Universityscroggs/ma591matlab/lecturenotes/L01.pdf · Lecture 01: Getting Started ... folder ncm For this

nexttime:control‐flow

1.  IF‐THEN‐ELSEIF‐ELSE2.  FOR‐END3.  WHILE‐END4.  SWITCH‐CASE

8/19/10 38IntrMatlabforFM,Fall2010