You are on page 1of 21

# Experiment number 1:- Introduction of MATLAB

Que.1. The purpose of this exercise is to demonstrate differant ways of addresing element of
matrics. Consider the following matrics A:

11 12 13 14
A = 21 22 23 24
31 32 33 34
41 42 43 44
You are to predict the result of the following operation and later check prediction on the computer.
close all;
clear all;
clc
A=[11:14; 21:24; 31:34; 41:44]
A(:,1)
A(2,:)
A(:,2:3)
A(2:3,2:3)
A(:,1:2:3)
A(2:3)
A(:)
A(:,:)
ones(2,2)
eye(2)
B=[A,[ones(2,2);eye(2)]]
diag(A)
diag(A,1)
diag(A,-1)
diag(A,2)
eig(A)
poly(A)

ANSWER:= (COMMAND WINDOW)
A =
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

ans =
11
21
31
41
ans =
21 22 23 24
ans =
12 13
22 23
32 33
42 43
ans =
22 23
32 33
ans =
11 13
21 23
31 33
41 43
ans =
21 31
ans =
11
21
31
41
12
22
32
42
13
23
33
43
14
24
34
44
ans =
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
ans =
1 1
1 1
ans =
1 0
0 1
B =
11 12 13 14 1 1
21 22 23 24 1 1
31 32 33 34 1 0
41 42 43 44 0 1
ans =
11
22
33
44
ans =
12
23
34
ans =
21
32
43
ans =
13
24
ans =
111.7891
-1.7891
0.0000
-0.0000
ans =
1.0000 -110.0000 -200.0000 0.0000 0.0000

Que.2. Create some variables in MATLAB and save workspace saved for verification.
ANSWER:=
In this case we have so many variables available due to 1st program in workspace window.
Now we have to save this workspace window with any name (say matlab 11) now close the MATLAB
window.
Now again open new window and open import data option in workspace window
we get following window now open your saved file,

after this we gets a window, in that we have to click on 'generate matlab code' box and click
on finish button.

After that, we get a window having workspace values of saved variables...
after dragging this values yo command window we get the status of that variable or last
saved data in that variable.

Que.3. Generate two row-vectors A and B with dimensions 1*4 and 1*6 respectively and combine
then form C vector of length10(i.e. 1*10)
(EDITOR WINDOW)
close all;
clear all;
clc
A=[1:4];
B=[1:6];
c=[A,B]
(COMMAND WINDOW)
ANSWER:=
A =

1 2 3 4

B =

1 2 3 4 5 6

c =

1 2 3 4 1 2 3 4 5 6

Que.4. Generate a 3*3 matrix A and make.
1.Square of each element.
2.Square of matrix.
(EDITOR WINDOW)
close all;
clear all;
clc
A=[11:14; 21:24; 31:34; 41:44]
A=A.^2
A*A

ANSWER:= (COMMAND WINDOW)
A =
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

A =
121 144 169 196
441 484 529 576
961 1024 1089 1156
1681 1764 1849 1936

ans =
570030 605920 643070 681480
1743430 1855520 1971670 2091880
3557630 3788320 4027470 4275080
6012630 6404320 6810470 7231080

Que.5. The purpose o this exercise is to demonstrate matrix functions; e.g. the matrix sine function.
a)Enter matrix A= 0 1.0000
0.5236 1.5236
b)Find the sine of the individual elements; call this B1.
c)Find the cosine of the individual elements; call this B2.
d)Find B1^2 +B2^2. Note that this is not identity matrix.
e)Find the eigenvalues and eigenvectors of A ; call the eigenvector matrix M and eigenvector matrix
L.
f)find Msin(L)M^-1.
g)Find the matrix sine function of A using the "funm" command
h)Find cos(A)
i)Show sin(A)^2+cos(A)^2 = I =identity matrix

(EDITOR WINDOW)

close all;
clear all;
clc
A=[0:pi/3; pi/6:pi/2]
B1=sin(A)
B2=cos(A)
(B1)^2+(B2)^2
[M,L]=eig(A)
M*sin(L)*M^-1
M*funm(L, @sin)*M^-1
cos(A)
sin(A)^2+cos(A)^2

(COMMAND WINDOW)
ANSWER:=
A =
0 1.0000
0.5236 1.5236

