Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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.4

(44)

94 documents

1 / 7

Toggle sidebar

Related documents


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~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 ike 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~,ltiplyE'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; 1 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. %' 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); ~vill not tlisplay t,hc 3 x 3 itlcnt,it,y matrix. %' Use t,hc up-arron7 cursor t,o rct,nrn t,o prcvio~~s cornmantls. 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~vays a scrrlicolorl i)ctn~ccn ro~vs) A = [ 1 % 3 also produces the rrlat,rix A i~i i t is harder to t,ypc 4 5 61 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 ~vi th 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 To write your own scripts or fiinct,ions; yon have t,o crcatc a nc~v t,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,ll any 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,s of t,llc filc will he cxccntcd. For an rn-file t,o he n filrlct,ion it rrlllst st,art ~vitll tllc ~vord f~~,ract%orr follo~vcd by t,hc oiit,p~lt ~rariablcs in 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,o a t,cxt filc nnrrlcd rn~llt,.rn Then 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~ltlcd as an outpiit varinhlc. Tllc comrrlands arc follo~vcd by ;so t,llat t,llcy will not he prirlt,ctl to t,llc II.ITL.4B ~vindo~v It is ~lscfill \vllcn cvcry t,imc t,llcy arc cxcciit,cd. 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,s ix rii 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~lcs and 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. Keeping a diary of your work The corrlmnrltl diary('fi1e') tells II.ITL.IB to record cvcryt,lling tlonc in t,hc RI.ITL.4B n~intlo\~~, Typing diary on or diary off and save t,hc rcs~llt,s in t,llc text filc narncd 'file'. t,ogglcs the rccortling. Old tliary filcs car1 bc vic~vcd iising a t,cxt ctlit,or, or printed iising lp7 in iinix. In RI.4TL.IB; t,llcy car1 he vic\~.ctl usirlg t,llc type ,file cornrnand. Saving your variables and matrices The corrlmnrltl diary snvcs the corrlmnrltls you typed as nrcll as II.ITL.IB3s oiit,p~lt,, i~ i i t it docs not save t,hc corlt,crlt of your varinhlcs and rnat,riccs. Thcsc ~rariablcs can he listcd by t,llc comrrland whos nrhich also lists t,hc sizes of t,llc rnat,riccs. The cornrnand save 'xxx' will save t,hc rnatriccs and all vnriablcs listcd by t,hc whos comrrland irlt,o t,hc filc rlamctl , , , . II.ITL.IB labcls tllcsc filcs ~vit,ll a .mat cxt,cnsion inst,cad of .m ~vhich arc script,s or fiinctions. :1;:1;:1;.rno,t filcs can bc rend by RI.4TL.IB at a later t,irrlc by t,yping load :1;:1;:1;. Graphics The sirrlplcst cornrnand is plot(.^, :r/) ~vhich 11scs two xrcct,ors rc; nrltl y of t,llc samc lcngt,h. Tllc point,s (rc;,, yi) will i)c plot,tcd and conncct,cd by solid lines. If no xrcct,or .T is given, hI.ITLAB assnrncs t,llat rc;(i)= I . Then plot(r/)has cqnnl spacing on t,llc .T-axis: the point,s arc (i; r/(i)). Tllc t,ypc and color of the line bct,~vccn point,s can i)c chnrlgcd by a third argiimcnt,. Tllc dcfn~llt ~vit,ll no argiimcrlt is a solid black line '' " . Use help plot for Inany opt,ions. n7c indi- cnt,c only a few: RI.ITL.4B 5: plot(:rr.11.'r+ : I ) plot,s in r. = rctl \\,it11 + for points nrltl tlot,t,cd line Yo11 can orrlit t,llc lirlcs nrltl plot only t,llc discrete point,s in diffcrcrlt ways: plot(rc;,r/;'ol)gives circles. Ot,hcr opt,ions arc '+' or ' x ' or '*' For t,wo graphs on t,llc samc axcs use plot(rc;;9 ;X. Y). Rcplncc plot by loglog or semilogy or semilogx t,o chnrlgc orlc or bot,h axcs t,o lognrit,hrrlic scale. Tllc comrrland 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~cl t,hc ?;-axis or t,hc ?/-axis, put t,llc tlcsircd label in q~lot,cs as 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
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved