Implement union, intersection , complement and difference
operations of fuzzy set using python
import numpy as np
# Define the fuzzy sets
setA = np.array([0.1, 0.4, 0.7, 0.9])
setB = np.array([0.2, 0.5, 0.8])
# Define the universe of discourse
x = np.linspace(0, 1, 100)
# Define the membership functions
mA = np.interp(x, [0, 0.5, 1], [0, 1, 0])
mB = np.interp(x, [0, 0.5, 1], [0, 1, 0.5])
# Perform union
union = np.fmax(mA, mB)
# Perform intersection
intersection = np.fmin(mA, mB)
# Perform complement
complementA = 1 - mA
# Perform difference
difference = np.fmax(mA - mB, np.zeros_like(x))
# Plot the fuzzy sets and the operations
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 8))
plt.plot(x, mA, label="A")
plt.plot(x, mB, label="B")
plt.plot(x, union, label="A union B")
plt.plot(x, intersection, label="A intersection B")
plt.plot(x, complementA, label="A complement")
plt.plot(x, difference, label="A difference B")
plt.legend(loc="upper right")
plt.show()
Output: