实验二MATLAB基础知识二.docx

上传人:b****3 文档编号:11513886 上传时间:2023-06-01 格式:DOCX 页数:11 大小:26.79KB
下载 相关 举报
实验二MATLAB基础知识二.docx_第1页
第1页 / 共11页
实验二MATLAB基础知识二.docx_第2页
第2页 / 共11页
实验二MATLAB基础知识二.docx_第3页
第3页 / 共11页
实验二MATLAB基础知识二.docx_第4页
第4页 / 共11页
实验二MATLAB基础知识二.docx_第5页
第5页 / 共11页
实验二MATLAB基础知识二.docx_第6页
第6页 / 共11页
实验二MATLAB基础知识二.docx_第7页
第7页 / 共11页
实验二MATLAB基础知识二.docx_第8页
第8页 / 共11页
实验二MATLAB基础知识二.docx_第9页
第9页 / 共11页
实验二MATLAB基础知识二.docx_第10页
第10页 / 共11页
实验二MATLAB基础知识二.docx_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验二MATLAB基础知识二.docx

《实验二MATLAB基础知识二.docx》由会员分享,可在线阅读,更多相关《实验二MATLAB基础知识二.docx(11页珍藏版)》请在冰点文库上搜索。

实验二MATLAB基础知识二.docx

实验二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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2