18
18
Session2024-25
Submitted By : Submitted To :
Name: Rohit Dr. Preeti Gulia
Roll No. : 23108
2 Program 2 6-7
i) WAP to perform matrix multiplication
ii)WAP to perform matrix division (left and right).
3 Program 3 8-9
i)WAP to convert the temperature in 3 formats
(c-f, c-k; f-c, f-k; k-c, k-f)
ii) WAP to find minimum and maximum element in an
array.
4 Program 4 10-11
i)WAP to calculate distance and area of a triangle
using herons formula
ii) WAP to create two different vectors of the same
length and add them .
1
8 Write a Program to implement XOR function using 18-20
McCulloch Pitts Model.
The truth table for XOR function is as follows:
X1 X2 Y
0 0 0
0 1 1
1 0 1
1 1 0
2
PROGRAM 1
Code:
A = [1, 2, 3; 4, 5, 6];
disp('Original Array:');
disp(A);
disp('Transpose:');
disp(A');
output:
3
ii) WAP to concatenation of two array
Code:
A = [1, 2, 3];
B = [4, 5, 6];
C = [A, B]; % Horizontal concatenation
disp('Concatenated Array:');
disp(C);
output:
4
iii) WAP to perform mathematical operation like addition subtraction
multiplication and division of 2 arrays.
Code:
A = [1, 2, 3];
B = [4, 5, 6];
disp('Addition:'); disp(A + B);
disp('Subtraction:'); disp(A - B);
disp('Multiplication:'); disp(A .* B); % Element-wise
disp('Division:'); disp(A ./ B); % Element-wise
Output:
5
PROGRAM 2
Code:
A = [1, 2; 3, 4];
B = [2, 0; 1, 3];
disp('Matrix Multiplication:');
disp(A * B);
Output:
6
ii) WAP to perform matrix division (left and right)
Code:
A = [2, 4; 6, 8];
B = [1, 2; 3, 4];
disp('Left Division:');
disp(A \ B);
disp('Right Division:');
disp(A / B);
output:
7
PROGRAM 3
CODE
disp('Choose your Input type');
b=input('1 for Kelvin, 2 for Celsius, 3 for Fahrenheit : ');
if b==1
num=input('Enter temp in Kelvin : ');
c=num-273;
f=(num-273)*9/5 + 32;
disp(["c = "+c])
disp(["f="+f])
end
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
Output:
8
ii) WAP to find minimum and maximum element in an array
Code:
A = [1, 5, 3, 8, 2];
disp(['Minimum Element: ', num2str(min(A))]);
disp(['Maximum Element: ', num2str(max(A))]);
Output:
9
PROGRAM 4
i) WAP to calculate distance and area of a triangle using herons formula.
Code:
Output:
10
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:
11
PROGRAM 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
12
Output:
13
PROGRAM 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
14
disp(w1);
disp(w2);
disp('Threshold Value');
disp(thresholdValue);
OUTPUT:
15
PROGRAM 7 : WAP to implement ANDNOT Function Using McCulloch Pitts
Model.
The truth table for the ANDNOT function is as follows:
X1 X2 Y
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
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 : ');
16
w1=input('Enter the weight of x1 : ');
w2=input('Enter the weight of x2 : ');
thresholdValue=input('Enter the threshold value: ');
end
end
OUTPUT:
17
PROGRAM 8 : WAP a Program to implement XOR function using McCulloch
Pitts Model.
CODE
18
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);
disp(V2);
19
disp('Threshold Value');
disp(thresholdValue);
OUTPUT:
20
PROGRAM 9 : Write a program to plot various membership functions.
CODE
%Triangular Membership function
x=(0.0:1.0:10.0)';
y1=trimf(x,[1 3 5]);
subplot(6,2,1)
plot(x,y1);
title('Triangular membership function');
xlabel('X');
ylabel('Y');
21
y12 = gauss2mf(x,[2 5 1 7]);
y13 = gauss2mf(x,[2 6 1 6]);
y14 = gauss2mf(x,[2 7 1 5]);
y15 = gauss2mf(x,[2 8 1 4]);
subplot(6,2,5)
plot(x,[y11 y12 y13 y14 y15]);
title('Gaussian combination membership function');
xlabel('X');
ylabel('Y');
23
OUTPUT:
24
PROGRAM : 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
%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) = [];
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)
25
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
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
26
y3(:,1) = [];
disp('Bold Union operation');
disp(y3);
%Equality operation
disp('Equality operation');
if A == B
disp('Both sets are Equal');
else
disp('Given sets are not Equal');
end
27
OUTPUT:
28
PROGRAM 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
OUTPUT:
29
PROGRAM 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
OUTPUT:
30