0% found this document useful (0 votes)
43 views32 pages

Programmes

The document contains 6 tasks related to matrices in MATLAB: 1. It defines a 3x4 matrix and restructures it into different sizes and orders, such as 1x12, 4x3, etc. 2. It demonstrates some useful matrix commands like determinant, rank, transpose, and inverse of a sample matrix. 3. It creates special matrices like zero, identity, and ones matrices. 4. It performs arithmetic operations on matrices like addition and shows how to access specific elements. 5. It demonstrates how to add/delete rows and columns and remove elements from a matrix. 6. It finds the eigenvalues and eigenvectors of a sample matrix and generates a random matrix

Uploaded by

Hammad khan
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)
43 views32 pages

Programmes

The document contains 6 tasks related to matrices in MATLAB: 1. It defines a 3x4 matrix and restructures it into different sizes and orders, such as 1x12, 4x3, etc. 2. It demonstrates some useful matrix commands like determinant, rank, transpose, and inverse of a sample matrix. 3. It creates special matrices like zero, identity, and ones matrices. 4. It performs arithmetic operations on matrices like addition and shows how to access specific elements. 5. It demonstrates how to add/delete rows and columns and remove elements from a matrix. 6. It finds the eigenvalues and eigenvectors of a sample matrix and generates a random matrix

Uploaded by

Hammad khan
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/ 32

