Mohit 2
Mohit 2
Mdu rohtak
m.sc physics
PRACTICAL FILE
FOR OPEN ELECTIVE
SESSION: 2023-24
SUBJECT: MATLAb
Submitted By Submitted To
NAME: Anurag kaushik Miss. Shruti Jain
ROLL NO: 2252
(3rd Sem) FINAL YEAR
Exercise 1:- Create a vector using linspace command with values ranging from
0 to 20 and having 8 elements .
>> x = linspace(0,20,8)
x=
0 2.8571 5.7143 8.5714 11.4286 14.2857 17.1429 20.0000
b) Magic matrix
>> magic(4)
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
c) Matrix with all zeros
>> zeros(3,4)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
1 2 3 4 5 6 7
9 5 7 5 8 2 1
2 4 6 8 10 12 14
a) After building this matrix reshape this matrix in 1xN shape where N
is the total number of element in the given matrix
>> y=reshape(x,1,21)
y=
Columns 1 through 20
1 9 2 2 5 4 3 7 6 4 5 8 5 8 10 6 2 12 7 1
Column 21
14
b) Access only 2nd ,3rd rows and 2nd to 5th columns from the given matrix
>> x(2:3,2:5)
ans =
5 7 5 8
4 6 8 10
c) Delete the 4th column from the given matrix
>> x(:,4)=[ ]
x=
1 2 3 5 6 7
9 5 7 8 2 1
2 4 6 10 12 14
Exercise 4:- Give a MATLAB expression that multiplies two vectors to obtain
000
111
222
>> a = [0 ; 1 ; 2]
a=
0
1
2
>> b = [1 , 1 , 1]
b=
1 1 1
>> c = a*b
c=
0 0 0
1 1 1
2 2 2
Exercise 5:- Create a vector of 7 elements using a short MATLAB expression.
>> x = 1 : 7
x=
1 2 3 4 5 6 7
>> A = [ 2 3 4 ; 2 1 2 ; 3 4 3 ]
A=
2 3 4
2 1 2
3 4 3
>> B = [ 1 2 3 ; 4 1 2 ; 3 4 9 ]
B=
1 2 3
4 1 2
3 4 9
>> S = A + B
S=
3 5 7
6 2 4
6 8 12
>> M = A * B
M=
26 23 48
12 13 26
28 22 44
Exercise 7:- Find the rank and trace of matrix
1 4 6
A= 4 9 2
3 6 7
>> A = [ 1 4 6 ; 4 9 2 ; 3 6 7 ]
A=
1 4 6
4 9 2
3 6 7
>> rank(A)
ans =
3
>> trace (A)
ans =
17
Exercise 8:- Find the inverse of matrix
0 2 3
A= 4 0 2
9 0 0
>> A = [ 0 2 3 ; 4 0 2 ; 9 0 0 ]
A=
0 2 3
4 0 2
9 0 0
>> inv(A)
ans =
0 0 0.1111
0.5000 -0.7500 0.3333
0 0.5000 -0.2222
>> A = [ 5 2 -3 ; -4 9 2 ; 9 6 -9 ]
A=
5 2 -3
-4 9 2
9 6 -9
>> B = diag(A)
B=
5
9
-9
Exercise 10:- Find the Eigen values and Eigen vector of the matrix
3 2 1
1 0 2
3 2 0
>> eig(a)
ans =
4.9142
-0.2436
-1.6706
>> [P,D]=eig(a)
P=
-0.7100 -0.5558 0.2234
-0.3846 0.8118 -0.8001
-0.5899 0.1790 0.5566
D=
4.9142 0 0
0 -0.2436 0
0 0 -1.6706
Exercise 11:- Create a random matrix of size 4X4 with normalized values.
>> randn(4,4)
ans =
0.5377 0.3188 3.5784 0.7254
1.8339 -1.3077 2.7694 -0.0631
-2.2588 -0.4336 -1.3499 0.7147
0.8622 0.3426 3.0349 -0.2050
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
>> rand(3,3)
ans =
0.4218 0.9595 0.8491
0.9157 0.6557 0.9340
0.7922 0.0357 0.6787
Exercise 15:- Use the colon operator to create a vector of x values ranging from
zero to pi , compute the cosine of these values, and plot the result.
>> x=0:pi/100:pi;
>> y=cos(x);
>> plot( x , y , 'g-', 'linewidth',5 )
>> xlabel('vector x from x=0:pi')
>> ylabel('vector y = cos of x')
Exercise 16:- Plot Sinc function, where Sinc (x) = xsin(x) , and -π ≤ x ≤ 2π
>> x=-2*pi : 0.01 : 2*pi ;
>> sinc=sin(x)./x;
>> plot(x,sinc);
>> xlabel('x');
>> ylabel('sinc');
Exercise 17:- Write a script to show the use of
b) while loop
>> x=5;
>> while x<=20
x=x+3
end
x=
8
x=
11
x=
14
x=
17
x=
20
x=
23
Exercise 18:- Exchange the real and imaginary parts of the following matrix
A= 0.8147 + 0.1576i 0.9058 + 0.9706i 0.1270 + 0.9572i
0.9134 + 0.4854i 0.6324 + 0.8003i 0.0975 + 0.1419i
>>A=[0.8147+0.1576i,0.9058+0.9706i,0.1270+0.9572i,0.9134+0.4854i,0.6324+0.8003i,0.0975
+0.1419i];
>> x=real(A);
>> y=imag(A);
>> a=x;
>> x=y;
>> y=a;
>> A=x+i*y
A=
0.1576 + 0.8147i 0.9706 + 0.9058i 0.9572 + 0.1270i 0.4854 + 0.9134i 0.8003 +
0.6324i 0.1419 + 0.0975i
Exercise 20:- Plot sin(x) and cos(x) on the same figure using different colors.
>> x = [ -pi : 0.01 : pi ];
y = sin(x);
plot(x,y,'r','marker','o','linewidth',0.5);
>> hold on
>> x = [ -pi : 0.01 : pi ];
y=cos(x);
plot(x,y,'b','marker','*','linewidth',1.5);