0% found this document useful (0 votes)
2 views

18

The document is a practical file for a Software Lab course at Maharshi Dayanand University, detailing various programming assignments related to array operations, matrix manipulations, temperature conversions, geometric calculations, and logical functions using the McCulloch Pitts model. It includes code snippets and expected outputs for each program, covering topics such as loops, fuzzy set operations, and plotting membership functions. The file is submitted by a student named Rohit to Dr. Preeti Gulia for the MCA 3rd semester.

Uploaded by

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

18

The document is a practical file for a Software Lab course at Maharshi Dayanand University, detailing various programming assignments related to array operations, matrix manipulations, temperature conversions, geometric calculations, and logical functions using the McCulloch Pitts model. It includes code snippets and expected outputs for each program, covering topics such as loops, fuzzy set operations, and plotting membership functions. The file is submitted by a student named Rohit to Dr. Preeti Gulia for the MCA 3rd semester.

Uploaded by

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

Maharshi Dayanand University, Rohtak

Department of Computer Science


and Applications

Practical File based on


SOFTWARE LAB - 6 : 20MCA22CL2
( PAPER CODE : 20MCA23C2)

Session2024-25

Submitted By : Submitted To :
Name: Rohit Dr. Preeti Gulia
Roll No. : 23108

MCA 3rd semester


Index

Sr. Contents Page No. Signature


No.
1 Program 1 3-5
i) WAP to find the transpose of the array
ii) WAP to concatenation of two array
iii) WAP to perform mathematical operation like
addition subtraction multiplication and division of 2
arrays

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 .

5 WAP to implement all the loops (while, do while, for). 12-13


6 WAP to implement AND Function using McCulloch 14-15
Pitts Model.

7 WAP to implement ANDNOT Function Using 16-17


McCulloch Pitts Model.
The truth table for the ANDNOT function is as
follows:
X1 X2 Y
000
0 1 0
1 0 1
1 1 0

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

9 Write a program to plot various membership functions. 21-24


10 Write a program in fuzzy set operation. 25-28
11 Using the plot command for multiple plots, plot 29
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 30


commands, plot y=Sin(x) and y=cos(x) on the same
graph for values of x defined by: 0:pi/30:2*pi.

2
PROGRAM 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');

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

i) WAP to perform matrix multiplication

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

i) WAP to convert the temperature in 3 formats


(c-f, c-k; f-c, f-k; k-c, k-f)

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:

a = 3; b = 4; c = 5; % Sides of the triangle


s = (a + b + c) / 2; % Semi-perimeter
area = sqrt(s * (s - a) * (s - b) * (s - c));
disp(['Area of Triangle: ', num2str(area)]);

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

% Do While Loop (Simulated using while in MATLAB)


disp('Do While Loop:');
i = 1;
while true
disp(i);
i = i + 1;
if i > 5
break;
end
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

disp('Mcculloch Pitts model for AND function is : ');


disp('Weights');

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

disp('Mcculloch Pitts model for ANDNOT function is : ');


disp('Weights');
disp(w1);
disp(w2);
disp('Threshold Value');
disp(thresholdValue);

OUTPUT:

17
PROGRAM 8 : WAP a Program to implement XOR function using 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

CODE

disp('Enter the weights');


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 the Weight V2 : ');
thresholdValue = input('Enter Threshold Value : ');
x1=[0 0 1 1];
x2=[0 1 0 1];
z=[0 1 1 0];
y=[0 0 0 0];
y1=[0 0 0 0];
y2=[0 0 0 0];
flag = true;
while flag
zin1 = x1*w11 + x2*w21;
zin2 = x1*w21 + x2*w22;
for i = 1:4
if zin1(i) < thresholdValue
y1(i) = 0;
else
y1(i) = 1;
end
if zin2(i) < thresholdValue

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

%Trapezoidal Membership function


y2=trapmf(x,[1 3 5 7]);
subplot(6,2,2)
plot(x,y2);
title('Trapezoidal Membership function');
xlabel('X');
ylabel('Y');

%Bell Shaped Membership function


y3=gbellmf(x,[1 2 5])';
subplot(6,2,3)
plot(x,y3);
title('Bell Shaped Membership function');
xlabel('X');
ylabel('Y');

%Gaussian curve membership function


y4=gaussmf(x,[2 5]');
subplot(6,2,4)
plot(x,y4);
title('Gaussian curve membership function');
xlabel('X');
ylabel('Y');

%Gaussian combination membership function


y11 = gauss2mf(x,[2 4 1 8]);

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

%S-shaped membership function


y6 = smf(x,[1 8]);
subplot(6,2,6)
plot(x,y6);
ylim([-0.05 1.05])
title('S-shaped membership function');
xlabel('X');
ylabel('Y');

%Z-Shaped membership function


y7 = zmf(x,[3 7]);
subplot(6,2,7)
plot(x,y7);
ylim([-0.05 1.05])
title('Z-Shaped membership function');
xlabel('X');
ylabel('Y');

%Product of two sigmoidal membership functions


y8 = psigmf(x,[2 3 -5 8]);
subplot(6,2,8)
plot(x,y8);
ylim([-0.05 1.05])
title('Product of two sigmoidal membership functions');
xlabel('X');
ylabel('Y');

%Difference between two sigmoidal membership function


y9 = dsigmf(x,[5 2 5 7]);
subplot(6,2,9)
plot(x,y9);
22
title('Difference between two sigmoidal membership function');
xlabel('X');
ylabel('Y');

%PI - membership function


y10 = pimf(x,[1 4 5 10]);
subplot(6,2,10)
plot(x,y10);
ylim([-0.05 1.05])
title('PI - membership function');
xlabel('X');
ylabel('Y');

%Sigmoidal membership function


y11 = sigmf(x,[2 4]);
subplot(6,2,11)
plot(x,y11);
ylim([-0.05 1.05])
title('Sigmoidal 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

A = [10 20 30 40 50; 0.2 0.4 0.8 0.9 0.1];


B = [11 20 30 54 50; 0.3 0.4 0.6 0.9 0.1];
display(A);
display(B);
s1 = length(A);
s2 = length(B);
C=[A B];
s3=s1+s2;

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

%Bold intersection operation


y4=zeros(1);
for i = 1:s3
flag = true;
for j = 1:length(y4)
if y4(1,j) == C(1,i)
y4(2,j) = max(0,(y4(2,j)-C(2,i)));
flag = false;
break;
end
end
if flag
y4(1,end+1) = C(1,i);
y4(2,end) = C(2,i);
end
end
y4(:,1) = [];
disp('Bold intersection operation');
disp(y4);

%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

You might also like