Practical No.

1
Problem Statements :(ii) Study of basic matrix operations using MATLAB.
1. Create a matrix A of size 4 × 4 in MATLAB given by,
11
A = [21
31
41

12
22
32
42

13
22
33
43

14
24]
34
44

Find its size in terms of number of rows and number of columns.
OUTPUT :
A =
11

12

13

14

21

22

23

24

31

32

33

34

41

42

43

44

Size =
4

4

2. The purpose of this problem is to demonstrate different ways of
addressing elements of matrices. Find the following elements of matrix A
defined above.
(a) A(2,3)
(b) A(:,2)
(c) A(3,:)
(d) A(:,2:3)
(e) A(2:4,2:3)
(f) A(:,1:2:3)
(g) A(12)
(h) A(:)
(i) A(:,:)
(j) diag(A)
(k) diag(A,1)
(l) diag(A,-1)
(m) diag(A,2)

2014BEL005

OUTPUT : a2 = 23 b2 = 12 22 32 42 c2 = 31 32 33 34 d2 = 12 13 22 23 32 33 42 43 22 23 32 33 42 43 11 13 21 23 31 33 41 43 e2 = f2 = 2014BEL005 .

g2 = 43 h2 = 11 21 31 41 12 22 32 42 13 23 33 43 14 24 34 44 i2 = 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 2014BEL005 .

(d) Create a matrix with uniformly distributed random elements of size 1 × 5 (e) Create a matrix with normally distributed random elements of size 5 × 5.j2 = 11 22 33 44 k2 = 12 23 34 l2 = 21 32 43 m2 = 13 24 3. (a) Create a matrix filled with zeros of size 2 × 3. (b) Create a matrix filled with ones of size 2 × 2. (c) Create a identity matrix I of size 3 × 3. OUTPUT : a3 = 0 0 0 0 0 0 2014BEL005 . The purpose of this problem is to generate certain simple matrices using MATLAB commands.

1832 0.1952 0.6868 0.6601 0.2097 0.2614 1.5528 -0.9492 -0.5152 -1.1176 -0.b3 = 1 1 1 1 1 0 0 0 1 0 0 0 1 c3 = d3 = 0.3349 0. OUTPUT : E = 1 2 3 4 1 2 3 4 1 2 3 4 5 6 5 6 F = a4 = 1 2 3 4 2014BEL005 .0391 -0.6252 0.1352 1.7447 0.8261 0.5270 -1.1890 0.3071 0. (a) Generate two row vectors A and B with dimensions 1 × 6 and 1× 4 respectively and combine them to form a matrix C with dimensions 1 × 10.2176 1.0230 -0.2607 -0.0298 0.0679 0.1835 -0.3031 0.9415 e3 = 4. The purpose of this exercise is to study matrix concatenation.2963 0.0513 0.4669 0.

OUTPUT : D = 1 2 3 4 5 6 7 8 9 2014BEL005 .(b) Find the result of following operation for the matrix A defined in problem 1: B = [A [ones(2. (e) Rank of matrix. (f) Coefficients of characteristic polynomial obtained by. (d) Inverse of matrix. (g) Eigen values obtained by solving above polynomial. (b) Square of matrix. 2). |λI − A| = 0. Generate a 3 × 3 matrix A and make (a) Square of each element. eye(2)] ] OUTPUT : b4 = 11 21 31 41 12 22 32 42 13 23 33 43 14 24 34 44 1 1 1 0 1 1 0 1 (c) Create a square matrix C of size 2×2 and do the following operation to replicate the matrix: D = repmat(C. 3) OUTPUT : C = 11 21 12 22 c4 = 11 21 11 21 12 22 12 22 11 21 11 21 12 22 12 22 11 21 11 21 12 22 12 22 5. 2. (c) Transpose of matrix.

