Unit III: Fuzzy Logic – I (Introduction)

Ultimate Deep Understanding Notes + Best Python Code Examples (2025 Standards) Perfect for Exams, Interviews & Real-World Applications

Fuzzy Logic – I (Introduction)

Unit III: Fuzzy Logic – I (Introduction)

Ultimate Deep Understanding Notes + Best Python Code Examples (2025 Standards)
Perfect for Exams, Interviews & Real-World Applications

1. Why Fuzzy Logic Exists – The Real-World Problem

Crisp Logic (Boolean) Fuzzy Logic (Zadeh 1965)
A person is either Tall or Not Tall A person can be 0.8 Tall (180 cm)
Temperature is either Hot or Cold Temperature can be 0.7 Hot (35°C)
Only 0 or 1 → Binary Continuum between 0 and 1 → Human-like reasoning

Fuzzy Logic = Computing with Words
→ Used in washing machines, ABS brakes, cameras, medical diagnosis, stock trading, AI controllers.

2. Crisp Set vs Fuzzy Set – Core Difference Table

Property Crisp Set Fuzzy Set
Membership Only 0 or 1 Any value in [0,1]
Example A = {x ≥ 180} → {180,181,...} A = Tall = {(170,0.3), (180,0.8), (190,1.0)}
Boundary Sharp Gradual (no sharp boundary)
Representation Characteristic function χ(x) Membership function μ_A(x) ∈ [0,1]

3. Fuzzy Set Operations – With Code & Visualization

import numpy as np
import matplotlib.pyplot as plt

# Define universe
x = np.linspace(0, 100, 1000)

# Membership functions
def triangular(x, a, b, c):
    return np.maximum(0, np.minimum((x - a)/(b - a), (c - x)/(c - b)))

def trapezoidal(x, a, b, c, d):
    return np.maximum(0, np.minimum(np.minimum((x - a)/(b - a), 1), (d - x)/(d - c)))

# Example fuzzy sets
A = triangular(x, 20, 50, 80)   # "Medium Income"
B = triangular(x, 60, 80, 100)  # "High Income"

# Operations
union = np.maximum(A, B)                    # A ∪ B
intersection = np.minimum(A, B)             # A ∩ B
complement_A = 1 - A                        # ¬A
difference = np.minimum(A, 1 - B)           # A - B
algebraic_product = A * B                   # A · B
algebraic_sum = A + B - A*B                 # A + B (bounded)

# Plot
plt.figure(figsize=(12, 8))
plt.plot(x, A, label='Medium Income (A)', linewidth=3)
plt.plot(x, B, label='High Income (B)', linewidth=3)
plt.plot(x, union, '--', label='A ∪ B (Union)', linewidth=3)
plt.plot(x, intersection, ':', label='A ∩ B (Intersection)', linewidth=3)
plt.plot(x, complement_A, '-.', label='¬A (Complement)', linewidth=3)
plt.legend(fontsize=12)
plt.title('Fuzzy Set Operations', fontsize=16)
plt.xlabel('Income (thousands)', fontsize=14)
plt.ylabel('Membership Degree μ(x)', fontsize=14)
plt.grid(True, alpha=0.3)
plt.show()

4. Properties of Fuzzy Sets (Memorize This Table!)

Property Crisp Sets Fuzzy Sets Formula / Meaning
Commutative Yes Yes A ∪ B = B ∪ A
Associative Yes Yes (A ∪ B) ∪ C = A ∪ (B ∪ C)
Distributive Yes Yes A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
Idempotent Yes Yes A ∪ A = A
Identity Yes Yes A ∪ ∅ = A, A ∩ U = A
Involution Yes Yes ¬(¬A) = A
De Morgan’s Laws Yes Yes ¬(A ∪ B) = ¬A ∩ ¬B
Excluded Middle (A ∪ ¬A = U) Yes No Not true in fuzzy!
Non-Contradiction (A ∩ ¬A = ∅) Yes No Not true in fuzzy!

Key Point: Fuzzy sets violate classical logic’s excluded middle → allows partial truth!

5. Fuzzy Relations – With Best Example

A fuzzy relation R from X→Y is a fuzzy set in X×Y.

Example: "x is much taller than y"

# Height in cm
X = [150, 160, 170, 180, 190]
Y = [155, 165, 175, 185, 195]

# Fuzzy relation matrix: R(x,y) = how much x is taller than y
R = np.zeros((5,5))
for i in range(5):
    for j in range(5):
        diff = X[i] - Y[j]
        if diff <= 0:
            R[i,j] = 0
        elif diff <= 20:
            R[i,j] = diff / 20
        else:
            R[i,j] = 1.0

