Anda di halaman 1dari 11

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

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

j2 =
11
22
33
44
k2 =
12
23
34
l2 =
21
32
43
m2 =
13
24

3. The purpose of this problem is to generate certain simple matrices using


MATLAB commands.
(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.
(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.
OUTPUT :
a3 =
0

2014BEL005

b3 =
1

c3 =

d3 =
0.2963

0.7447

0.1890

0.6868

0.1835

-0.3349

0.6601

0.0230

-0.2097

0.3071

0.5528

-0.0679

0.0513

0.6252

0.1352

1.0391

-0.1952

0.8261

0.1832

0.5152

-1.1176

-0.2176

1.5270

-1.0298

0.2614

1.2607

-0.3031

0.4669

0.9492

-0.9415

e3 =

4. The purpose of this exercise is to study matrix concatenation.


(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.
OUTPUT :
E =
1

F =
a4 =
1

2014BEL005

(b) Find the result of following operation for the matrix A defined in
problem 1:
B = [A [ones(2, 2); 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 22 and do the following operation to


replicate the matrix:
D = repmat(C, 2, 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. Generate a 3 3 matrix A and make


(a) Square of each element.
(b) Square of matrix.
(c) Transpose of matrix.
(d) Inverse of matrix.
(e) Rank of matrix.
(f) Coefficients of characteristic polynomial obtained by, |I A| = 0.
(g) Eigen values obtained by solving above polynomial.
OUTPUT :
D =
1

2014BEL005

a5 =
1

16

25

36

49

64

81

30

36

42

66

81

96

102

126

150

b5 =

c5 =

d5 =
1.0e+16 *
-0.4504

0.9007

-0.4504

0.9007

-1.8014

0.9007

-0.4504

0.9007

-0.4504

-15.0000

-18.0000

e5 =
2
f5 =
1.0000

-0.0000

g5 =
16.1168
-1.1168
-0.

2014BEL005

(iii) Study of basic plotting tools available in MATLAB.


1. Generate a sine wave in MATLAB. Draw the grid lines to the plot.
2. Generate a cosine wave in the same figure window for the same time
interval. Draw grid lines to X and Y axes.
OUTPUT :

3. In the new figure window, plot the following using subplot command:
(a) sin(t) w. r. t. cos(t).
(b) sin(2t) w. r. t. cos(t).
(c) sin(3t) w. r. t. cos(t).
(d) sin(4t) w. r. t. cos(t).
OUTPUT :

2014BEL005

4. Draw a 3-D plot which contain values of cos(3t) and sin(3t) drawn w. r. t.
time t.
OUTPUT :

RESULT :
MATLAB (Matrix laboratory) is an interactive software system for numerical
computations and graphics. As the name suggests, MATLAB is especially designed for
matrix computations: solving systems of linear equations, computing eigenvalues and
eigenvectors, factoring matrices, and so forth. In addition, it has a variety of graphical
capabilities, and can be extended through programs written in its own programming
language.

CONCLUSION :
In this way we have studied Introduction to MATLAB software, basic matrix
operations and basic plotting tools available in MATLAB.

2014BEL005

PROGRAM :
clc;
clear all;
disp('(ii) Study of basic matrix operations using MATLAB.')
A=[11 12 13 14;21 22 23 24;31 32 33 34;41 42 43 44]
Size=size(A)
a2=A(2,3)
b2=A(:,2)
c2=A(3,:)
d2=A(:,2:3)
e2=A(2:4,2:3)
f2=A(:,1:2:3)
g2=A(12)
h2=A(:)
i2=A(:,:)
j2=diag(A)
k2=diag(A,1)
l2=diag(A,-1)
m2=diag(A,2)
a3=zeros(2,3)
b3=ones(2)
c3=eye(3)
d3=rand(1,5)
e3=randn(5)
E=[1 2 3 4 5 6]
F=[1 2 3 4]
a4=[E,F]
% a4=cat(2,E,F)
b4=[A [ones(2); eye(2)] ]
C=[11 12;21 22]
c4=repmat(C,2,3)
D=[1 2 3;4 5 6;7 8 9]
a5=D.^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.')
t=0:0.01:25;
% S=sin(0.002*pi*f*t)
figure(1)
S=sin(t)
subplot(1,2,1)
plot(t,S)
grid on
xlabel('Time')
ylabel('Amplitude')

2014BEL005

title('Sine Wave')
T=cos(t)
subplot(1,2,2)
plot(t,T)
grid on
xlabel('Time')
ylabel('Amplitude')
title('Cosine Wave')
figure(2)
subplot(2,2,1)
plot(cos(t),sin(t))
grid on
xlabel('Cos(t)')
ylabel('Sin(t)')
subplot(2,2,2)
plot(cos(t),sin(2*t))
grid on
xlabel('Cos(t)')
ylabel('Sin(2t)')
subplot(2,2,3)
plot(cos(t),sin(3*t))
grid on
xlabel('Cos(t)')
ylabel('Sin(3t)')
subplot(2,2,4)
plot(cos(t),sin(4*t))
grid on
xlabel('Cos(t)')
ylabel('Sin(4t)')
figure(3)
t=0:0.01:100;
plot3(sin(3*t),cos(3*t),t)
grid on
xlabel('Sin(3t)')
ylabel('cos(3t)')
zlabel('t')
title('3D plot of sin(3t) and cos(3t) w.r.t time t')

2014BEL005