Professional Documents
Culture Documents
Lab File
Lab File
Technology Jalandhar
Submi ed by:
Name: Saloni Aggarwal
Roll No. 21124095
Group – A4
Submi ed to
Dr Neeraj Kumar
1
2
Index
3
4
Lab – 1 (Part -1)
Logic Gates in Python.
1. AND Gate:
The AND gate produces a high output (1) only if all its inputs are high (1).
Python Code:
def and_gate(input1, input2): return input1 and input2
2. OR Gate:
The OR gate produces a high output (1) if at least one of its inputs is high (1).
Python Code:
def or_gate(input1, input2): return input1 or input2
3. NOT Gate:
The NOT gate produces an output that is the opposite of its input.
Python Code:
def not_gate(input1): return not input1
4. XOR Gate:
The XOR (exclusive OR) gate produces a high output (1) only if the number of high inputs is odd.
Python Code:
def xor_gate(input1, input2): return (input1 or input2) and not (input1 and input2)
5. NAND Gate:
The NAND gate produces a low output (0) only if all its inputs are high (1).
Python Code:
def nand_gate(input1, input2): return not (input1 and input2)
6. NOR Gate:
The NOR gate produces a low output (0) if at least one of its inputs is high (1).
5
PYTHON CODE:
def not_gate(input1):
def main():
# Example inputs
input_a = True
input_b = False
# AND gate
# OR gate
# NOT gate
result_not = not_gate(input_a)
# XOR gate
6
print(f"XOR gate: {input_a} XOR {input_b} = {result_xor}")
# NAND gate
# NOR gate
if __name__ == "__main__":
main()
OUTPUT :
7
Lab – 2
Implementa on of Fuzzy and Crisp in Python
THEORY:
Crisp Sets:
Defini on: Crisp sets, also known as classical or tradi onal sets, deal with well-defined,
dis nct elements.
Characteris cs: Each element either belongs to the set (membership value of 1) or does not
belong (membership value of 0).
Representa on: Typically represented using mathema cal nota on, where membership is
binary (0 or 1).
Fuzzy Sets:
Defini on: Fuzzy sets introduce the concept of par al membership, allowing elements to
belong to a set to varying degrees between 0 and 1.
Characteris cs: Membership values are not strictly binary but can be any real number
between 0 and 1, expressing the degree of belongingness.
Representa on: O en represented using linguis c terms (e.g., "very high," "medium,"
"low") to capture the uncertainty or vagueness.
Example: The set of tall people in a room, where height membership values can range from 0
to 1 based on a defined criterion.
Crisp sets have clear, well-defined boundaries for membership (either in or out).
Fuzzy sets accommodate uncertainty and vagueness, allowing for par al membership and
gradual transi ons between in and out.
PYTHON CODE:
import numpy as np
8
return 0
def sigmoid(x):
return float(1 / (1 + np.exp(-x)))
def fuzzy():
tas = float(input("Enter the taste"))
beh = float(input("Enter the behav"))
print(sigmoid(tas)*10*sigmoid(beh)*10)
print()
def main():
tas = int(input("Enter the taste[0-1]"))
beh = int(input("Enter the behav[0-1]"))
print(Crisp(tas, beh))
fuzzy()
if __name__ == "__main__":
main()
OUTPUT:
9
LAB: 3 Implemen ng Fuzzy Logic in MATLAB
CODE:
OUTPUT:
10
Fig 3.1 All cases with values of service and food.
Fig 3.2 All rules are shown with weight and name.
11
Fig 3.3 All input and output with func on and parameters.
12
LAB 4: Converting coloured image to grayscale in MATLAB.
THEORY:
Converting a colored image to grayscale in MATLAB involves transforming each pixel's RGB (Red,
Green, Blue) color values into a single grayscale intensity value. This process can be done using
several methods, but a common one is luminance-based conversion, which mimics the human
perception of light.
RGB to Grayscale Conversion: In a colored image, each pixel is represented by three values
corresponding to its Red, Green, and Blue components. Grayscale images, on the other hand,
represent each pixel with a single intensity value representing its brightness.
CODE:
RGB = imread('1911265.jpg');
imshow(RGB)
[h,s,v] = imsplit(HSV);
saturationFactor = 2;
s_sat = s*saturationFactor;
HSV_sat = cat(3,h,s_sat,v);
imshow(HSV_sat)
RGB_sat = hsv2rgb(HSV_sat);
I = rgb2gray(RGB);
Figure
imshow(I)
OUTPUT:
13
LAB 5: Creating a perceptron using MATLAB.
THEORY:
A perceptron is a basic unit of a neural network used for binary classification tasks. It takes
multiple input values, each with an associated weight, computes a weighted sum, and applies
an activation function (often a step function) to produce a binary output. The weights are
adjusted during training based on the error between the predicted output and the true output
using a learning algorithm (e.g., perceptron learning rule). Perceptrons are simple and
efficient for linearly separable problems but have limitations for complex tasks.
CODE:
% Create a perceptron
net = perceptron;
14
OUTPUT:
Fig 5.1 Ini al values of perceptron’s variables and representa on of perceptron with ac va on func on step.
15