`programmes

Lab#2
%%
%Task#1
%write a programme to calculate area of circle
%having radius 3cm
% area of circle
r=3;
Area=pi*(r^2)
%%
%Task#2
%given two sides a=3.2 and b=4.6 of a triangle and angle 60 deg between these
%two sides, find the length of the third side and the area of triangle
%programe:
a=3.2; b=4.6; theta=60;
c=sqrt(a^2+b^2-2*a*b*cosd(theta))
Area_of_triangle=1/2*a*b*sind(theta)
%%
%Task#3
%Evaluate the given Expression for 'x' from 1 to 2 in steps of 0.1
%programme:
x=1:0.1:2;
y=(1./x)+(x.^3)./(x.^4+5.*x.*sin(x))
%%
%Task#4
%write assingment statements to evalite the following:
%A:
r=2; h=3;
Volume=pi*r^2*h
%B:
N=1000; T=10;
power=2*pi*N*T/60
%C:
V=250; I=5; R=2;W=20;
Efficiency=(V*I*0.8)/(V*I*0.8+I^2*R+W)
%%
%Task#5
%in an electric circuit with an inductance L=0.01 and resistance R=180, the
damped natural
% frequency of oscilliation is given,write a programme to calculate
% frequency for different values of "c" ranging from 0.1 to 1 in steps of
% 0.1
%programme starts:
L=0.01*10^-3; R=180;
C=0.1:0.1:1;
Frequency=sqrt(1./(L.*C)-(R.^2)./(4*C.^2))

Lab#3
Task#1 20-02-2019
%%
%some useful commands
1.
x=[3 4 -1 6 0]
length(x)
output

ans =

5
2.
%%
x=[3 4 -1 6 0]
size(x)
output

ans =

1 5

3.
%%
x=[3 4 -1 6 0]
sort(x)
output
ans =
-1 0 3 4 6
4.
%%
x=[3 4 -1 6 0]
sort(x,'descend')
output
ans =

6 4 3 0 -1
5.
%%
x=[3 4 -1 6 0]
mean(x)
max(x)
min(x)
sum(x)
median(x)
std(x)
output
ans =

2.4000

ans =

6
ans =

-1

ans =

12

ans =

2.8810

Task#02
%%
%some useful commands related to matrix
%for saqure matrix
A=[1 2 3;4 5 6;7 8 9]
det(A)
rank(A)
transpose(A)
inv(A)
trace(A)

output
A =

1 2 3
4 5 6
7 8 9

ans =

-9.5162e-16

ans =

2
ans =

1 4 7
2 5 8
3 6 9

Warning: Matrix is close to singular or badly scaled. Results


may be inaccurate. RCOND = 2.202823e-18.

ans =

1.0e+16 *

0.3153 -0.6305 0.3153


-0.6305 1.2610 -0.6305
0.3153 -0.6305 0.3153

ans =

15
Task#03
%%
%special matrices
%zero matrix
A=zeros(3,3)
Output
A=

0 0 0
0 0 0
0 0 0
%%
%identity matrix
b=eye(3,4)

Output
b=

1 0 0 0
0 1 0 0
0 0 1 0
%%
%matrix of ones
c=ones(4,3)
Output
c=

1 1 1
1 1 1
1 1 1
1 1 1
%%
%task#04
%artihmatic operations on matrices
%addition
A=[2 4 4;6 7 8;9 8 7]
B=[4 5 6;7 6 5;5 7 9]
z=A+B
output
A=

2 4 4
6 7 8
9 8 7

B=

4 5 6
7 6 5
5 7 9

z=

6 9 10
13 13 13
14 15 16
%%
%Task#05
%given a matrix A
%write a matlab statement to obtain:
%a All the elements of all rows but 1st colum
%b All the elemnets of 1st row but all colums
%c elements in the 2nd row and 3rd column
%d all the elements of row 1 to 3 and colums from 3 to 4
A=[2 -4 6 -8; 1 3 5 7;2 12 30 56]
A(:,1)
A(1,:)
A(2,3)
A(1:3,3:4)
Output
A=
2 -4 6 -8
1 3 5 7
2 12 30 56

ans =

2
1
2

ans =

2 -4 6 -8

ans =

ans =

6 -8
5 7
30 56
Manipulation of matrix

%%
%task #01 lab#04 27-02-2019
%define a 3*4 matrix and reshape it into a different possible orders given
%below
%1*12
%12*1
%4*3
%2*6
%6*2
A=[1 2 3 4; 5 6 7 8; 9 2 3 5]
B1=reshape(A,4,3)
B2=reshape(A,2,6)
B3=reshape(A,6,2)
B4=reshape(A,1,12)
B5=reshape(A,12,1)
Output
A=

1 2 3 4
5 6 7 8
9 2 3 5

B1 =

1 6 3
5 2 4
9 3 8
2 7 5

B2 =

1 9 6 3 3 8
5 2 2 7 4 5

B3 =

1 3
5 7
9 3
2 4
6 8
2 5

B4 =

1 5 9 2 6 2 3 7 3 4 8 5
B5 =

1
5
9
2
6
2
3
7
3
4
8
5
%%
%TASK#02
%Replacing an element, row or coulmn wihin matrix
A=[3 4 5 6 7;8 9 10 11 12;13 14 15 16 17]
%a A(3,2) with 80
A(3,2)=80

Output

A=

3 4 5 6 7
8 9 10 11 12
13 14 15 16 17

A=

3 4 5 6 7
8 9 10 11 12
13 80 15 16 17
%%
A=[3 4 5 6 7;8 9 10 11 12;13 14 15 16 17]
%b 2nd row with [80 90 100 110 112]
A(2,1:5)=[80 90 100 110 112]
Output
A=

3 4 5 6 7
8 9 10 11 12
13 14 15 16 17

A=

3 4 5 6 7
80 90 100 110 112
13 14 15 16 17
%%
A=[3 4 5 6 7;8 9 10 11 12;13 14 15 16 17]
%c 4th coulmn with 60 110 160
A(1:3,4)=[60 110 160]
Output
A=

3 4 5 6 7
8 9 10 11 12
13 14 15 16 17

A=

3 4 5 60 7
8 9 10 110 12
13 14 15 160 17
%%
A=[3 4 5 6 7;8 9 10 11 12;13 14 15 16 17]
%d 1st row(ist three elements)with 30 40 50
A(1,1:3)=[30 40 50]

Output
A=

3 4 5 6 7
8 9 10 11 12
13 14 15 16 17
A=

30 40 50 6 7
8 9 10 11 12
13 14 15 16 17
%%
%TASK#03
%ADD a column and row vector to a given matrix
P=[3 4 5 1; 5 6 7 2; 7 8 9 3]
%for coulmn addition
x=[0;1;2]
P=[P x]

Output

P=

3 4 5 1
5 6 7 2
7 8 9 3

x=

0
1
2

P=

3 4 5 1 0
5 6 7 2 1
7 8 9 3 2
%%
%for row addition
P=[3 4 5 1; 5 6 7 2; 7 8 9 3]
y=[0 2 3 4]
P=[P;y]
Output
P=

3 4 5 1
5 6 7 2
7 8 9 3
y=

0 2 3 4

P=

3 4 5 1
5 6 7 2
7 8 9 3
0 2 3 4

%%
%task#4
%lab#4
%deleting rows or matrix from the matrix
A=[2 3 5 7 10;11 4 2 0 1;5 6 2 4 3;1 3 0 1 2]
%delete all row of column 1 to 3
A(:,1:3)=[]

Output
A=

2 3 5 7 10
11 4 2 0 1
5 6 2 4 3
1 3 0 1 2

A=

7 10
0 1
4 3
1 2

A=[2 3 5 7 10;11 4 2 0 1;5 6 2 4 3;1 3 0 1 2]


%delete all column of row 2 to 3
A(2:3,:)=[]
Out put

A=
2 3 5 7 10

11 4 2 0 1

5 6 2 4 3

1 3 0 1 2

A=

2 3 5 7 10

1 3 0 1 2

%%
%delete all rows of ist column
A=[2 3 5 7 10;11 4 2 0 1;5 6 2 4 3;1 3 0 1 2]
A(:,1)=[]

Output

A=

2 3 5 7 10

11 4 2 0 1

5 6 2 4 3

1 3 0 1 2

A=

3 5 7 10

4 2 0 1

6 2 4 3

3 0 1 2
%%
%delete all column of 3rd row
A=[2 3 5 7 10;11 4 2 0 1;5 6 2 4 3;1 3 0 1 2]
A(3,:)=[]
Output

A=

2 3 5 7 10

11 4 2 0 1

5 6 2 4 3

1 3 0 1 2

A=

2 3 5 7 10

11 4 2 0 1

1 3 0 1 2
%%
%task#5
%find eigen value and eigen vector of given matrix
A=[1 2 3;7 4 2;1 0 5]
[B,x]=eig(A
Output

A=

1 2 3

7 4 2

1 0 5
B=

0.6492 -0.3734 -0.1805

-0.7546 -0.9124 -0.9032

-0.0959 -0.1673 0.3894

x=

-1.7680 0 0

0 7.2315 0

0 0 4.5365

%%
%task#6
%constructing a matrix of random number
%create a 4*4 matrix of random niumber multiplyuing 10 to eachentry and
%round off all the element
A=rand(4,4)
B=10*A
C=round(B)
Output

A=

0.8147 0.6324 0.9575 0.9572

0.9058 0.0975 0.9649 0.4854

0.1270 0.2785 0.1576 0.8003

0.9134 0.5469 0.9706 0.1419


B=

8.1472 6.3236 9.5751 9.5717

9.0579 0.9754 9.6489 4.8538

1.2699 2.7850 1.5761 8.0028

9.1338 5.4688 9.7059 1.4189

C=

8 6 10 10

9 1 10 5

1 3 2 8

9 5 10 1

>>
%%
%lab#5
%task#1
%manupoolation of polynomial
%P=x^3+4x^2+1
%Q=x^2+2x+1
P=[1 4 0 1]
Q=[0 1 2 3]
addition=P+Q
Subtrection=P-Q
Output

P=

1 4 0 1

Q=
0 1 2 3

addition =

1 5 2 4

Subtrection =

1 3 -2 -2

%%
%task#2
P=[1 4 0 1]
%find the root of P
Roots=roots(P)
%find the derivative of P
derivative=polyder(P)
%find integration of P
integration=polyint(P,4)
%
Output

P=

1 4 0 1

Roots =

-4.0606 + 0.0000i

0.0303 + 0.4953i
0.0303 - 0.4953i

derivative =

3 8 0

integration =

0.2500 1.3333 0 1.0000 4.0000


%%
%multiply the following polynomial
p1=[0 1 3]
p2=[0 1 6]
p3=[1 4 6]
p4=conv(p1,p2)
p5=conv(p4,p3)
output

p1 =

0 1 3

p2 =

0 1 6

p3 =

1 4 6
p4 =

0 0 1 9 18

p5 =

0 0 1 13 60 126 108

>>
%%
%task#4
%devide p by q and q by p
p=[1 0 4 10]
q=[1 3 4 0]
[x,r]=deconv(p,q)
[x,r]=deconv(q,p)
Output

p=

1 0 4 10

q=

1 3 4 0

x=
1

r=

0 -3 0 10

x=

r=

0 3 0 -10

>>
%%
%roots are given find polynomial
r=[2 4 6]
poly(r)
output

r=

2 4 6

ans =

1 -12 44 -48
>>

%13-3-19
%lab#06
%find the polinomial of degree 5 for given data at x=15
%given
x=[2,5,7,9,11,13];
y=[24,156,384,580,650,780];
p=polyfit(x,y,5)
x=15;
p_15=polyval(p,x)

output

p=

0.0601 -1.9751 22.1834 -95.8079 188.4925 -117.5417

p_15 =

1.6668e+03

>>

ans =

ans =

2.8810
Lab#6 tasks
%%
%use labels,title,gtext on graph
clc
clear all
close all
x=-10:10;
y=x.^3+(2.*x.^2)-5;
plot(x,y)
xlabel('x-axis');
ylabel('y-axis');
title('2D graph');
gtext('y=f(x)');
grid on;
axis tight;
output

%%
%task#2
%let y=sin(x) range is -pi/100 to pi/100
%use area command to plot the function
clc
clear all
close all
x=-pi:0.01:pi;
y=sin(x)
area(x,y)
xlabel('x-axis');
ylabel('y-axis');
title('2D Graph')
gtext('y=f(x)');
grid on;
axis tight;
output
%%
%task#3
%write a program to draw the curves for functions in a single figure window
%by using the plot command illusrate the use of different styles and legend
%y1=sin(x)
%y2=sin(x-0.35)
%y3=sin(x-0.55)
%Y4=sin(x-0.85)
clc
close all
clear all
x=0:pi/10:2*pi
y1=sin(x);
y2=sin(x-0.35);
y3=sin(x-0.55);
y4=sin(x-0.85);
plot(x,y1,'r-o',x,y2,'g--*',x,y3,'m-',x,y4,'c-.+','linewidth',2)
legend('y1=sin(x)','y2=sin(x-0.35)','y3=sin(x-0.55)','y4=sin(x-0.85)')
output
%%
%write a program to draw muliple curves in figure window using hold on
command
x=0:pi/10:2*pi;
y1=sin(x);
plot(x,y1,'g--')
hold on
pause(2)
y2=sin(x-0.35);
plot(x,y2,'y--*')
hold on
pause(2)
y3=sin(x-0.55);
plot(x,y3,'r-o')
hold on
pause(2)
y4=sin(x-0.85);
plot(x,y4,'p-.')
pause(2)
hold off
xlabel('x-axis')
ylabel('y-axis')
title('2D graph')
grid on
legend('y1=sinx','y2=sin(x-0.35)','y3=sin(x-0.55)','y4=sin(x-0.85)')
gtext('y1=sin(x)')
gtext('y1=sin(x-0.35)')
gtext('y1=sin(x-0.55)')
gtext('y1=sin(x-0.85)')

output
%%
x=0:pi/10:2*pi;
y1=sin(x);
plot(x,y1,'g--')
y2=sin(x-0.35);
line(x,y2)
y3=sin(x-0.55);
line(x,y3)
y4=sin(x-0.85);
line(x,y4)

%%
% lab#6
%task#6
%Subplot commands
%subplot(no of rows,no of columns,loctation of function)
%Divide the figure window into four sub-divisions and plot the following
%functions.
%a)Plot V vs I where V=4I and I= 1,2,3,4 on the upper left sub-window
%b)Plot y vs x where y=x^2 and x=1,2,3,4 on the upper right sub-window
%c)For t= 0:2*pi in the step of t=pi/60 plot sin(t)vs t on lower left
%sub-window.
%d)For t=0:pi/30:2*pi plot cos(t)vs t on the lower right sub-window.
I=1:4;
V=4.*I;
subplot(2,2,1)
plot(I,V)
%b)
x=1:4;
y=x.^2;
subplot(2,2,2)
plot(x,y)
%c)
t=0:pi/60:2*pi;
f=sin(t);
subplot(2,2,3)
plot(t,f)
%d)
t=0:pi/30:2*pi;
f=cos(t);
subplot(2,2,4)
plot(t,f)
%%
%lab#07
%task#1
%given is y=sin(x) and z=x+5 and x=0:5000 obtain 3D graph using plot3 and
%comet3 commands.
x=0:2*pi:5000;
y=sin(x);
z=x+5;
subplot(1,2,1)
plot3(x,y,z)
subplot(1,2,2)
comet3(x,y,z)
%%
%task#2
%create mesh plot,surface plot and water fall plot in a single window for
%the following function
x=-8:0.5:8;
y=-8:0.5:8;
[X,Y]=meshgrid(x,y);
r=sqrt(X.^2+Y.^2);
Z=sin(r)./r;
subplot(5,1,1)
mesh(X,Y,Z)
subplot(5,1,2)
meshc(X,Y,Z)
subplot(5,1,3)
meshz(X,Y,Z)
subplot(5,1,4)
surf(X,Y,Z)
subplot(5,1,5)
waterfall(X,Y,Z)
%%
%task#2
%create mesh plot,surface plot and water fall plot in a single window for
%the following function
x=-8:0.5:8;
y=-8:0.5:8;
[X,Y]=meshgrid(x,y);
r=sqrt(X.^2+Y.^2);
Z=sin(r)./r;
subplot(3,2,1)
mesh(X,Y,Z)
subplot(3,2,2)
meshc(X,Y,Z)
subplot(3,2,3)
meshz(X,Y,Z)
subplot(3,2,4)
surf(X,Y,Z)
subplot(3,2,5)
waterfall(X,Y,Z)
%%
%task#2
%create mesh plot,surface plot and water fall plot in a single window for
%the following function
x=-8:0.5:8;
y=-8:0.5:8;
[X,Y]=meshgrid(x,y);
r=sqrt(X.^2+Y.^2);
Z=sin(r)./r;
subplot(2,3,1)
mesh(X,Y,Z)
subplot(2,3,2)
meshc(X,Y,Z)
subplot(2,3,3)
meshz(X,Y,Z)
subplot(2,3,4)
surf(X,Y,Z)
subplot(2,3,5)
waterfall(X,Y,Z)

You might also like