



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive tutorial on using MATLAB for linear algebra and matrix operations. It covers creating vectors and matrices, clearing the workspace, performing basic linear algebra operations, and using various built-in MATLAB functions. It also explains how to input matrices, create special matrices, and manipulate submatrices.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Yoii ncctl n srrlall niimbcr of l~asiccorrlmnrltls to st,art using II.ITL.IB. This short t,ut,orial dcscribcs those fimdnrncntal commands. Yo11 nccd t,o create vectors and mat,riccs. t,o claarage t,llcm, and t,o opemte ~vit,llthcrn. Those arc all short high-lcvcl cornmnntls, bccn~lschI.ITL.4B
t,o (lo linear algebra by a scrics of short inst,rnct,ions:
The irlstr~lct,ionE(3, 1) = 5 rcscts t,llc (3, 1) entry to 5. Tllc corrlmnrltl E * ,ir rrliiltiplics the
of inverting n rrlat,rix and solving a linear syst,cm:
The rnatrix of all ones urns ntltlcd t,o cyc(3). and h is its t,llirtl colnrnn. Then inv(.4) prodiiccs
is solrctl by rc; = inv(A) * h ; ~vhichis t,hc slow \ ~ ~ a y .Tllc bnckslash comrnand rc; = A\b uses Gaiissian clirnination if .4 is sqiiarc and rlcvcr cornpiit,cs the invcrsc rnat,rix. 1l:hcn the right
nri~kes:I; 11 col?rrran 'i,ector.) Then A * :I; picks out t,hc third colnrnn of .4; and nrc have A?; = 11.
can i)c follo~vcdby a hI.ITLAIBsyrnhol or cornrnand narnc or hl-file name.
the dccirnal.
%' .I scrrlicolon aft,cr a cornrnantl avoids tlisplay of t,llc rcs~llt,.
How to input a row or column vector
How to input a matrix (a row at a time)
A = [l 2 3; 4 5 61 has t,wo rows (al~vaysa scrrlicolorl i)ctn~ccnro~vs)
How to create special matrices
diag(v) prodnccs t,llc diagonal mat,rix nrit,h vcct,or v on it,s diagonal
toeplitz(l1) givcs the syrnrnctric corastarat-rliagorrcrl matrix ~ v i t h 11 as first row and first col- 1111111
toeplitz(lr:, 11) givcs the const,ant,-tliagoml matrix nrit,h ,I(! as first coliimn and 1: as first row
det(A) is t,llc drternriraarat (if A is a sqllarc rnat,rix)
rank(.4) is the ro,nk (nnrnbcr of r)ivot,s = dirrlcnsion of row space and of colllrrln space)
size(A) is the pair of nnrnbcrs [ r r , n]
trace(.4) is t,llc trace = slim of diagonal cnt,rics = snrn of cigcnvaliics
o r t h ( A ) is a mat,rix ~rllosc r. coliirrlrls arc an orthogonal basis for the colurrln space of.
is a scqiicncc of comrrlands ~vhichmay be cxcciit,cd oft,cn. arltl can bc placctl in an m-file so t,llc corrlmarltls (lo not have to i)c rct,ypctl. II.4TL.4B's dcrrlos arc cxamplcs of t,llcsc script,^. -4n cxarnplc is the tlcmo called lao?rsr. hIost of h1.4TL.4B3s filrlct,ions arc act,nally rn-files. and car1 be vic~vcdby \vrit,ing type xxx nrhcrc :1;:1;:1; is t,llc narnc of t,llc fiinct,ion.
To write your own scripts or fiinct,ions; yon have t,o crcatc a n c ~ vt,cxt filc nrit,h any name yon likc, proritlcd it ends nrit,h .m; so RI.4TL.IB will rccognizc it,. Text files can bc created,
hI.4TL.4B commands. TVhcn tllc filc nnrrlc is t,ypctl at t,hc RI.4TL.4B prompt,, t,llc corlt,crlt,sof t,llc filc will he cxccntcd. For an rn-file t,o he n filrlct,ion it rrlllst st,art ~vitlltllc ~vord f~~,ract%orr follo~vcdby t,hc oiit,p~lt~rariablcsin brackct,~,t,hc filrlct,ion nnrnc, and t,llc irlput ~rariablcs.
Examples
firrrction [C]=nr'i~,lt(A) ~=rixrrk(A); C = d ' * A ;
Save t,llc above comrrlands i~lt,oa t,cxt filc nnrrlcd rn~llt,.rnThen this fiint,ion will t,nkc n
urns not incl~ltlcdas an outpiit varinhlc. Tllc comrrlands arc follo~vcdby ;so t,llat t,llcy will not he prirlt,ctl to t,llc II.ITL.4B ~vindo~vcvcry t,imc t,llcy arc cxcciit,cd. It is ~lscfill\vllcn dealing nrit,h lnrgc rnat,riccs. Hcrc is nrlot,llcr example:
firrrction [V,D,r]=liropertie.s(A)
[nr,ra]=,size(A); if m==n [V,D]=eig(A); ~=rixrrk(A); else dis1ij'E~ror:Tlre nrixtri:~;rrairst be sq~rixri:');
Hcrc tllc fiinctiorl takes tllc mnt,rix A as input nrltl only rctiirrls two rrlat,riccs and t,hc rank as o~lt,pnt. Tllc 96 is used as a cornmcnt,. Tllc fiinctiorl cllccks to scc if t,llc irlput rna- t,rix is sqnnrc and t,hcrl finds t,llc mnk, cigcnval~lcsand cigcrlvcctors of n rrlat,rix A. Typing liroperties(A) only rct,urns t,llc first out,pnt. V. t,hc rrlat,rix of cigcnvcctors. Yoii ~rliist,type [V,D,r]=pmpwtie,s(A) t,o get all t,llrcc o~lt,pnts.
graph or lal~clt,hc ?;-axis or t,hc ?/-axis, put t,llc tlcsircd label in q~lot,csas in tllcsc cxamplcs:
t i t l e ('hcight of sat,cllit,c') .xlahel ('time in seconds') glahel ('hcight in rnct,crs3)
Tllc comrrland hold kccps t,hc cnrrcnt graph as yo11 plot a ncxv gmpll. Repeating hold will clear t,llc screen. To print. or save t,hc graphics n~intlox~~in a file. scc lrelp p r i n t or 11sc print -Pprint,cmarnc print -d filcnarrlc