print("Fuzzy Relation: 'x is much taller than y'")
print(np.round(R, 2))

Output:

[[0.   0.   0.   0.   0.  ]
 [0.   0.   0.25 0.75 1.  ]
 [0.   0.   0.   0.5  1.  ]
 [0.   0.   0.   0.   1.  ]
 [0.   0.   0.   0.   0.  ]]

6. Max-Min Composition of Fuzzy Relations

If R: X→Y and S: Y→Z → R∘S: X→Z
(T = R ∘ S)[i,k] = max over j [ min(R[i,j], S[j,k]) ]

# Example: R = "x is similar to y", S = "y is friend of z"
R = np.array([[1.0, 0.8, 0.3],
              [0.8, 1.0, 0.4],
              [0.3, 0.4, 1.0]])

S = np.array([[1.0, 0.2],
              [0.6, 1.0],
              [0.1, 0.9]])

# Max-Min composition
T = np.zeros((3,2))
for i in range(3):
    for k in range(2):
        T[i,k] = np.max([np.min([R[i,j], S[j,k]]) for j in range(3)])

print("R ∘ S = 'x is indirect friend of z via similarity'")
print(np.round(T, 2))

7. Fuzzy to Crisp Conversion (Defuzzification Methods)

When you need a single crisp output from fuzzy system.

Method Formula Best For Code Example
Centroid (COG) z* = ∫μ(z)z dz / ∫μ(z) dz Most accurate Yes
Bisector Area left = Area right Balanced Yes
Mean of Maximum (MOM) Average of z with max μ Fast Yes
Smallest of Max (SOM) Min z with max μ Conservative Yes
Largest of Max (LOM) Max z with max μ Aggressive Yes
def centroid(x, mf):
    return np.sum(x * mf) / np.sum(mf + 1e-8)

def mom(x, mf):
    max_val = np.max(mf)
    return np.mean(x[mf >= max_val - 1e-8])

# Example output fuzzy sets
output_mf = triangular(x, 30, 60, 90) * 0.8 + triangular(x, 50, 70, 90) * 1.0
output_mf = np.clip(output_mf, 0, 1)

print(f"Centroid defuzzification: {centroid(x, output_mf):.2f}")
print(f"MOM defuzzification: {mom(x, output_mf):.2f}")

8. Best Real-World Example: Fuzzy Temperature Controller

class FuzzyTemperatureController:
    def __init__(self):
        self.x = np.linspace(-20, 50, 700)

    def cold(self, t): return trapezoidal(t, -30, -20, -5, 5)
    def warm(self, t): return triangular(t, 5, 20, 35)
    def hot(self, t): return trapezoidal(t, 25, 35, 45, 60)

    def heater_output(self, temp):
        c = self.cold(temp)
        w = self.warm(temp)
        h = self.hot(temp)

        # Rules:
        # If COLD → Heater HIGH
        # If WARM → Heater MEDIUM
        # If HOT  → Heater LOW

        high = triangular(self.x, 50, 80, 100)  # High
        med = triangular(self.x, 20, 50, 80)   # Medium
        low = triangular(self.x, 0, 10, 40)    # Low

        # Aggregation
        aggregated = np.maximum(np.minimum(c, high),
                                np.maximum(np.minimum(w, med), np.minimum(h, low)))

        # Defuzzify
        return centroid(self.x, aggregated)

# Test
controller = FuzzyTemperatureController()
print(f"At 10°C → Heater: {controller.heater_output(10):.1f}%")
print(f"At 25°C → Heater: {controller.heater_output(25):.1f}%")
print(f"At 40°C → Heater: {controller.heater_output(40):.1f}%")

Final Summary Table (Write This in Exam!)

Concept Crisp Logic Fuzzy Logic
Truth Values {0,1} [0,1]
Sets Sharp boundary Gradual membership
Operations AND=Min, OR=Max (same as fuzzy!) Same, but meaning different
Excluded Middle Holds Violated → strength
Real-World Modeling Poor (binary) Excellent (human reasoning)
Applications Digital circuits Control systems, AI, decision making

Master Formula to Remember:
- Union: μ_{A∪B} = max(μ_A, μ_B)
- Intersection: μ_{A∩B} = min(μ_A, μ_B)
- Complement: μ_{¬A} = 1 − μ_A(x)

You now have complete theoretical + practical mastery of Unit III Fuzzy Logic – I.

Practice the temperature controller 5 times — it's the most common exam question!

Next unit: Fuzzy Logic – II (Membership Functions, Fuzzy Rules, Inference) → coming soon!

Last updated: Nov 30, 2025

