实验二MATLAB基础知识二.docx
- 文档编号:24750926
- 上传时间:2023-06-01
- 格式:DOCX
- 页数:11
- 大小:26.79KB
实验二MATLAB基础知识二.docx
《实验二MATLAB基础知识二.docx》由会员分享,可在线阅读,更多相关《实验二MATLAB基础知识二.docx(11页珍藏版)》请在冰豆网上搜索。
实验二MATLAB基础知识二
Experiment1.FundamentalKnowledgeofMatlab(II>
【ExperimentalPurposes】
1、熟悉并掌握MATLAB的工作环境。
2、运行简单命令,实现数组及矩阵的输入输出,了解在MATLAB下如何绘图。
【ExperimentalPrinciple】
1.Vectors
Let'sstartoffbycreatingsomethingsimple,likeavector.Entereachelementofthevector(separatedbyaspace>betweenbrackets,andsetitequaltoavariable.Forexample,tocreatethevectora,enterintotheMATLABcommandwindow(youcan"copy"and"paste"fromyourbrowserintoMATLABtomakeiteasy>:
b5E2RGbCAP
a=[123456987]
MATLABshouldreturn:
a=
123456987
Togenerateaseriesthatdoesnotusethedefaultofincrementingby1,specifyanadditionalvaluewiththecolonoperator(first:
step:
last>.InbetweenthestartingandendingvalueisastepvaluethattellsMATLABhowmuchtoincrement(ordecrement,ifstepisnegative>betweeneachnumberitgenerates.Togenerateavectorwithelementsbetween0and20,incrementingby2(thismethodisfrequentlyusedtocreateatimevector>,usep1EanqFDPw
t=0:
2:
20
t=
02468101214161820
Manipulatingvectorsisalmostaseasyascreatingthem.First,supposeyouwouldliketoadd2toeachoftheelementsinvector'a'.Theequationforthatlookslike:
DXDiTa9E3d
b=a+2
b=
34567811109
Nowsuppose,youwouldliketoaddtwovectorstogether.Ifthetwovectorsarethesamelength,itiseasy.Simplyaddthetwoasshownbelow:
RTCrpUDGiT
c=a+b
c=
468101214201816
Subtractionofvectorsofthesamelengthworksexactlythesameway.5PCzVD7HxA
MATLABsometimesstoressuchalistinamatrixwithjustonerow,andothertimesinamatrixwithjustonecolumn.Inthefirstinstance,sucha1-rowmatrixiscalledarow-vector。
inthesecondinstance,sucha1-columnmatrixiscalledacolumn-vector.Eitherway,thesearemerelydifferentwaysforstoringvectors,notdifferentkindsofvectors.jLBHrnAILg
2.MatricesandArrays
ThemostbasicMATLABdatastructureisthematrix:
atwo-dimensional,rectangularlyshapeddatastructurecapableofstoringmultipleelementsofdatainaneasilyaccessibleformat.Thesedataelementscanbenumbers,characters,logicalstatesoftrueorfalse,orevenotherMATLABstructuretypes.MATLABusesthesetwo-dimensionalmatricestostoresinglenumbersandlinearseriesofnumbersaswell.Inthesecases,thedimensionsare1-by-1and1-by-nrespectively,wherenisthelengthofthenumericseries.MATLABalsosupportsdatastructuresthathavemorethantwodimensions.ThesedatastructuresarereferredtoasarraysintheMATLABdocumentation.xHAQX74J0X
MATLABisamatrix-basedcomputingenvironment.AllofthedatathatyouenterintoMATLABisstoredintheformofamatrixoramultidimensionalarray.Evenasinglenumericvaluelike100isstoredasamatrix(inthiscase,amatrixhavingdimensions1-by-1>:
LDAYtRyKfE
A=100。
NameSizeBytesClass
A1x18doublearray
EnteringmatricesintoMATLABisthesameasenteringavector,excepteachrowofelementsisseparatedbyasemicolon(。
>orareturn:
Zzz6ZB2Ltk
B=[1234。
5678。
9101112]
B=
1234
5678
9101112
B=[1234
5678
9101112]
B=
1234
5678
9101112
Thesquarebracketsoperatorconstructstwo-dimensionalmatricesonly,(including0-by-0,1-by-1,and1-by-nmatrices>.dvzfvkwMI1
MatricesinMATLABcanbemanipulatedinmanyways.Forone,youcanfindthetransposeofamatrixusingtheapostrophekey:
rqyn14ZNXI
C=B'
C=
159
2610
3711
4812
ItshouldbenotedthatifChadbeencomplex,theapostrophewouldhaveactuallygiventhecomplexconjugatetranspose.Togetthetranspose,use.'(thetwocommandsarethesameifthematixisnotcomplex>.EmxvxOtOco
NowyoucanmultiplythetwomatricesBandCtogether.Rememberthatordermatterswhenmultiplyingmatrices.SixE2yXPq5
D=B*C
D=
3070110
70174278
110278446
D=C*B
D=
107122137152
122140158176
137158179200
152176200224
Anotheroptionformatrixmanipulationisthatyoucanmultiplythecorrespondingelementsoftwomatricesusingthe.*operator(thematricesmustbethesamesizetodothis>.6ewMyirQFL
E=[12。
34]
F=[23。
45]
G=E.*F
E=
12
34
F=
23
45
G=
26
1220
Ifyouhaveasquarematrix,likeE,youcanalsomultiplyitbyitselfasmanytimesasyoulikebyraisingittoagivenpower.kavU42VRUs
E^3
ans=
3754
81118
Ifwantedtocubeeachelementinthematrix,justusetheelement-by-elementcubing.y6v3ALoS89
E.^3
ans=
18
2764
Youcanalsofindtheinverseofamatrix:
X=inv(E>
X=
-2.00001.0000
1.5000-0.5000
oritseigenvalues:
eig(E>
ans=
-0.3723
5.3723
Thereisevenafunctiontofindthecoefficientsofthecharacteristicpolynomialofamatrix.The"poly"functioncreatesavectorthatincludesthecoefficientsofthecharacteristicpolynomial.M2ub6vSTnP
p=poly(E>
p=
1.0000-5.0000-2.0000
Rememberthattheeigenvaluesofamatrixarethesameastherootsofitscharacteristicpolynomial:
0YujCfmUCw
roots(p>
ans=
5.3723
-0.3723
3.Functions
Tomakelifeeasier,MATLABincludesmanystandardfunctions.Eachfunctionisablockofcodethataccomplishesaspecifictask.MATLABcontainsallofthestandardfunctionssuchassin,cos,log,exp,sqrt,aswellasmanyothers.Commonlyusedconstantssuchaspi,andiorjforthesquarerootof-1,arealsoincorporatedintoMATLAB.eUts8ZQVRd
sin(pi/4>
ans=
0.7071
Todeterminetheusageofanyfunction,typehelp[functionname]attheMATLABcommandwindow.MATLABevenallowsyoutowriteyourownfunctionswiththefunctioncommand。
followthelinktolearnhowtowriteyourownfunctionsandseealistingofthefunctionswecreatedforthistutorial.sQsAEJkW5T
4.Plotting
ItisalsoeasytocreateplotsinMATLAB.Supposeyouwantedtoplotasinewaveasafunctionoftime.Firstmakeatimevector(thesemicolonaftereachstatementtellsMATLABwedon'twanttoseeallthevalues>andthencomputethesinvalueateachtime.GMsIasNXkA
t=0:
0.25:
7。
y=sin(t>。
plot(t,y>
Theplotcontainsapproximatelyoneperiodofasinewave.BasicplottingisveryeasyinMATLAB,andtheplotcommandhasextensiveadd-oncapabilities.Iwouldrecommendyouvisittheplottingpagetolearnmoreaboutit.TIrRGchYzg
5.Polynomials
InMATLAB,apolynomialisrepresentedbyavector.TocreateapolynomialinMATLAB,simplyentereachcoefficientofthepolynomialintothevectorindescendingorder.Forinstance,let'ssayyouhavethefollowingpolynomial:
7EqZcWLZNX
ToenterthisintoMATLAB,justenteritasavectorinthefollowingmannerlzq7IGf02E
x=[13-15-29]
x=
13-15-29
MATLABcaninterpretavectoroflengthn+1asannthorderpolynomial.Thus,ifyourpolynomialismissinganycoefficients,youmustenterzerosintheappropriateplaceinthevector.Forexample,zvpgeqJ1hk
wouldberepresentedinMATLABas:
y=[10001]
Youcanfindthevalueofapolynomialusingthepolyvalfunction.Forexample,tofindthevalueoftheabovepolynomialats=2,NrpoJac3v1
z=polyval([10001],2>
z=
17
Youcanalsoextracttherootsofapolynomial.Thisisusefulwhenyouhaveahigh-orderpolynomialsuchas1nowfTG4KI
Findingtherootswouldbeaseasyasenteringthefollowingcommand。
fjnFLDa5Zo
roots([13-15-29]>
ans=
-5.5745
2.5836
-0.7951
0.7860
Let'ssayyouwanttomultiplytwopolynomialstogether.Theproductoftwopolynomialsisfoundbytakingtheconvolutionoftheircoefficients.MATLAB'sfunctionconvthatwilldothisforyou.tfnNhnE6e5
x=[12]。
y=[148]。
z=conv(x,y>
z=
161616
Dividingtwopolynomialsisjustaseasy.Thedeconvfunctionwillreturntheremainderaswellastheresult.Let'sdividezbyyandseeifwegetx.HbmVN777sL
[xx,R]=deconv(z,y>
xx=
12
R=
0000
Asyoucansee,thisisjustthepolynomial/vectorxfrombefore.Ifyhadnotgoneintozevenly,theremaindervectorwouldhavebeensomethingotherthanzero.V7l4jRB8Hs
Ifyouwanttoaddtwopolynomialstogetherwhichhavethesameorder,asimplez=x+ywillwork(thevectorsxandymusthavethesamelength>.Inthegeneralcase,theuser-definedfunction,polyaddcanbeused.Tousepolyadd,copythefunctionintoanm-file,andthenuseitjustasyouwouldanyotherfunctionintheMATLABtoolbox.Assumingyouhadthepolyaddfunctionstoredasam-file,andyouwantedtoaddthetwounevenpolynomials,xandy,youcouldaccomplishthisbyenteringthecommand:
83lcPA59W9
z=polyadd(x,y>
x=
12
y=
148
z=
1510
【Experimentcontentsandprocedure】
1、运行简单命令,实现数组及矩阵的输入输出;
2、运行特殊矩阵的输入及输出;
MATLABhasanumberoffunctionsthatcreatedifferentkindsofmatrices.SomecreatespecializedmatricesliketheHankelorVandermondematrix.Thefunctionsshowninthetablebelowcreatematricesformoregeneraluse.mZkklkzaaP
Function
Description
ones
Createamatrixorarrayofallones.
zeros
Createamatrixorarrayofallzeros.
eye
Createamatrixwithonesonthediagonalandzeroselsewhere.
accumarray
Distributeelementsofaninputmatrixtospecifiedlocationsinanoutputmatrix,alsoallowingforaccumulation.
diag
Createadiagonalmatrixfromavector.
magic
Createasquarematrixwithrows,columns,anddiagonalsthatadduptothesamenumber.
rand
Createamatrixorarrayofuniformlydistributedrandomnumbers.
randn
Createamatrixorarrayofnormallydistributedrandomnumbersandarrays.
randperm
Createavector(1-by-nmatrix>containingarandompermutationofthespecifiedintegers.
Mostofthesefunctionsreturnmatricesoftypedouble(double-precisionfloatingpoint>.However,you
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 MATLAB 基础知识
![提示](https://static.bdocx.com/images/bang_tan.gif)