B1 =
0 0.8415
0.5000 0.9989
B2 =
1.0000 0.5403
0.8660 0.0472
ans =
1.8887 1.4063
1.4063 1.8887
M =
-0.9607 -0.4831
0.2775 -0.8756
L =
-0.2889 0
0 1.8125
ans =
-0.1122 0.5976
0.3129 0.7983
ans =
-0.1122 0.5976
0.3129 0.7983
ans =
1.0000 0.5403
0.8660 0.0472

ans =
1.8887 1.4063
1.4063 1.8887

Que.6. part(1) The purpose of this exercise is to demonstrate the use of matrix division (/)
command Use the "rand" command to generate five random 2*2 matrices A,B,C,D,E.Without the
use of "inv" command compute F in one line F=A^-1[B+C^-1 (D^-1 * E)].
part(2) Without the use of "inv" command, find the first column of A^-1 using one command.
Check your answer in both parts using the "inv" command.

(EDITOR WINDOW)
close all;
clear all;
clc
A=rand(2)
B=rand(2)
C=rand(2)
D=rand(2)
E=rand(2)
F=A^-1*[B+C^-1*(D^-1*E)]
A^-1*[:,1]

ANSWER:= (COMMAND WINDOW)

A =
0.6557 0.8491
0.0357 0.9340
B =
0.6787 0.7431
0.7577 0.3922

C =
0.6555 0.7060
0.1712 0.0318
D =
0.2769 0.0971
0.0462 0.8235
E =
0.6948 0.9502
0.3171 0.0344
F =
-2.0924 -13.0708
3.5802 8.3855