Unit III: Fuzzy Logic – I (Introduction)

Ultimate Deep Understanding Notes + Best Python Code Examples (2025 Standards) Perfect for Exams, Interviews & Real-World Applications

Fuzzy Logic – I (Introduction)

Unit III: Fuzzy Logic – I (Introduction)

Ultimate Deep Understanding Notes + Best Python Code Examples (2025 Standards)
Perfect for Exams, Interviews & Real-World Applications

1. Why Fuzzy Logic Exists – The Real-World Problem

Crisp Logic (Boolean) Fuzzy Logic (Zadeh 1965)
A person is either Tall or Not Tall A person can be 0.8 Tall (180 cm)
Temperature is either Hot or Cold Temperature can be 0.7 Hot (35°C)
Only 0 or 1 → Binary Continuum between 0 and 1 → Human-like reasoning

Fuzzy Logic = Computing with Words
→ Used in washing machines, ABS brakes, cameras, medical diagnosis, stock trading, AI controllers.

2. Crisp Set vs Fuzzy Set – Core Difference Table

Property Crisp Set Fuzzy Set
Membership Only 0 or 1 Any value in [0,1]
Example A = {x ≥ 180} → {180,181,...} A = Tall = {(170,0.3), (180,0.8), (190,1.0)}
Boundary Sharp Gradual (no sharp boundary)
Representation Characteristic function χ(x) Membership function μ_A(x) ∈ [0,1]

3. Fuzzy Set Operations – With Code & Visualization

import numpy as np
import matplotlib.pyplot as plt

# Define universe
x = np.linspace(0, 100, 1000)

# Membership functions
def triangular(x, a, b, c):
    return np.maximum(0, np.minimum((x - a)/(b - a), (c - x)/(c - b)))

def trapezoidal(x, a, b, c, d):
    return np.maximum(0, np.minimum(np.minimum((x - a)/(b - a), 1), (d - x)/(d - c)))

# Example fuzzy sets
A = triangular(x, 20, 50, 80)   # "Medium Income"
B = triangular(x, 60, 80, 100)  # "High Income"

# Operations
union = np.maximum(A, B)                    # A ∪ B
intersection = np.minimum(A, B)             # A ∩ B
complement_A = 1 - A                        # ¬A
difference = np.minimum(A, 1 - B)           # A - B
algebraic_product = A * B                   # A · B
algebraic_sum = A + B - A*B                 # A + B (bounded)

# Plot
plt.figure(figsize=(12, 8))
plt.plot(x, A, label='Medium Income (A)', linewidth=3)
plt.plot(x, B, label='High Income (B)', linewidth=3)
plt.plot(x, union, '--', label='A ∪ B (Union)', linewidth=3)
plt.plot(x, intersection, ':', label='A ∩ B (Intersection)', linewidth=3)
plt.plot(x, complement_A, '-.', label='¬A (Complement)', linewidth=3)
plt.legend(fontsize=12)
plt.title('Fuzzy Set Operations', fontsize=16)
plt.xlabel('Income (thousands)', fontsize=14)
plt.ylabel('Membership Degree μ(x)', fontsize=14)
plt.grid(True, alpha=0.3)
plt.show()

4. Properties of Fuzzy Sets (Memorize This Table!)

Property Crisp Sets Fuzzy Sets Formula / Meaning
Commutative Yes Yes A ∪ B = B ∪ A
Associative Yes Yes (A ∪ B) ∪ C = A ∪ (B ∪ C)
Distributive Yes Yes A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
Idempotent Yes Yes A ∪ A = A
Identity Yes Yes A ∪ ∅ = A, A ∩ U = A
Involution Yes Yes ¬(¬A) = A
De Morgan’s Laws Yes Yes ¬(A ∪ B) = ¬A ∩ ¬B
Excluded Middle (A ∪ ¬A = U) Yes No Not true in fuzzy!
Non-Contradiction (A ∩ ¬A = ∅) Yes No Not true in fuzzy!

Key Point: Fuzzy sets violate classical logic’s excluded middle → allows partial truth!

5. Fuzzy Relations – With Best Example

A fuzzy relation R from X→Y is a fuzzy set in X×Y.

Example: "x is much taller than y"

# Height in cm
X = [150, 160, 170, 180, 190]
Y = [155, 165, 175, 185, 195]

# Fuzzy relation matrix: R(x,y) = how much x is taller than y
R = np.zeros((5,5))
for i in range(5):
    for j in range(5):
        diff = X[i] - Y[j]
        if diff <= 0:
            R[i,j] = 0
        elif diff <= 20:
            R[i,j] = diff / 20
        else:
            R[i,j] = 1.0

