MATLAB Tutorial: Learning Linear Algebra and Matrix Operations, Exams of Art

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

2021/2022

Uploaded on 09/27/2022

oliver97
oliver97 🇺🇸

4.5

(44)

323 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB
Tutorial
Yoii ncctl n srrlall niimbcr of l~asic corrlmnrltls 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,ll thcrn. Those arc all short high-lcvcl cornmnntls, bccn~lsc hI.ITL.4B
constantly ~vorks nrit,h rnat,riccs.
I
bclicvc t,llat yon ~vill like t,llc pon7cr that this soft,\~~arc gives,
t,o (lo linear algebra by a scrics of short inst,rnct,ions:
creixte
E
creixte
u,
claarage
E
n),'i~,ltiply
E'II
E
=
cyc(3)
,(I,
=
E
(:.
1)
E(3.1)
=
5
v=E*u,
The ~vord cyc st,ands for the idcntit,y mnt,rix. The s~lhrnatrix
u,
=
E(:,
1)
picks oiit colnrnn
1.
The irlstr~lct,ion E(3,
1)
=
5
rcscts t,llc (3,
1)
entry to
5.
Tllc corrlmnrltl
E
*
,ir
rrliiltiplics the
mnt,riccs
E
and
11,.
.I11
t,hcsc corrlmnrltls arc rcpcnt,ctl in our list bclo~v. Hcrc is an cxnrnplc
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
t,llc invcrsc mnt,rix
(normally
in dccirnals; for fractions iisc fornrixt rat). Tllc syst,crn .4:1;
=
11
is solrctl by
rc;
=
inv(A)
*
h;
~vhich is t,hc slow \~~ay. 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
side
11
cqiials the t,llirtl colnrnn of ,4; t,llc sol~lt,ion
:I;
rrlllst bc
[0
0
11'.
(Tlre tro,nspose symbol
I
nri~kes
:I;
11
col?rrran 'i,ector.) Then
A
*
:I;
picks out t,hc third colnrnn of .4; and nrc have
A?;
=
11.
Hcrc arc
a
fcxv comrncnt,~. The corrlmcrlt syrnl~ol is
%':
%'
Tllc syrnl)ols
rr
and .4 arc iii,fererat: hI.ITL.4B is case-scnsit,ivc
%'
Type lrelp .slo,,sh for n dcscript,ion of ho\v t,o nsc the bnckslash syrnl)ol. Tllc word lrelp
can i)c follo~vcd by
a
hI.ITLAIB syrnhol or cornrnand narnc or hl-file name.
pf3
pf4
pf5

Partial preview of the text

Download MATLAB Tutorial: Learning Linear Algebra and Matrix Operations and more Exams Art in PDF only on Docsity!

MATLAB Tutorial

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

constantly ~vorksnrit,h rnat,riccs. I bclicvc t,llat yon ~villlike t,llc pon7crthat this soft,~~arcgives,

t,o (lo linear algebra by a scrics of short inst,rnct,ions:

creixte E creixte u, claarage E n),'i~,ltiplyE'II

