11.07.2015 Views

MatlabNotes

MatlabNotes

MatlabNotes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15.6 Building MatricesIt is often convenient to build large matricesfrom smaller ones:>> C=[0 1; 3 -2; 4 2]; x=[8;-4;1];>> G = [C x]G =0 1 83 -2 -44 2 1>> A, B, H = [A; B]A =5 7 91 -3 -7B =-1 2 59 0 5H =5 7 91 -3 -7-1 2 59 0 5so we have added an extra column (x) toC inorder to form G and have stacked A and B ontop of each other to form H.>> J = [1:4; 5:8; 9:12; 20 0 5 4]J =1 2 3 45 6 7 89 10 11 1220 0 5 4>> K = [ diag(1:4) J; J’ zeros(4,4)]K =1 0 0 0 1 2 3 40 2 0 0 5 6 7 80 0 3 0 9 10 11 120 0 0 4 20 0 5 41 5 9 20 0 0 0 02 6 10 0 0 0 0 03 7 11 5 0 0 0 04 8 12 4 0 0 0 0The command spy(K) will produce a graphicaldisplay of the location of the nonzero entries inK (it will also give a value for nz—the numberof nonzero entries):>> spy(K), gridThe keyword end can also be used with multidimensionalarraysK(1:2,end-1:end)ans =3 47 815.7 Tabulating FunctionsThis has been addressed in earlier sections butwe are now in a position to produce a moresuitable table format.Example 15.1Tabulate the functions y = 4sin3x and u =3 sin 4x for x =0, 0.1, 0.2,...,0.5.>> x = 0:0.1:0.5;>> y = 4*sin(3*x); u = 3*sin(4*x);>> [ x’ y’ u’]ans =0 0 00.1000 1.1821 1.16830.2000 2.2586 2.15210.3000 3.1333 2.79610.4000 3.7282 2.99870.5000 3.9900 2.7279Note the use of transpose (’) to get columnvectors. (we could replace the last commandby [x; y; u;]’)We could also have done this more directly:>> x = (0:0.1:0.5)’;>> [x 4*sin(3*x) 3*sin(4*x)]15.8 Extracting Parts of MatricesWe may extract sections from a matrix in muchthe same way as for a vector (page 6).Each element of a matrix is indexed accordingto which row and column it belongs to. Theentry in the ith row and jth column is denotedmathematically by A i,j and, in Matlab,by A(i,j). So>> JJ =1 2 3 45 6 7 820

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!