Você está na página 1de 28

MATLABTutorial1

AydanGasimova
SPGTA
MedicalPhysicsandBiomedicalEngineering
aydan.gasimova.10@ucl.ac.uk

WhydoweneedMATLAB?
MATLABisamathematicaltool.TherearemanyusesofMATLAB,e.g.
Mathandcomputation
Algorithmdevelopment
Modelling,simulation,andprototyping
Dataanalysis,exploration,andvisualization
Scientificandengineeringgraphics
Applicationdevelopment,includingGraphicalUserInterfacebuilding

Someexamples Datavisualisation
2Dand3Dplots:

Someexamples DataAnalysis
Filteringnoisydata:

Someexamples ImageProcessing
Letterdetection

Edge
detection

Texture
transfer

Firstthingsfirst
Whatarewetryingtoachieve?
Goals:
1.Plotagraphofasimplefunction,suchasy=ax +b,and
addaxislabelsandtitle.
2.Plotagraphofdatapointsandfitanappropriatepolynomial
curvethroughthedata.

Task1
OpenMATLAB.Youshouldseethefollowing:
CurrentFolder
Yourcurrent
workingdirectory

Workspace
Exploredatathat
youcreateor
importfromfiles

Command
Window
Terminal,enter
commandsatthe
commandline,
indicatedbythe
prompt(>>).

Task2
Today,wewillbeworkingonlyusingthecommandline.
Createsomevariablesandassignthemvaluestoseehowtheyare
displayedandstored.
e.g.
>>a=1
(5+10)*cos(0.2)
>>b=15;
>>c=cos(a)

Task2
Youshouldseethefollowing:
Nosemicolon resultis
displayed

Variablenames
andvaluesare
nowstoredinthe
Workspace

Useofsemicolon
supressesoutputdisplay,
butresultisstillstored

Task3
Wecanalsoassignvariablesasarrays andperformfunctionsonthe
array.
Createthefollowingarray:
>>x=[1,2,3,4,5,6,7,8,9,10]

Task3
Wecannowperformafunctiononthearray,e.g.
>>y=x+10
>>z=5*x

Task4
YoucanuseMATLABshelptooltolookupalloftheinbuilt functions.
e.g.
>>helpplot

Task4
Nowwecanplotasimplelinearfunction.
Createafunction,e.g.
>>y=5*x+10
PlotusingtheMATLABsfunctionplot:
>>plot(x,y);

Task4
Youshouldseethefollowingfigure:
Addlabels:
>>xlabel('voltage')
>>ylabel('current')
>>title('VoltageCurrentResponse')

Task4
Labelledfigure:

UsefulFunctions
>>clc
>>clearall

%Clearsthecommandwindow(doesntdelete
data).
%Deletesallthevariablesintheworkspace.

Inaddition,youcanrecallpreviouscommandsbypressingtheup and
downarrowkeys,and.Pressthearrowkeyseitheratanempty
commandlineorafteryoutypethefirstfewcharactersofacommand.

Task5
Nowwecanplotmultiplelinesononegraphandcreatelegends.
Createasecondfunction,e.g.
>>z=x.^2+2*x+5
>>plot(x,y)
>>xlabel(x);ylabel(y);title(FunctionPlot);
>>holdon
%holdcurrentgraphproperties
>>plot(x,z)
>>legend('y=5x+10','y=x^2+2x+5')
>>holdoff
%returntodefaultmode

Quicknote
MATLABtreatsallthearraysasvectors.Thismeansthatwhenyouperforma
functiononanarray,youareperformingitonthevector,andnotindividual
numbersinthearray.
i.e.Ifx=[1,2,3,4,5],thenx*x orx^2 =[1,2,3,4,5]*[1,2,3,4,5]
MATLABwillthrowanerrorsinceitistreatingx asavector,andtheinner
dimensionsofthevectorsmustagreeforthemtomultiply.
Togetthesquareofindividualnumbersinthearray,simplyputa.infrontofthe
function:
>>z=x.^2+2*x+5

Task5
Figurewithtwoplotsandlegend:

Task6
WecanalsouseMATLABtoplotourowndatapoints,notjust
functions.
e.g.
>>height=[1.54,1.57,1.65,1.70,1.78,1.90,2.00];
>>weight=[52,59,65,70,72,81,87];
>>plot(height,weight);
>>xlabel(Height,(m));
>>ylabel(Weight(kg));
>>title(HeightvsWeightofStudents);

Task6
Figurewithourowndata:

Task7
Wecanalsoplotdatausingdifferentmarkers andcolours:
>>plot(height,weight,*);
>>plot(height,weight,r)

UsefulSpecifiers

Task8.1
Wecanfitastraightlinethroughthedataasfollows:
OntheFigureWindowSelect:
ToolsBasicFitting:

Task8.1
Wecanfitastraightlinethroughthedataasfollows:
OntheBasicFittingWindow:
CheckLinear
Showequation:
ThenPressClosebutton

Task8.1
Basicstraightlinefitthroughthedata:
Equationof
fittedline

LinearFit

Task8.2
Usethesamestepstofitacubicpolynomialcurveinstead:
Equationof
fittedcubic
polynomial
curve

cubic
polynomial
fit

Task9
Trytouseeverythingyouhavelearnedtoplotandlabelthefollowingdata,
includingfittingapolynomialcurveofappropriatedegreethroughthedata:
TemperaturerecordedeverytwohoursinLondonandDubaionthe17th
September,2015.
Time

6:00

8:00

10:00

12:00

14:00

16:00

18:00

20:00

22:00

00:00

Temp.inLondon 11
(C)

12

14

16

18

18

17

16

14

13

Temp.inDubai
(C)

29

33

37

40

37

34

32

30

28

26

Você também pode gostar