You are on page 1of 9

LECTURE 2

x=input('Enter the value of x: ');


for m=1:x
for n=1:x
k=m*n;
fprintf('%4i',k);
pause(0.5)
end
fprintf('\n');
end

LECTURE 3
x=0:pi/100:2*pi
y1=sin(x)
y2=sin(x+120*(pi/180));
y3=sin(x+240*(pi/180));
plot(x,y1,'red+',x,y2,'bluex',x,y3,'greend')
A=[1 4 7 ; 2 5 8 ; 3 6 9]
plot (A)
xlabel('X-COMPONENT')
ylabel('Y-COMPONENT')
title('X-Y GRAPH')
legend('phaseA','phaseB', 'phaseC')

LECTURE 4
N=input('Enter a no: ');
if mod(N,2)==0
disp('Even');
elseif mod(N,3)==0
disp('Divisible by 3');
elseif mod(N,10)==0
disp('Divisible by 10');
else
fprintf('You entered the no. %d\n',N);
end
x=input('Enter a letter:', 's')
switch lower(x)
case{'a','e','i','o','u'}
disp('Vowel');
case 'b'
disp('You type a letter b');
otherwise
fprintf('You type letter %s\n',x);
end
for i=1:2:12
P=P*i;
disp('P')
end;
N=input('Enter a value for N: ');
P=1
for i=1 : N
P=P*i;
end;
fprintf('%d!=%d,\n',N,P);
fprintf('check:%d!=%d\n', prod(1:N));
fprintf('double-check:d!=5d\n', N, factorial (N));

x=1
while(x<=5)
y=x^2;
disp(y);
x=x+1;
end;
while (true)
reply = input('Did you understand?\n','s');
switch lower (reply)
case 'yes'
fprintf('That''s good!\n');
case'perhaps'
fprintf('Maybe not!\n');
case{'no','never'}
fprintf('why?\nAns=');
why;break;
end;
end;

ACTIVITY 2

G=[ 10 40 20; 6 20 1; 3 2 0 ];
G'
L=G'
L(1,3)
L(2,:)
L(:,3)
det(G)
A=[ 0 0 0 0 0 ]
B=[1;1;1]
C=[1 2 3 4 pi]
D=[3:3:27]
E= randi(100,3,3)
MENU 1
k=menu('Select the Operation','x^y','eye(x,y)','xyTable','Quit');
switch (k)
case 1
x2y
case 2
my_eye
case 3
xyTable
otherwise
disp ('Program Terminates');break
end

XY TABLE
x=0 ; y=0
while x<=0
x = input ('Enter the value of x: ');
end
while y<=0
y = input ('Enter the value of y: ');
end
for i=1:x
for j=1:y
A(i,j)=i*j;
end
end
disp (A)

X2Y
x = input ('Enter the value of x: ');
y = input ('Enter the value of y: ');
p=1
if (y>0)
for i=1:y
p=p*x;
end
elseif y>0
for i=1:-(y)
p=p/x;
end
end
fprintf('%i^%i=%i\n',x,y,p);

MY EYE
x = input ('Enter the value of x: ');
y=input('Enter the value of y:');
j=zeros(x,y);
k=1;
if y>=x
while k<=x;
j(k,k)=1;
k=k+1
disp(j)
end
if y<x;
while k<=x;
j(k,k)=1;
k=k+1
disp (j)
end
end
end

PLOTTING GRAPH
2D
1. LINE CHART
2. BAR CHART
3. PIE CHART
a. p1= y1/summation of y * 100%
b. p2= y2/summation of y*100%

LECTURE 6
A.
x = [0.1,0.2,0.3,0.4,0.5];
y = [ 1, 2, 3, 4, 5 ];
z = [ 100, 50, 33.33, 25, 20 ];
plot(x,y,'r+:');
title(' OHM''s LAW GRAPH ');
xlabel('Current');
ylabel('Voltage');
hold on;
plot(x,z, 'b');
legend('Voltage vs. Current', 'Resistance vs. Current');

B.
x = [0.1:0.1:0.5];
y = x*10;
z = 10./x;
plot(x,y,'r+:');
title(' OHM''s LAW GRAPH ');
xlabel('Current');
ylabel('Voltage');
hold on;
plot(x,z, 'b');

C.
x = [0.1,0.2,0.3,0.4,0.5];
y = [ 1, 2, 3, 4, 5 ];
z = [ 100, 50, 33.33, 25, 20 ];
plot(x,y,'r+:');
title(' OHM''s LAW GRAPH ');
xlabel('Current');
ylabel('Voltage');
hold on;
plot(x,z, 'b');
legend('Voltage vs. Current', 'Resistance vs. Current');
hold off;
plot(x,y,'b',x,z,'r');

D.
x = [0.1,0.2,0.3,0.4,0.5];
y = [ 1, 2, 3, 4, 5 ];
z = [ 100, 50, 33.33, 25, 20 ];
plot(x,y,'r+:');
title(' OHM''s LAW GRAPH ');
xlabel('Current');
ylabel('Voltage');
hold on;
plot(x,z, 'b');
legend('Voltage vs. Current', 'Resistance vs. Current');
hold off;
plot(x,y,'b',x,z,'r');
bar(y);
pie3(y);

E.
x=linspace(0,2*pi,360);
y=sin(x);
plot(x,y)
grid on
plot(x,y,'lineWidth', 4)
grid on
plot(x,y, '--','lineWidth', 4)
grid on
z= cos(x);
plot(x,y,'g',x,z,'r','lineWidth', 4)
p=sin(x*pi/2)
hold on
plot(x,p,'g','lineWidth', 3)

PLOT
x=(0: 1: 360)*pi/180;
y1= sin(x);
plot (x,y1,'red--');
title('Phase A');
pause;
y2=sin(x-120*pi/180)
plot(x,y2, 'blue :');
title('Phase B');
pause;
y3=sin(x-240*pi/180);
plot (x,y3,'green');
title('Phase C');
pause;
plot (x,y1,'red--'); title('Phase A');
hold on;
y2=sin(x-120*pi/180)
plot(x,y2, 'blue :'); title('Phase B');
hold on;
y3=sin(x-240*pi/180);
plot (x,y3,'green');
title('3-Phase Sine Waves');
pause;
plot (x,y1,'red--', x,y2, 'blue :', x,y3,'green' )
legend ('phaseA', 'phaseB', 'phaseC');
title( '3-Phase Sine Waves' , 'Font Size' , 18, 'Color','m');
pause;

x=(0: 1: 360)*pi/180;
y1= sin(x);
plot (x,y1,'red--');
title('Phase A');
pause;
y2=sin(x-120*pi/180)
plot(x,y2, 'blue :');
title('Phase B');
pause;
y3=sin(x-240*pi/180);
plot (x,y3,'green');
title('Phase C');
pause;
plot (x,y1,'red--'); title('Phase A');
hold on;
y2=sin(x-120*pi/180)
plot(x,y2, 'blue :'); title('Phase B');
hold on;
y3=sin(x-240*pi/180);
plot (x,y3,'green');
title('3-Phase Sine Waves');
pause;
plot (x,y1,'red--', x,y2, 'blue :', x,y3,'green' )
legend ('phaseA', 'phaseB', 'phaseC');
title( '3-Phase Sine Waves' , 'FontSize' , 18, 'color','m');
pause;
syntax ; subplot(Rows, Columns, Position)
subplot(2,2,1);plot(x,y1,'r'); title('Phase A');
subplot(2,2,2);plot(x,y2,'b:'); title('Phase B');
subplot(2,2,3);plot(x,y3,'g--'); title('Phase C');
subplot(2,2,4);plot(x,y1,'r--' , x,y2,'b:' , x,y3,'g');
title( '3-Phase Sine Waves' , 'FontName' , 'Cambria' , 'FontSize' ,20, 'color','r');
pause;
plot(x,y1, 'r', 'LineWidth',6);
subplot(2,3,1);plot(x,y1,'r','LineWidth',6); title('Phase A');
subplot(2,3,2);plot(x,y2,'b','LineWidth',6); title('Phase B');
subplot(2,3,3);plot(x,y3,'g','LineWidth',6); title('Phase C');
subplot(2,3,[4,5,6]);plot(x,y1,'r' , x,y2,'b' , x,y3,'g');
title('3-Phase Sine Waves');

MATRIX
A(1:2,:)=zeros(2,5)
A(3:4,:)=ones(2,5)
A=[zeros(2,5);ones(2,5)]
Create a 6x6 matrix in which the middle two rows and the middle two columns are 1’s and the
rest are 0’s.

SOLUTION: OUTPUT

A=zeros(6,6); A=
A(3:4,:)=ones(2,6); 0 0 1 1 0 0
A(:,3:4)=ones(6,2) 0 0 1 1 0 0
1 1 1 1 1 1
1 1 1 1 1 1
0 0 1 1 0 0
0 0 1 1 0 0

Create the following matrix A:

A=[1:5, 6:10; 11:15]

Use the matrix A to:


A. Create a five-element row vector named va that contains the elements of the first
row of A.
B. Create a three-element row vector named vb that contains the elements of the
third column of A.
C. Create a eight-element row vector named vc that contains the elements of the
second rows of A and the fourth column of A.
D. Create a six-element row vector named vd that contains the elements of the first
and fifth columns of A.

SOLUTION OUTPUT

A=[1:5;6:10;11:15] va=[1 2 3 4 5]
va=A(1,:) vb=[3;8;13]
vb=A(:,3) vc=[6 7 8 9 10 4 9 14]
vc=[A(2,:), A(:,4)'] vd=[1 6 11 5 10 15]
vd=[A(:,1)', A(:,5)']
pause;
PROBLEM 4

Given are a 5x6 matrix A, a 3x6 matrix B, and a 9 element long vector v.

SOLUTION OUTPUT

A=
A= [ 2:3:17 ; 3:3:18 ; 4:3:19 ; 5:3:20 ; 6:3:21 ]
B= [ 5:5:30 ;30:5:55 ; 55:5:80 ] 2 5 5 10 15 20
V= [99:-1:91] 3 6 9 12 15 18
4 7 30 35 40 45
5 8 95 94 93 92
A(1,[3:6]) = B(1,[1:4])
6 9 60 65 70 75
A(3,[3:6]) = B(2,[1:4])
A(5,[3:6]) = B(3,[2:5])
A(4,[3:6]) = V(1,[5:8])

ANOTHER SOLUTION:

A= [ 2:3:17 ; 3:3:18 ; 4:3:19 ; 5:3:20 ; 6:3:21 ]


B= [ 5:5:30 ;30:5:55 ; 55:5:80 ]
V= [99:-1:91]

C = [A(:,1),A(:,2)]
D = B(1,1:4)
E = A(2,3:6)
F = B(2,1:4)
G = V(1,5:8)
H = B(3,2:5)
A = [([C]),([D;E;F;G;H])]

You might also like