E = cyc(3) ,(I, = E(:. 1) E(3.1) = 5 v = E * u ,

The ~vordcyc st,ands for the idcntit,y mnt,rix. The s~lhrnatrix u, = E(:,1) picks oiit colnrnn 1.

The irlstr~lct,ionE(3, 1) = 5 rcscts t,llc (3, 1) entry to 5. Tllc corrlmnrltl E * ,ir rrliiltiplics the

mnt,riccs E and 11,. .I11 t,hcsc corrlmnrltls arc rcpcnt,ctl in our list bclo~v.Hcrc is an cxnrnplc

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

t,llc invcrsc mnt,rix (normally in dccirnals; for fractions iisc fornrixt rat). Tllc syst,crn .4:1; = 11

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

side 11 cqiials the t,llirtl colnrnn of ,4; t,llc sol~lt,ion:I; rrlllst bc [0 0 11'. (Tlre tro,nspose symbol I

nri~kes:I; 11 col?rrran 'i,ector.) Then A * :I; picks out t,hc third colnrnn of .4; and nrc have A?; = 11.

Hcrc arc a fcxv comrncnt,~.The corrlmcrlt syrnl~olis %':

%' Tllc syrnl)ols rr and .4 arc iii,fererat: hI.ITL.4B is case-scnsit,ivc

%' Type lrelp .slo,,sh for n dcscript,ion of ho\v t,o nsc the bnckslash syrnl)ol. Tllc word lrelp

can i)c follo~vcdby a hI.ITLAIBsyrnhol or cornrnand narnc or hl-file name.

%' To tlisplay all 16 digit,s type fofol.rno,t lorag. Tllc norrnal fofol.rno,t slrort gives 4 digits aft,cr

the dccirnal.

%' .I scrrlicolon aft,cr a cornrnantl avoids tlisplay of t,llc rcs~llt,.

.4 = oncs(3); ~villnot tlisplay t,hc 3 x 3 itlcnt,it,ymatrix.

%' Use t,hc up-arron7 cursor t,o rct,nrn t,o prcvio~~scornmantls.

How to input a row or column vector

11,= [2 4 51 has one row nrit,h t,llrcc corrlponcnts (a^1 x^ 3 rnat,rix)

v = [2; 4; 51 has t,llrcc rows scparat,cd by scrrlicolons (a 3^ x^1 rnat,rix)

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)

A = [ 1 % 3 also produces the rrlat,rix^ A^ i ~ i i tis harder to t,ypc

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

ones(n) givcs an rr x rr rrlat,rix of orlcs

Numbers and matrices associated with A

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

null(.4) is a mat,rix ~rllosc r , - r. colnrnns arc an orthogonal basis for the n~lllspaccof A

o r t h ( A ) is a mat,rix ~rllosc r. coliirrlrls arc an orthogonal basis for the colurrln space of.

Examples

E = cyc(4); E(%,l)= 3 crcat,cs a 4 x 4 clcmcnt,ary climirlat,ion mat,rix

E*.4 siil)t,mct,s3 t,irrlcs row 1 of A fiorn row 2.

B = [ A h] creates the allgmcrlt,ctl mat,rix ~ v i t h 11 as cxt,ra colnrnn

E = cyc(3); P = E([% 1 31. :) crcat,cs a pcrmntation mat,rix

Xot,c that t,riu(A) + t,ril(A) - tliag(tliag(.4)) equals A

Built-in M-files for matrix factorizations (all important!)

[ L ,C: PI = lu(.4) g i ~ c ytllrcc matriccy ~ v i t hP.4 = LU

[S,El = eig(A) gives a diagonal cigcnxi~,lncrnatrix E arltl cigcnvcct,or matrix S ~vit,llA S =

SE. If A is not diagona1izal)lc (too few cigcnvcct,ors) then S is not invcrtiblc.

[Q.R] = qr(A) gi~rcsan rn x r r , orthogonal mat,rix Q arltl r r , x rr t,riangiilar R ~ v i t hA = QR

Creating M-files

h1-files arc text files ending ~vit,ll.rnnrhich hI.4TL.4B iiscs for fimct,ions ant1 scripts. -4 script,

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,

cdit,ctl and savctl ~vit,llany t,cxt cdit,or, likc erraacs. EZ. or i~i. .I script filc is simply n list of

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

mnt,rix .4 and rct,urn only t,llc matrix prodiict C. Tllc vnrial~lc r is not rctiirrlcd i)ccaiisc it

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)

% Tlais firrrction ,finrls tlae rixrrk, eigerr~i~ixl~i~,r,e,sixrrii eigeravectors o f 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.

axis ([n 11 (: d l ) will scale the graph t,o lie in t,llc rcct,arlglc n 5 rc; 5 h ; c 5 Z/ 5 rl. To title the

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