{Note:- For problem 7, 8, 9 generate a matrix A with dimension 10*10 and element with random
values in the range of 1 - 100 }
{ INSTRUCTION FOR GENERATING 10*10 MATRIX RANGE BETWEEN 1-100 =
randint(10,10[1'100]) }

Que.7. Take i and j as two indices from user and output in command windows the ith row and jth
column element.
(EDITOR WINDOW)
close all;
clear all;
clc
A=randint(10,10,[1,100])
i=input('enter the value of i=');
j=input('enter the value of j=');
A(i,j)

ANSWER:= (COMMAND WINDOW)

A =
91 43 54 78 52 26 92 18 27 65
88 10 66 43 95 23 1 73 77 68
82 60 41 10 64 67 47 48 19 64
27 48 82 27 96 85 43 16 29 95
60 70 72 16 25 35 47 35 10 21
3 70 97 29 68 79 78 61 58 71
43 64 54 45 29 68 33 20 69 24
32 4 33 53 68 1 79 74 55 12
17 7 11 46 70 61 48 25 43 61
18 32 62 88 7 39 4 92 65 46
enter the value of i=2
enter the value of j=2
ans =
10

Que.8. Copy 4th 5th and 6th column and row of matrix A to a new matrix B and output the same.
(EDITOR WINDOW)
close all;
clear all;
clc
A=randint(10,10,[1,100])
B=A(4:6,4:6)
i=input('enter the value of i=');
j=input('enter the value of j=');
B(i,j)

ANSWER:= (COMMAND WINDOW)

A =
46 59 55 41 70 35 74 83 80 52
67 55 65 45 10 15 40 43 95 89
78 87 55 37 53 59 69 89 33 59
36 27 73 77 54 27 71 40 68 16
67 32 53 63 87 5 45 77 44 20
42 12 100 78 49 76 2 40 84 41
85 94 22 94 40 25 34 81 77 75
84 65 11 98 68 45 43 76 17 83
26 48 11 20 75 69 28 38 87 79
62 64 7 14 53 36 20 22 99 32
ans =
77 54 27
63 87 5
78 49 76

enter the value of i=2
enter the value of j=2
ans =
87

Que.9. Copy complete A matrix to a new matrix B with Following condition,
1. Element of A is less than 40 copy as it as zero.
2. If element of B is greater than 80 copy it as 100.
(EDITOR WINDOW)
close all;
clear all;
clc
A=(10,10,[1,100])
for i=1:10
for j=1:10
if A(i,j)<40
A(i,j)=0;
elseif A(i,j)>80
A(i,j)=100;
else
A(i,j)=A(i,j);
end
end
end
B=A

(COMMAND WINDOW)
ANSWER:=
B=
46 59 55 41 70 0 74 100 100 52
67 55 65 45 0 0 40 43 100 100
78 100 55 0 53 59 69 100 0 59
0 0 73 77 54 27 71 40 68 0
67 0 53 63 100 0 45 77 44 0
42 0 100 78 49 76 0 40 100 41
100 100 0 100 40 0 0 100 77 75
100 65 0 100 68 45 43 76 0 100
0 48 0 0 75 69 0 0 100 79
62 64 0 0 53 0 0 0 100 0

Que.10. The purpose of this problem is to practice some of graphics features.
A) Plot following in polar co-odinate for 0<t<2pi
a)r=3(1-cos(t)) Cardioids
(EDITOR WINDOW)
close all;
clear all;
clc
t=[0:0.01:2*pi]
r=3*(1-cos(t))
polar(t,r);

(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'r'....
And it shows the polar plot as follows....

b)r=2(1+cos(t))
(EDITOR WINDOW)
close all;
clear all;
clc
t=[0:0.01:2*pi]
r=3*(1+cos(t))
polar(t,r);

(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'r'....
And it shows the polar plot as follows...

c)r=2(1+sin(t))
(EDITOR WINDOW)
close all;
clear all;
clc
t=[0:0.01:2*pi]
r=3*(1+sin(t))
polar(t,r);
(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'r'.... And it shows the polar plot as follows....

d)r=cos(3t) Three Leaf Rose
(EDITOR WINDOW)
close all;
clear all;
clc
t=[0:0.01:2*pi]
r=3*cos(3*t)
polar(t,r);
(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'r'....
And it shows the polar plot as follows....

e)r<=e(t/4)*pi Logarithmic Spiral
(EDITOR WINDOW)
close all;
clear all;
clc
t=[0:0.01:2*pi];
r=exp((t/4)*pi);
polar(t,r)

(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'r'....
And it shows the polar plot as follows....

B)obtain the 3-D plot of the function "z" for the given range of values -5<=x<=+5 and
(EDITOR WINDOW)
close all;
clear all;
clc
x=-5:.1:5
y=-5:.1:5
a=[(square(x+1))+(square(y+1))+1]
b=[square(x-1)+square(y-1)+1]
z=[(1./a)-(1.5./b)]
plot3(x,y,z,'--')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
legend('z')
axis('equal')
(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 'x', 'y', 'a', 'b' and 'z'....
And it shows the 3D plot as follows....

Que.11. The purpose of this problem is to practice graphics along with some MATLAB math function.
1.
(EDITOR WINDOW)
close all;
clear all;
clc
t=0:.01:8
y=[1-2.*exp(-t).*sin(t)]
plot(t,y)
xlabel('Time')
ylabel('Amplitude')
title('Decying Oscillating Exponential')

(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'y'....
And it shows the 2D plot as follows....

2.
(EDITOR WINDOW)
close all;
clear all;
clc
t=0:0.1:30
y=5.*exp(-0.2.*t).*cos(0.9.*t-pi/6)+0.8.*exp(-2.*t)
plot(t,y)
(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'y'....
And it shows the 2D plot as follows....

3.
(EDITOR WINDOW)
close all;
clear all;
clc
t=0:.01:10
y=1.23*cos(2.83*t+(4*pi)/3)+.625
x=.625
plot(t,y,t,x)
legend('y')

(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'y'....
And it shows the 2D plot as follows....

4.
(EDITOR WINDOW)
close all;
clear all;
clc
t=linspace(0,20,1000)
y1=2.62*exp(-0.25*t).*cos(2.22*t+174*pi/180)+0.6
y2=2.62*exp(-0.25*t)+0.6
y3=0.6
plot(t,y1,'--',t,y2,t,y3)
legend('y1','y2','y3')
ylim([-2 3])
xlabel('t')
ylabel('y')
title('Three plots on same graph')

(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'y1', 'y2, y3'....
And it shows the 2D plot as follows....

5.
(EDITOR WINDOW)
t=[0:0.01:25]
min=0
max=16
y1=(1.25)*(exp(-t))
y2=2.02*exp(-0.3*t)
y3=2.02*exp(-.3*t).*cos(0.554*t-angle(128))+2.02*exp(-t)
y3=@(t) 2.02*exp(-.3*t).*cos(0.554*t-angle(128))+2.02*exp(-t)
y3(0)
y3(12)
y3(min)
y3(max)
plot(t,y2,t,y1,t,y3(0),t,y3(12))
grid
axis([0 16 -.2 1])
xlabel('x')
ylabel('y')
title('Graph of different equation')

(COMMAND WINDOW)
ANSWER:=
In command window it shows the values of 't' and 'y1', 'y2, 'y3', y3(0), y3(12), y3(min), y3(max)....
And it shows the 2D plot as follows....