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

Ritesh Matlab

This document is a practical file for a Software Lab course at Maharshi Dayanand University, containing various programming assignments in MATLAB. The assignments include tasks such as array operations, matrix manipulations, temperature conversions, and implementing logical functions using the McCulloch-Pitts model. Each program is accompanied by code snippets and expected outputs, structured in a clear index format.

Uploaded by

Ritesh
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 views32 pages

Ritesh Matlab

This document is a practical file for a Software Lab course at Maharshi Dayanand University, containing various programming assignments in MATLAB. The assignments include tasks such as array operations, matrix manipulations, temperature conversions, and implementing logical functions using the McCulloch-Pitts model. Each program is accompanied by code snippets and expected outputs, structured in a clear index format.

Uploaded by

Ritesh
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/ 32

MAHARSHI DAYANAND UNIVERSITY

ROHTAK, HARYANA (124001)

Department of Computer Science & Applications

PRACTICAL FILE BASED ON


SOFTWARE LAB 2: 21MCA23CL2
(PAPER CODE : 21MCA23C2)
(Session : 2023-2025)

Submitted By Submitted To
Ritesh Dr. Preeti Gulia
MCA , 3rd sem
Roll: 23121
INDEX
Sr. No. PROGRAMS Page No. Remarks
.

i) WAP to find the transpose of the array. 1,2


ii) WAP to concatenation of two array.
1)
iii) WAP to perform mathematical operation
like addition substraction multiplication
and division of two arrays.

2) i) WAP to perform matrix multiplication.


3,4
ii) WAP to perform matrix division (left and
right).

3) i) WAP to convert the temperature in 3 5,6


formats ( kelvin to Fahrenheit,Celsius to
Kelvin, Fahrenheit to Celsius, F to K & Kelvin
to C, K to F ).
ii) WAP to find minimum and maximum
element in an array.

4) i) WAP to calculate distance and area of a 7,8


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, 9,10
for).

6) WAP to implement AND Function using 11,12


McCulloch Pitts Model.

7) WAP to implement ANDNOT Function Using 13,14


McCulloch Pitts Model.

The truth table for the ANDNOT function is as follows:

X1 X2 X3

0 0 0

0 1 0

1 0 1

1 1 0

8) Write a Program to implement XOR function using


15 to 17
McCulloch Pitts Model.

The truth table for XOR function is as follows:

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.

10) Write a program in fuzzy set operation. 21 to 24

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

1
Output:-

1) (iii) WAP to perform mathematical operation like addition


substraction multiplication and division of two arrays.

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:-

2) (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);

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:-

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

5
Output:-

3) (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:-

6
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:-

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

% 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

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.

The truth table for the ANDNOT function is as


follows:

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

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


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

Output:-

14
8) WAP to implement XOR Function using McCulloch
Pitts Model.

The truth table for XOR function is as follows:

X1 X2 X3

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

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:

%Triangular Membership function


x=(0.0:1.0:10.0)';
y1=trimf(x,[1 3 5]);
subplot(6,2,1)
plot(x,y1);
xlabel('Triangular membership function');

%Trapezoidal Membership function


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

%Bell Shaped Membership function


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

%Gaussian curve membership function


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

%Gaussian combination membership function


y11 = gauss2mf(x,[2 4 1 8]);
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]);

18
xlabel('Gaussian combination membership function');

%S-shaped membership function


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

%Z-Shaped membership function


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

%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])
xlabel('Product of two sigmoidal membership functions');

%Difference between two sigmoidal membership function


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

%PI - membership function


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

%Sigmoidal membership function


y11 = sigmf(x,[2 4]);

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

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); %number of columns in A


s2 = length(B); %number of columns in B
C=[A B];
s3=s1+s2; %number of columns in both array

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

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

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:

Create a script file as follows or directly type these


commands in command window:

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:

Create a script file as follows or directly type these


commands in command window:

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

You might also like