Skip to content

Instantly share code, notes, and snippets.

@hritik5102
Created February 4, 2021 12:26
Show Gist options
  • Save hritik5102/f6601c75c42549134b2b5aa9ce6abf3a to your computer and use it in GitHub Desktop.
Save hritik5102/f6601c75c42549134b2b5aa9ce6abf3a to your computer and use it in GitHub Desktop.
To implement different membership functions used for fuzzification of inputs.
# import package
import numpy as np
import matplotlib.pyplot as plt
import math
def trianguler_mf(x,a,b,c):
if x<=a:
return 0
elif a <= x <= b:
return (x-a)/(b-a)
elif b <= x <= c:
return (c-x)/(c-b)
else:
return 0
def triangular_main():
x = list(range(0,100,1))
y = []
a = 20
b = 60
c = 80
for i in range(0,100,1):
y.append(trianguler_mf(i,a,b,c))
display(x,y,'Trianguler MF')
def trapezoidal_mf(x,a,b,c,d):
if x<=a:
return 0
elif a <= x <= b:
return (x-a)/(b-a)
elif b <= x <= c:
return 1
elif c<= x <= d:
return (d-x)/(d-c)
else:
return 0
def trapezoidal_main():
x = list(range(0,100,1))
y = []
a = 20
b = 40
c = 60
d = 80
for i in range(0,100,1):
y.append(trapezoidal_mf(i,a,b,c,d))
display(x,y,'Trapezoidal MF')
def gaussian_mf(x,c,sigma):
m = -1/2 * ((x-c)**2) / (sigma)**2
return math.exp(m)
def gaussian_main():
x = list(range(0,100,1))
y = []
c = 50
sigma = 20
for i in range(0,100,1):
y.append(gaussian_mf(i,c,sigma))
display(x,y,'Gaussian MF')
def generalised_bell_mf(x,a,b,c):
denominator = abs((x-c)/a)**(2*b)
return 1/ (1+denominator)
def generalised_bell_main():
x = list(range(0,100,1))
y = []
a = 20
b = 40
c = 60
sigma = 20
for i in range(0,100,1):
y.append(generalised_bell_mf(i,a,b,c))
display(x,y,'Generalised bell MF')
def sigmoid_mf(x,a,c):
return (1 + math.exp(-a*(x-c)))**(-1)
def sigmoid_main():
x = np.arange(0,10,0.1)
y = []
a = 2
c = 4
for i in x:
y.append(sigmoid_mf(i,a,c))
display(x,y,'Sigmoid MF')
def display(x,y,plot_name='Default'):
'''
Display : plot different graph
'''
plt.xlabel('X - Cordinates')
plt.ylabel('Y - Cordinates')
plt.plot(x,y,label=plot_name)
plt.legend()
plt.show()
if __name__=='__main__':
triangular_main()
trapezoidal_main()
gaussian_main()
generalised_bell_main()
sigmoid_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment