Ritesh Matlab
Ritesh Matlab
Submitted By Submitted To
Ritesh Dr. Preeti Gulia
MCA , 3rd sem
Roll: 23121
INDEX
Sr. No. PROGRAMS Page No. Remarks
.
X1 X2 X3
0 0 0
0 1 0
1 0 1
1 1 0
X1 X2 X3
0 0 0
0 1 1
1 0 1
1 1 0
9) Write a program to plot various membership 18 to 20
functions.
11) Using the plot command for multiple plots, plot 25,26
y=Sin(x) and y=Cos(x) on the same graph for
values of x defined by: 0: pi/30:2*pi.
12) Using the plot command for single plot and hold 27,28
commands, plot y=Sin(x) and y=Cos(x) on the
same graph for values of x defined by:
0:pi/30:2*pi.
MATLAB
1) (i) WAP to find the transpose of the array.
Code:-
A = [1, 2, 3; 4, 5, 6];
disp('Original Array:');
disp(A);
disp('Transpose:');
disp(A');
0utput:-
1
Output:-
Code: -
a = 10;
b = 5;
disp(['Addition: ', num2str(a + b)]);
disp(['Subtraction: ', num2str(a - b)]);
disp(['Multiplication: ', num2str(a * b)]);
disp(['Division: ', num2str(a / b)]);
Output:-
2
2) (i) WAP to perform matrix multiplication.
Code:-
A = [1, 2; 3, 4];
B = [2, 0; 1, 3];
disp('Matrix Multiplication:');
disp(A * B);
Output:-
3
Output:-
4
3) (i) WAP to convert the temperature in 3 formats ( Kelvin
to Fahrenheit, Celsius to Kelvin , Fahrenheit to Celsius,
F to K & Kelvin to C, K to F ).
Code:-
if b==2
num=input('Enter temp in Celsius : ');
c=num+273;
f=(num *9/5) + 32;
disp(["k = "+c])
disp(["f="+f])
end
if b==3
num=input('Enter temp in Fahrenheit : ');
c=(num - 32) * 5/9;
f=(num - 32) * 5/9 + 273;
disp(["c = "+c])
disp(["k="+f])
end
5
Output:-
Code:-
A = [1, 5, 3, 8, 2];
disp(['Minimum Element: ', num2str(min(A))]);
disp(['Maximum Element: ', num2str(max(A))]);
Output:-
6
4) (i) WAP to calculate distance and area of a triangle using
herons formula.
Code:-
Output:-
7
4) (ii) WAP to create two different vectors of the same
length and add them.
Code:-
A = [1, 2, 3];
B = [4, 5, 6];
C = A + B;
disp('Added Vectors:');
disp(C);
Output:-
8
5) WAP to implement all the loops (while, do while, for).
Code:-
% For Loop
disp('For Loop:');
for i = 1:5
disp(i);
end
% While Loop
disp('While Loop:');
i = 1;
while i<= 5
disp(i);
i = i + 1;
end
9
Output:
10
6) WAP to Implement AND function using McCulloch
Pitts model.
Code:-
x1=[0 1 0 1];
x2=[0 0 1 1];
z=[0 0 0 1];
y=[0 0 0 0];
w1=input('Enter the weight of x1 : ');
w2=input('Enter the weight of x2 : ');
thresholdValue=input('Enter the threshold value: ');
flag = true;
while flag
zin = x1*w1+x2*w2;
for i=1:4
if thresholdValue < zin(i)
y(i) = 1;
else
y(i) = 0;
end
end
disp('Output of the net is: ');
display(y);
if z == y
flag = false;
else
disp('MP net is not learning, please enter the weights and threshold value
again : ');
w1=input('Enter the weight of x1 : ');
w2=input('Enter the weight of x2 : ');
thresholdValue=input('Enter the threshold value: ');
end
end
11
disp('Mcculloch Pitts model for AND function is : ');
disp('Weights');
disp(w1);
disp(w2);
disp('Threshold Value')
disp(thresholdValue);
OUTPUT:-
12
7) WAP to implement ANDNOT Function using McCulloch
Pitts Model.
X1 X2 X3
0 0 0
0 1 0
1 0 1
1 1 0
Code:-
x1=[0 0 1 1];
x2=[0 1 0 1];
z=[0 0 1 0];
y=[0 0 0 0];
w1=input('Enter the weight of x1 : ');
w2=input('Enter the weight of x2 : ');
thresholdValue=input('Enter the threshold value: ');
flag = true;
while flag
zin = x1*w1+not(x2)*w2;
for i=1:4
if thresholdValue < zin(i)
y(i) = 1;
else
y(i) = 0;
end
end
13
disp('Output of the net is: ');
display(y);
if z == y
flag = false;
else
disp('MP net is not learning, please enter the weights and threshold
value again : ');
w1=input('Enter the weight of x1 : ');
w2=input('Enter the weight of x2 : ');
thresholdValue=input('Enter the threshold value: ');
end
end
Output:-
14
8) WAP to implement XOR Function using McCulloch
Pitts Model.
X1 X2 X3
0 0 0
0 1 1
1 0 1
1 1 0
Code:-
15
y1(i) = 0;
else
y1(i) = 1;
end
if zin2(i) < thresholdValue
y2(i)=0;
else
y2(i)=1;
end
end
yin = y1*V1 + y2*V2;
for i=1:4
if yin(i)>=thresholdValue
y(i)=1;
else
y(i)=0;
end
end
disp('Output of network is: ');
disp(y);
if y == z
flag = false;
else
disp('Network is not learning, Enter the vales again: ');
w11 = input('Enter the Weight w11 : ');
w12 = input('Enter the Weight w12 : ');
w21 = input('Enter the Weight w21 : ');
w22 = input('Enter the Weight w22 : ');
V1 = input('Enter the Weight V1 : ');
V2 = input('Enter teh Weight V2 : ');
thresholdValue = input('Enter Threshold Value : ');
end
end
disp('Ouput of Mcculloch Pitts model for XOR function:');
disp('Weights for z1');
disp(w11);
disp(w21);
disp('Weights for z2');
disp(w12);
disp(w22);
disp('Weights for y');
disp(V1);
16
disp(V2);
disp('Threshold Value');
disp(thresholdValue);
OUTPUT:
17
9) Write a program to plot various membership functions.
Code:
18
xlabel('Gaussian combination membership function');
19
subplot(6,2,11)
plot(x,y11);
ylim([-0.05 1.05])
xlabel('Sigmoidal membership function');
OUTPUT:
20
10) Write a program in fuzzy set operation.
Code:
%Let the two fuzzy sets are A and B (Matrix with 2 rows)
%Here 1st row shows the element and 2nd for membership
display(A);
display(B);
%Union Operation
y1=zeros(1);
for i = 1:s3
flag = true;
for j = 1:length(y1)
if y1(1,j) == C(1,i)
y1(2,j) = max(y1(2,j), C(2,i));
flag = false;
break;
end
end
if flag
y1(1,end+1) = C(1,i);
y1(2,end) = C(2,i);
end
end
y1(:,1) = [];
21
disp('Union operation');
disp(y1);
%Intersection Operation
y2=zeros(1);
for i = 1:s3
flag = true;
for j = 1:length(y2)
if y2(1,j) == C(1,i)
y2(2,j) = min(y2(2,j), C(2,i));
flag = false;
break;
end
end
if flag
y2(1,end+1) = C(1,i);
y2(2,end) = C(2,i);
end
end
y2(:,1) = [];
disp('Intersection operation');
disp(y2);
%Complement Operation
A_comp = A;
B_comp = B;
for i = 1:s1
A_comp(2,i) = 1-A_comp(2,i);
B_comp(2,i) = 1-B_comp(2,i);
end
disp('Complement Operation');
disp('Complement of A');
disp(A_comp);
disp('Complement of B');
disp(B_comp);
%Bold Union
22
y3=zeros(1);
for i = 1:s3
flag = true;
for j = 1:length(y3)
if y3(1,j) == C(1,i)
y3(2,j) = min(1,(y3(2,j)+C(2,i)));
flag = false;
break;
end
end
if flag
y3(1,end+1) = C(1,i);
y3(2,end) = C(2,i);
end
end
y3(:,1) = [];
disp('Bold Union operation');
disp(y3);
23
%Equality operation
disp('Equality operation');
if A == B
disp('Both sets are Equal');
else
disp('Given sets are not Equal');
end
OUTPUT:
24
11) Using the plot command for multiple plots, plot y=Sin(x)
and y=Cos(x) on the same graph for values of x defined
by: 0: pi/30:2*pi.
Code:
x = 0:pi/30:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1,x,y2);
hold on
legends(‘sin(x)’,’cos(x)’);
xlabel(‘x’);
ylabel(‘y’);
hold off
25
OUTPUT:
26
12) Using the plot command for single plot and hold
commands, plot y=Sin(x) and y=Cos(x) on the same
graph for values of x defined by : 0:pi/30:2*pi.
Code:
x = 0:pi/30:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1);
hold on
plot(x,y2);
legends(‘sin(x)’,’cos(x)’);
xlabel(‘x’);
ylabel(‘y’);
hold off
27
OUTPUT:
28