print("Fuzzy Relation: 'x is much taller than y'")
print(np.round(R, 2))

Output:

[[0.   0.   0.   0.   0.  ]
 [0.   0.   0.25 0.75 1.  ]
 [0.   0.   0.   0.5  1.  ]
 [0.   0.   0.   0.   1.  ]
 [0.   0.   0.   0.   0.  ]]

6. Max-Min Composition of Fuzzy Relations

If R: X→Y and S: Y→Z → R∘S: X→Z
(T = R ∘ S)[i,k] = max over j [ min(R[i,j], S[j,k]) ]

# Example: R = "x is similar to y", S = "y is friend of z"
R = np.array([[1.0, 0.8, 0.3],
              [0.8, 1.0, 0.4],
              [0.3, 0.4, 1.0]])

S = np.array([[1.0, 0.2],
              [0.6, 1.0],
              [0.1, 0.9]])

# Max-Min composition
T = np.zeros((3,2))
for i in range(3):
    for k in range(2):
        T[i,k] = np.max([np.min([R[i,j], S[j,k]]) for j in range(3)])

print("R ∘ S = 'x is indirect friend of z via similarity'")
print(np.round(T, 2))

7. Fuzzy to Crisp Conversion (Defuzzification Methods)

When you need a single crisp output from fuzzy system.

Method Formula Best For Code Example
Centroid (COG) z* = ∫μ(z)z dz / ∫μ(z) dz Most accurate Yes
Bisector Area left = Area right Balanced Yes
Mean of Maximum (MOM) Average of z with max μ Fast Yes
Smallest of Max (SOM) Min z with max μ Conservative Yes
Largest of Max (LOM) Max z with max μ Aggressive Yes
def centroid(x, mf):
    return np.sum(x * mf) / np.sum(mf + 1e-8)

def mom(x, mf):
    max_val = np.max(mf)
    return np.mean(x[mf >= max_val - 1e-8])

# Example output fuzzy sets
output_mf = triangular(x, 30, 60, 90) * 0.8 + triangular(x, 50, 70, 90) * 1.0
output_mf = np.clip(output_mf, 0, 1)

print(f"Centroid defuzzification: {centroid(x, output_mf):.2f}")
print(f"MOM defuzzification: {mom(x, output_mf):.2f}")

8. Best Real-World Example: Fuzzy Temperature Controller

class FuzzyTemperatureController:
    def __init__(self):
        self.x = np.linspace(-20, 50, 700)

    def cold(self, t): return trapezoidal(t, -30, -20, -5, 5)
    def warm(self, t): return triangular(t, 5, 20, 35)
    def hot(self, t): return trapezoidal(t, 25, 35, 45, 60)

    def heater_output(self, temp):
        c = self.cold(temp)
        w = self.warm(temp)
        h = self.hot(temp)

        # Rules:
        # If COLD → Heater HIGH
        # If WARM → Heater MEDIUM
        # If HOT  → Heater LOW

        high = triangular(self.x, 50, 80, 100)  # High
        med = triangular(self.x, 20, 50, 80)   # Medium
        low = triangular(self.x, 0, 10, 40)    # Low

        # Aggregation
        aggregated = np.maximum(np.minimum(c, high),
                                np.maximum(np.minimum(w, med), np.minimum(h, low)))

        # Defuzzify
        return centroid(self.x, aggregated)

# Test
controller = FuzzyTemperatureController()
print(f"At 10°C → Heater: {controller.heater_output(10):.1f}%")
print(f"At 25°C → Heater: {controller.heater_output(25):.1f}%")
print(f"At 40°C → Heater: {controller.heater_output(40):.1f}%")

Final Summary Table (Write This in Exam!)

Concept Crisp Logic Fuzzy Logic
Truth Values {0,1} [0,1]
Sets Sharp boundary Gradual membership
Operations AND=Min, OR=Max (same as fuzzy!) Same, but meaning different
Excluded Middle Holds Violated → strength
Real-World Modeling Poor (binary) Excellent (human reasoning)
Applications Digital circuits Control systems, AI, decision making

Master Formula to Remember:
- Union: μ_{A∪B} = max(μ_A, μ_B)
- Intersection: μ_{A∩B} = min(μ_A, μ_B)
- Complement: μ_{¬A} = 1 − μ_A(x)

You now have complete theoretical + practical mastery of Unit III Fuzzy Logic – I.

Practice the temperature controller 5 times — it's the most common exam question!

Next unit: Fuzzy Logic – II (Membership Functions, Fuzzy Rules, Inference) → coming soon!

Last updated: Nov 30, 2025