# Untitled

unknown
plain_text
a year ago
3.1 kB
2
Indexable
Never
```# Define the fuzzy sets for temperature
def hot(temp):
if temp >= 25:
return 1
elif temp < 20:
return 0
else:
return (temp - 20) / 5

def normal(temp):
if temp >= 25 or temp <= 15:
return 0
elif temp < 20:
return (temp - 15) / 5
else:
return (25 - temp) / 5

def cold(temp):
if temp <= 15:
return 1
elif temp >= 20:
return 0
else:
return (20 - temp) / 5

# Define the fuzzy sets for humidity
def high(humidity):
if humidity >= 70:
return 1
elif humidity < 50:
return 0
else:
return (humidity - 50) / 20

def medium(humidity):
if humidity >= 70 or humidity <= 30:
return 0
elif humidity < 50:
return (humidity - 30) / 20
else:
return (70 - humidity) / 20

def low(humidity):
if humidity <= 30:
return 1
elif humidity >= 50:
return 0
else:
return (50 - humidity) / 20

# Define the fuzzy sets for fan speed
def low_speed(speed):
if speed >= 500:
return 0
elif speed <= 250:
return 1
else:
return (500 - speed) / 250

def medium_speed(speed):
if speed <= 250 or speed >= 750:
return 0
elif speed < 500:
return (speed - 250) / 250
else:
return (750 - speed) / 250

def high_speed(speed):
if speed <= 500:
return 0
elif speed >= 750:
return 1
else:
return (speed - 500) / 250

def defuzzify(fan_speed):
if (fan_speed <= 100):
return "Low speed"
elif (fan_speed >100 and fan_speed <=250):
return "Medium speed"
else:
return "High speed"

# Define the fuzzy rules
def fuzzy_logic(temp, humidity):
temp_hot = hot(temp)
temp_normal = normal(temp)
temp_cold = cold(temp)
print("Temperature set:", temp_hot, temp_normal, temp_cold)

hum_high = high(humidity)
hum_medium = medium(humidity)
hum_low = low(humidity)
print("Humidity set:", hum_high, hum_medium, hum_low)

# Apply fuzzy rules
speed_low = min(temp_cold, hum_low) * 750
speed_medium = min(temp_normal, hum_medium) * 750
speed_high = min(temp_hot, hum_high) * 750
# print(speed_low, speed_medium, speed_high)

# Calculate the degree of membership for each fuzzy set
speed_low_membership = low_speed(speed_low)
speed_medium_membership = medium_speed(speed_medium)
speed_high_membership = high_speed(speed_high)
print("Speed set:", speed_low_membership, speed_medium_membership, speed_high_membership)

# Defuzzify the output
fan_speed = (speed_low_membership * 250 + speed_medium_membership * 500 + speed_high_membership * 750) / (speed_low_membership + speed_medium_membership + speed_high_membership)

return fan_speed

# Set the input values for temperature and humidity
temperature = 25
humidity = 40

# Calculate the fan speed using fuzzy logic
fan_speed = fuzzy_logic(temperature, humidity)

# Print the resulting fan speed
print("The fan speed is:", defuzzify(fan_speed))
# print("```