matlab1
DESCRIPTION
Tutorial For matlabTRANSCRIPT
Matlab = Matrix Lab. The world around Matlab is a matrix world with matrices and vectors. >> y=7.0 y = 7 >> y=7.0; >> help rand() rand() not found. >> help rand RAND Uniformly distributed pseudorandom numbers. R = RAND(N) returns an N-by-N matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval(0,1). RAND(M,N) or RAND([M,N]) returns an M-by-N matrix. RAND(M,N,P,...) or RAND([M,N,P,...]) returns an M-by-N-by-P-by-... array. RAND returns a scalar. RAND(SIZE(A)) returns an array the same size as A. … >> x=rand(6) ←←←← yield a 66×××× random matrix x = 0.8147 0.2785 0.9572 0.7922 0.6787 0.7060 0.9058 0.5469 0.4854 0.9595 0.7577 0.0318 0.1270 0.9575 0.8003 0.6557 0.7431 0.2769 0.9134 0.9649 0.1419 0.0357 0.3922 0.0462
0.6324 0.1576 0.4218 0.8491 0.6555 0.0971 0.0975 0.9706 0.9157 0.9340 0.1712 0.8235 >>>> x=rand(1,6) ←←←← A single vector with 6 elements in it x = 0.6948 0.3171 0.9502 0.0344 0.4387 0.3816
>> y=magic(3) ←←←← A matrix whose elements (from 1 to 2n ) form same
column, row, and diagonal sums. y = 8 1 6 3 5 7 4 9 2 Notice! sum(y) would give us the row vector 15 15 15.
And ∑∑∑∑====
====
++++========2
1
22
2)1(
)(ni
i
nniysum
>> zeros(4) ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> >> z=eye(5) z =
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 >>>> y=hilb(5) % A Hilbert matrix y = 1.0000 0.5000 0.3333 0.2500 0.2000 0.5000 0.3333 0.2500 0.2000 0.1667 0.3333 0.2500 0.2000 0.1667 0.1429 0.2500 0.2000 0.1667 0.1429 0.1250 0.2000 0.1667 0.1429 0.1250 0.1111 >>>> x=[1 2 3; 4 5 6; 7 8 9] %user designed x = 1 2 3 4 5 6 7 8 9 Again! >> a=[1, 3, 5]; >> b=[7, 8, 9]; >> c=[a b] c = 1 3 5 7 8 9 >> d=[a;b] d = 1 3 5 7 8 9
>> >> a=[eye(2);zeros(2)] ← append these as rows a = 1 0 0 1 0 0 0 0 But >> a=[eye(2) zeros(2)] ← append them as columns a = 1 0 0 0 0 1 0 0 >> Some predefined constants >> pi ans = 3.1416 >> i ans = 0 + 1.0000i >> eps ans =
2.2204e-016 >> Inf ans = Inf >> NaN ans = NaN For instance, >> y=2+3*i y = 2.0000 + 3.0000i >> x=sqrt(y) x = 1.6741 + 0.8960i >> >> format long >> pi ans = 3.141592653589793 >> eps ans =
2.220446049250313e-016 In general, we could tweak formatting in this way: format short fixed point with 4 decimal places (the default) format long fixed point with 14 decimal places format short e scientific notation with 4 decimal places format long e scientific notation with 15 decimal places >> who Your variables are: a ans x y z >>>> whos Name Size Bytes Class Attributes a 4x2 64 double ans 1x1 8 double x 3x3 72 double y 5x5 200 double z 5x5 200 double >> % functions >> a= magic(6) a = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11 >> y=a' ← This is a transpose
y = 35 3 31 8 30 4 1 32 9 28 5 36 6 7 2 33 34 29 26 21 22 17 12 13 19 23 27 10 14 18 24 25 20 15 16 11 >> x=[1 2 3 4 5 6]; >> x x = 1 2 3 4 5 6 >> y=x' y = 1 2 3 4 5 6 Some tidy functions on vectors and matrices. >>x = -2 0 0 5 0 7 8 0 4 >> y=find(x) ← get all indices where an element is non-zero. y = 1 4 6 7 9
>> x=[6,3,9,11]; >> y=[14,2,9,13]; >> values = x(x<y) ← report those elements in x which are < y values = 6 11 >> indices = find(x<y) ← report their positions indices = 1 4 >> >> >> y=magic(5) y = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 >> [m,i]= max(y) m = 23 24 25 21 22 i = 2 1 5 4 3 >> min(y)
ans = 4 5 1 2 3 >> a=magic(5) a = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 >> b=ones(5,1) b = 1 1 1 1 1 Matrix addition, Matrix multiplication, Matrix division.
a. Two matrices of same sizes could be added, subtracted. >> a=rand(4) a = 0.4733 0.5497 0.7537 0.0540 0.3517 0.9172 0.3804 0.5308 0.8308 0.2858 0.5678 0.7792 0.5853 0.7572 0.0759 0.9340 >> b=rand(4) b =
0.1299 0.3371 0.5285 0.6541 0.5688 0.1622 0.1656 0.6892 0.4694 0.7943 0.6020 0.7482 0.0119 0.3112 0.2630 0.4505 >> c=a+b c = 0.6032 0.8868 1.2823 0.7080 0.9205 1.0794 0.5461 1.2200 1.3002 1.0801 1.1698 1.5273 0.5972 1.0684 0.3388 1.3846 >> d=rand(2) d = 0.0838 0.9133 0.2290 0.1524 >> e=a+d ??? Error using ==> plus Matrix dimensions must agree. To multiply matrix A by matrix B from the right, A and B must be of dimensions m x n and n x p, the inner two integer must be the same for matrix multiplication. >> a=[1 2 3; 4 -1 2; 3 1 -1] a = 1 2 3 4 -1 2 3 1 -1 >> b= [3 2 1; 2 -1 4; -1 -1 2]
b = 3 2 1 2 -1 4 -1 -1 2 >> c=a*b c = 4 -3 15 8 7 4 12 6 5 >> d=b*a d = 14 5 12 10 9 0 1 1 -7 Note that, in general, a*b ≠ b*a (non-commutative). An inverse matrix, if it exists, has the following property: For any square
matrix A, 1A−
is its inverse such that == −− AAAA 11eye(n) if A
is of dimension nn × .
In Matlab, )A(invA 1 =−
a = 1 2 3 4 -1 2 3 1 -1 >> b=inv(a)
b = -0.0250 0.1250 0.1750 0.2500 -0.2500 0.2500 0.1750 0.1250 -0.2250 >> a*b ans = 1.0000 0 0.0000 0.0000 1.0000 0 0.0000 0 1.0000 >> b*a ans = 1.0000 0.0000 0.0000 0 1.0000 0 0 -0.0000 1.0000 Consider now the division of a matrix by another. This could be done in two ways. Let >> a a = 1 2 3 4 -1 2 3 1 -1 >> b b = 2 1 -1 1 2 1
-1 -1 2
>> c1=a/b ← matrix right division 1AB1c −=
c1 = 0.3333 1.3333 1.0000 4.1667 -0.8333 3.5000 1.8333 -0.1667 0.5000 >> a*inv(b) ans = 0.3333 1.3333 1.0000 4.1667 -0.8333 3.5000 1.8333 -0.1667 0.5000
>> c2=a\b ← matrix left division BA2c 1−= >> inv(a)*b ans = -0.1000 0.0500 0.5000 0 -0.5000 0 0.7000 0.6500 -0.5000 >> c2 = -0.1000 0.0500 0.5000 0.0000 -0.5000 0 0.7000 0.6500 -0.5000
There are two types of operations we need to be concerned about. ■ Where an operation is meant to be scalar (element by element) op ■ where an operation is meant to be a matrix operation (done above) Here are some examples: New operators are: .* = (element by element scalar multiplication) ./ = (element by element scalar division) .^ = (element by element scalar exponentiation) >> x=[0.5 0.7 0.9];
>> y=sin(x).^3.*cos(x).^5 ← for xcosxsin 53
y = 0.0574 0.0700 0.0446 But >> 2+log(x) ans = 1.3069 1.6433 1.8946
Again. To compute 2x5x4 −+ for each element of x , we have to do >> y=x.^4+5*x-2 y = 0.5625 1.7401 3.1561
>> y=(x.^3-6)./(x.^2-7*x-9) ← for 9x7x
6x2
3
−−−
y = 0.4796 0.4218 0.3638
>> y=1./(1+1./(1+1./x)) ← for
x
11
11
1
++
y = 0.7500 0.7083 0.6786 >>a = 1 2 3 -2 3 1 -3 2 4 >> b=a.*a b = 1 4 9 4 9 1 9 4 16 >> c=a*a c = -12 14 17 -11 7 1 -19 8 9 >> x=-2.0:0.5:1.5; % colon notation >> x'
ans = -2.0000 -1.5000 -1.0000 -0.5000 0 0.5000 1.0000 1.5000 >> length(x) ans = 8 >> k=a(2,3) k = 7 >> a(2,:) ans = 23 5 7 14 16 >> a(:,5) ans = 15 16 22 3 9 >> a(2:4, 3:5)
ans = 7 14 16 13 20 22 19 21 3 >> a(1:2:5,:) ans = 17 24 1 8 15 4 6 13 20 22 11 18 25 2 9 >> b=rand(5) b = 0.7655 0.6463 0.6551 0.3404 0.5060 0.7952 0.7094 0.1626 0.5853 0.6991 0.1869 0.7547 0.1190 0.2238 0.8909 0.4898 0.2760 0.4984 0.7513 0.9593 0.4456 0.6797 0.9597 0.2551 0.5472 >> b([1 2],:)=a([1 2],:) % replacing the first two rows of b by that of a b = 17.0000 24.0000 1.0000 8.0000 15.0000 23.0000 5.0000 7.0000 14.0000 16.0000 0.1869 0.7547 0.1190 0.2238 0.8909 0.4898 0.2760 0.4984 0.7513 0.9593 0.4456 0.6797 0.9597 0.2551 0.5472 Polynomials in Matlab.
A polynomial like 2x14x3x5y 26 +−+= is expressed as a vector in the following form.
>> y=[5 0 0 0 3 -14 2]; If we want to find its roots, we do the following. >> roots(y) ← note that complex roots occur in pairs. ans = -1.0380 + 0.7788i -1.0380 - 0.7788i 0.4037 + 1.1284i 0.4037 - 1.1284i 1.1210 0.1475 We can build a polynomial from its roots (the reverse process). >> r=[1, -1, 2, -2, 6]; >> p=poly(r) p = 1 -6 -5 30 4 -24 >> roots(p) ans = 6.0000 -2.0000 -1.0000 2.0000 1.0000 polyval(a,x) evaluates a polynomial stored in a for a specific value of x. >> p p =
1 -6 -5 30 4 -24 >> polyval(p,6.001) ans = 1.1208