0% found this document useful (0 votes)
21 views12 pages

Mohit 2

The document contains a MATLAB practical file submitted by a student named Anurag Kaushik. It includes 19 exercises demonstrating various MATLAB commands and functions. The exercises cover topics such as creating vectors and matrices, performing operations on matrices, defining functions, plotting graphs, and manipulating complex numbers.

Uploaded by

T SERIES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

Mohit 2

The document contains a MATLAB practical file submitted by a student named Anurag Kaushik. It includes 19 exercises demonstrating various MATLAB commands and functions. The exercises cover topics such as creating vectors and matrices, performing operations on matrices, defining functions, plotting graphs, and manipulating complex numbers.

Uploaded by

T SERIES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Department of Physics

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

Exercise 2:- Create the following matrices


a) Identity matrix of order 4
>> eye(4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

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

Exercise 3:- Find a short MATLAB expression to built the matrix


1 2 3 4 5 6 7
9 5 7 5 8 2 1
2 4 6 8 10 12 14
>> x = [1 2 3 4 5 6 7 ; 9 5 7 5 8 2 1 ; 2 4 6 8 10 12 14]
x=

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

Exercise 6:- Find the addition and multiplication of the matrix


A=
2 3 4
2 1 2
3 4 3
B=
1 2 3
4 1 2
3 4 9

>> 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

Exercise 9:- Find the Diagonal of the matrix


5 2 −3
−4 9 2
9 6 −9

>> 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

>> a=[3 2 1;1 0 2;3 2 0]


a=
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

Exercise 12:- Create a cell-array.


>> a = cell(2)
a=
2×2 cell array
{0×0 double} {0×0 double}
{0×0 double} {0×0 double}

>> a{1,2} = 'you'


a=
2×2 cell array
{0×0 double} {'you' }
{0×0 double} {0×0 double}
Exercise 13:- Write command for any 5 built-in functions.
>> magic(3)
ans =
8 1 6
3 5 7
4 9 2
>> zeros(3,4)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
>> ones(3,4)
ans =
1 1 1 1
1 1 1 1
1 1 1 1

>> 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 14:- Define a user-defined function and plot its graph.


>> x=[-100:20:100];
>> y=x.^2;
>> plot(x,y)

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

a) if else end statement


>> a=10;
>> if(a<20)
disp('number is less than 20')
else
disp('number is greater than 20')
end
number is less than 20

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 19:- If x= [1 5 9 13 ; 2 7 4 9 ; 1 4 8 1 5], then

a) display the last element by using disp command.


>> x = [ 1 5 9 13 ; 2 7 4 9 ; 1 4 8 15 ]
x=
1 5 9 13
2 7 4 9
1 4 8 15
>> disp(x(end))
15
b) display the sum of each row as shown below
The sum of 2nd row =
The sum of 3rd row =
>> sum(x,2)
ans =
28
22
28

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);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy