Professional Documents
Culture Documents
Lecture1
Lecture1
GettingMatlabtorun
Onyourownlaptop...you'reonyourown...
OnAthenatype(withouttheathena%prompt)
athena%addmatlab
athena%matlab
Ittakesabout3060secstostartup...andyou'llgetascreen
showing:
<MATLAB>
Copyright19842006TheMathWorks,Inc.
Version7.2.0.283(R2006a)
January27,2006
Togetstarted,typeoneofthese:helpwin,helpdesk,demo,
helphelp,whatsnew,info
ForAthenaspecificinformation,type:helpathena
Forproductinformation,visitwww.mathworks.com
TostarttheMatlabDesktop,type:desktop
>>desktop
Alternatively,youcantype
athena%addmatlab
athena%matlabdesktop
Programming
Themostawfultruthaboutcomputersisthattheyarealldumb.Everythingmustbeexplicitly
told.Spellingoutthemostminorandobviousdetail.Otherwise,youwilleithergetasyntax
error(computernotunderstandingwhatyouaresaying)oraprogrammingerror(thecomputer
understandingwhatyouaresayingbutitisdifferentfromwhatyoumean).
Therearemanyprograminglanguages.WhyuseMatlab?Asafirstprogramminglanguage,it
isgreat,ithassimplesyntax,itiseasytogetstartedwithandeasytodographics.Whileother
programminglanguagesarefasterrunningtheyareslowertolearnandtouse.
Inaddition,Matlab,asaninterpretedlanguage,isinteractive,whichallowsformore
exploratoryprogramming.Oncetheexplorationisdone,oneshouldconsiderrewritingthe
programinadifferentlanguageifintensiveuseisrequired.
http://ocw.mit.edu/ans7870/resources/farjoun/Lecture1.html
1/3
9/7/2015
Lecture1
TheCommandPrompt
IntheinteractiveterminalofMatlabyouwillseethecommandprompt(>>).ThisisMatlab's
wayoftellingyouthatitisreadytoreceiveinstructionsfromyou.TocommandMatlab,type
yourrequestandhitEnter.Matlabthenevaluatesyourcommandanddisplaystheoutput(if
any).
Togethelpusethehelp,andlookforcommands.helpshouldbeusedtofindout
informationaboutaknowncommand,e.g.helpzeros.
lookforcanbeusedtofindacommandtoperformataske.g.lookforroots.
Ifyoudonotgetthecommandpromptforsomereason,youmayhavetypedasyntaxerror,or
haveputMatlabintoaninfiniteloop.Don'tpanic!pressCtrlCandyou'llgetthepromptback.
Exercises
Learnaboutthecommandsones,zeros,sum,anddiag.
Learnaboutmagicandverifywhatyoulearnedwithsum.
SimpleExpressions
Matlab'smostbasicbuildingblocksareexpressions:1,1+1,4*3,4^2,5/6.
ButMatlab'srealstrengthisinhandlingmatricesandvectors.Toenteravectorweuse
brackets.Trythefollowingexamplestoseewhathappens:[12],[1:10],[1,23
456,789101112]
Noticethatthecolonisusedtocreateanarithmeticsequence.Wecanalsocreateasequence
withastepsizedifferentfrom1:[4:0.1:5],[5:2:5].
Totransposeamatrix(orvector)usetheapostrophe('):
>>[1:10]'
Exercises
TrytosolvethesewithasingleMatlabcommand:
Createthevectorconsistingofthewholeevennumbersbetween15and27.
Createthevectorconsistingofthewholeoddnumbersbetween15and27.
Createthevectorofthepreviousquestionindecreasingorder.
Variables
AskingMatlabtocalculatethingsforusisgreat,butmostofthetimesacalculationisonly
onestepoutofmanyandtheresultofacalculationneedtobesavedforthe(verynear)future.
Todothisweuse'variables',whicharesimplyawaytonameabitofMatlabdata.Thisisbest
explainedbyexample:
>>a=1
http://ocw.mit.edu/ans7870/resources/farjoun/Lecture1.html
2/3
9/7/2015
Lecture1
putsthevalue1intoavariablecalleda.typing
>>a
resultsbackinthevalue1.
Wecanalsoassignacalculationintoavariable:
>>b=1+1
andnowbwillcontaintheanswer,2.
Variablenamescan(andshould)belongerthanoneletter.Awordoracoupleofwordsstrung
togethertendstoworkthebest.
Referencingmatrixelements
Wecanaccesstheelementsofamatrixinthefollowingmanner:
>>a=[14356]
>>a(4)
>>b=[1234]
>>b(2,2)
Wherethenumbersintheparenthesisare(row,column).
http://ocw.mit.edu/ans7870/resources/farjoun/Lecture1.html
3/3