8014 0. 2014BEL005 .0e+16 * -0.9007 -0.9007 -0.0000 g5 = 16.1168 -0.1168 -1.9007 -1.0000 -0.4504 0.0000 e5 = 2 f5 = 1.0000 -18.9007 -0.4504 0.4504 -15.a5 = 1 4 9 16 25 36 49 64 81 30 36 42 66 81 96 102 126 150 1 4 7 2 5 8 3 6 9 b5 = c5 = d5 = 1.4504 0.

cos(t). plot the following using subplot command: (a) sin(t) w. 1. OUTPUT : 3. t. Generate a cosine wave in the same figure window for the same time interval. t. cos(t). In the new figure window. r. t. t. (d) sin(4t) w. 2. cos(t). r. Generate a sine wave in MATLAB. r. (c) sin(3t) w. Draw grid lines to X and Y axes. (b) sin(2t) w. r. cos(t).(iii) Study of basic plotting tools available in MATLAB. OUTPUT : 2014BEL005 . Draw the grid lines to the plot.

MATLAB is especially designed for matrix computations: solving systems of linear equations. 2014BEL005 . r. and so forth. t.4. time t. basic matrix operations and basic plotting tools available in MATLAB. and can be extended through programs written in its own programming language. OUTPUT : RESULT : MATLAB (Matrix laboratory) is an interactive software system for numerical computations and graphics. it has a variety of graphical capabilities. In addition. CONCLUSION : In this way we have studied Introduction to MATLAB software. computing eigenvalues and eigenvectors. factoring matrices. Draw a 3-D plot which contain values of cos(3t) and sin(3t) drawn w. As the name suggests.

% S=sin(0.:) j2=diag(A) k2=diag(A.01:25.:) d2=A(:.21 22] c4=repmat(C.002*pi*f*t) figure(1) S=sin(t) subplot(1.F] % a4=cat(2.1:2:3) g2=A(12) h2=A(:) i2=A(:.^2 b5=D^2 c5=D' d5=inv(D) e5=rank(D) f5=poly(D) g5=eig(D) disp('(iii) Study of basic plotting tools available in MATLAB.2:3) e2=A(2:4.3) D=[1 2 3.5) e3=randn(5) E=[1 2 3 4 5 6] F=[1 2 3 4] a4=[E.PROGRAM : clc.F) b4=[A [ones(2). eye(2)] ] C=[11 12.7 8 9] a5=D.2) c2=A(3.2.') t=0:0.2:3) f2=A(:.-1) m2=diag(A.41 42 43 44] Size=size(A) a2=A(2.3) b3=ones(2) c3=eye(3) d3=rand(1.S) grid on xlabel('Time') ylabel('Amplitude') 2014BEL005 . disp('(ii) Study of basic matrix operations using MATLAB.4 5 6.1) l2=diag(A.21 22 23 24.2) a3=zeros(2.1) plot(t.3) b2=A(:.E.2.') A=[11 12 13 14.31 32 33 34. clear all.

r. plot3(sin(3*t).2) plot(cos(t).title('Sine Wave') T=cos(t) subplot(1.sin(2*t)) grid on xlabel('Cos(t)') ylabel('Sin(2t)') subplot(2.cos(3*t).sin(t)) grid on xlabel('Cos(t)') ylabel('Sin(t)') subplot(2.2.t time t') 2014BEL005 .2) plot(t.2.sin(3*t)) grid on xlabel('Cos(t)') ylabel('Sin(3t)') subplot(2.T) grid on xlabel('Time') ylabel('Amplitude') title('Cosine Wave') figure(2) subplot(2.3) plot(cos(t).01:100.2.t) grid on xlabel('Sin(3t)') ylabel('cos(3t)') zlabel('t') title('3D plot of sin(3t) and cos(3t) w.4) plot(cos(t).2.1) plot(cos(t).2.sin(4*t)) grid on xlabel('Cos(t)') ylabel('Sin(4t)') figure(3) t=0:0.