0% found this document useful (0 votes)
25 views15 pages

Assingment

The document provides MATLAB and Python code examples for various membership functions used in fuzzy logic, including Trapezoidal, Triangular, Gaussian, General Bell, Sigmoidal, Pi, and Left-Right functions. Each section contains code snippets for generating and plotting the respective membership functions with appropriate labels and formatting. The document serves as a guide for implementing these functions in both programming environments.

Uploaded by

Aatif Altaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views15 pages

Assingment

The document provides MATLAB and Python code examples for various membership functions used in fuzzy logic, including Trapezoidal, Triangular, Gaussian, General Bell, Sigmoidal, Pi, and Left-Right functions. Each section contains code snippets for generating and plotting the respective membership functions with appropriate labels and formatting. The document serves as a guide for implementing these functions in both programming environments.

Uploaded by

Aatif Altaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Name: Aatif Altaf

Roll no: 242040601

1.Trapezoidal Membership Function:


MATLAB Code:
clear;
clc;
clear all;
X=(0:0.1:10);
Y=trimf(X,[5,7,8]);
plot(X,Y,'LineWidth',2.0);
ylim([0 1]);
ylabel("Membership Grades");
set(gca,'FontName','Times','Fontsize',15.0);
Python Code:
import os
import numpy as np
import matplotlib.pyplot as plt
from skfuzzy.membership import trapmf
X = np.arange(0, 10.1, 0.1)
Y = trapmf(X, [3, 5, 8, 20])
plt.plot(X, Y, linewidth=3.0)
plt.ylim([0, 1])
plt.ylabel("Membership Grades", fontsize=15)
plt.gca().set_xlabel("X", fontsize=15)
plt.gca().tick_params(axis='both', which='major', labelsize=15)
plt.gca().set_title("Trapezoidal Membership Function", fontsize=15)
2.Triangular Membership Function:
MATLAB Code
clear;
clc;
clear all;
X=(0:0.1:10);
Y=trimf(X,[5,7,8]);
plot(X,Y,'LineWidth',2.0);
ylim([0 1]);
ylabel("Membership Grades");
set(gca,'FontName','Times','Fontsize',15.0);
Python Code:
import numpy as np
import matplotlib.pyplot as plt
from skfuzzy.membership import trimf
X = np.arange(0, 10.1, 0.1)
Y = trimf(X, [5, 7, 8])
plt.plot(X, Y, linewidth=2.0)
plt.ylim([0, 1])
plt.ylabel("Membership Grades", fontsize=15)
plt.gca().set_xlabel("X", fontsize=15)
plt.gca().tick_params(axis='both', which='major', labelsize=15)
plt.gca().set_title("Triangular Membership Function", fontsize=15)
3.Gaussian Membership Function:

MATLAB Code:
clc;
clear all;
X=(0:0.1:10);
Y=gaussmf(X,[0.5 2]);
plot(X,Y,'LineWidth',2.0);
ylim([0 1]);
ylabel("Membership Grades");
set(gca,'FontName','Times','Fontsize',15.0);
Python Code:
import numpy as np
import matplotlib.pyplot as plt
X = np.arange(0, 10.1, 0.1)
sigma = 0.5
mean = 2
Y = np.exp(-((X - mean)**2) / (2 * sigma**2))
plt.plot(X, Y, linewidth=2.0)
plt.ylim([0, 1])
plt.ylabel("Membership Grades", fontsize=15)
plt.gca().set_xlabel("X", fontsize=15)
plt.gca().tick_params(axis='both', which='major', labelsize=15)
plt.gca().set_title("Gaussian Membership Function", fontsize=15)
plt.show()
4.General Bell Shaped Membership Function:
MATLAB Code:
clc
clear all
X=(0:0.1:10)
Y=gbellmf(X,[1 4 6]);
plot(X,Y,'LineWidth',2.0)
ylim([0 1])
ylabel("Membership Grades")
set(gca,'FontName','Times','Fontsize',15.0)
Python Code:
import numpy as np
import matplotlib.pyplot as plt
def gbellmf(x, params):
a, b, c = params
return 1 / (1 + np.abs((x - c) / a)**(2 * b))
X = np.arange(0, 10.1, 0.1)
Y = gbellmf(X, [1, 4, 6])
plt.plot(X, Y, linewidth=2.0)
plt.ylim([0, 1])
plt.ylabel("Membership Grades", fontsize=15)
plt.gca().set_xlabel("X", fontsize=15)
plt.gca().tick_params(axis='both', which='major', labelsize=15)
plt.gca().set_title("Generalized Bell Membership Function", fontsize=15)
plt.show()
5.Sigmoidal Membership Function:
MATLAB Code:
clc
clear all
X=(0:0.1:10)
Y=sigmf(X,[4 5]);
plot(X,Y,'LineWidth',2.0)
ylim([0 1])
ylabel("Membership Grades")
set(gca,'FontName','Times','Fontsize',15.0)
Python Code:
import numpy as np
import matplotlib.pyplot as plt
def sigmf(x, params):
b, c = params
return 1 / (1 + np.exp(-b * (x - c)))
X = np.arange(0, 10.1, 0.1)
Y = sigmf(X, [4, 5])
plt.plot(X, Y, linewidth=2.0)
plt.ylim([0, 1])
plt.ylabel("Membership Grades", fontsize=15)
plt.gca().set_xlabel("X", fontsize=15)
plt.gca().tick_params(axis='both', which='major', labelsize=15)
plt.gca().set_title("Sigmoidal Membership Function", fontsize=15)
plt.show()
6.𝝅 Membership Function:
MATLAB Code:
clc
clear all
X=(0:0.1:10)
Y=pimf(X,[2 5 8 10]);
plot(X,Y,'LineWidth',2.0)
ylim([0 1])
ylabel("Membership Grades")
set(gca,'FontName','Times','Fontsize',15.0)
Python Code:
import numpy as np
import matplotlib.pyplot as plt
from skfuzzy.membership import trimf
X = np.arange(0, 10.1, 0.1)
Y = pimf(X, [2, 5, 8, 10])
plt.plot(X, Y, linewidth=2.0)
plt.ylim([0, 1])
plt.ylabel("Membership Grades", fontsize=15)
plt.gca().set_xlabel("X", fontsize=15)
plt.gca().tick_params(axis='both', which='major', labelsize=15)
plt.gca().set_title("Π (Pi) Membership Function", fontsize=15)
7.Left-Right Membership Function:

MATLAB Code:
clc;
clear all;
alpha=40;
beta=10;
c=70;
X=(0:0.1:100)';
for i=1:size(X,1);
if(X(i,1)>=c);
x1=(X(i,1)-c)/beta;
Y(i,1)=exp(-abs((x1)^3));
elseif (X(i,1)<=c);
x2=(c-X(i,1))/alpha;
Y(i,1)=exp(-abs((x2)^3));
end
end
figure;
plot(X,Y,'Linewidth',5.0);
ylim([0 1]);
ylabel("Membership Grades");
set(gca,'FontName','Times','Fontsize',15.0);
Python Code:
import numpy as np
import matplotlib.pyplot as plt
alpha = 40
beta = 10
c = 70
X = np.arange(0, 100.1, 0.1).reshape(-1, 1)
Y = np.zeros_like(X)
for i in range(X.shape[0]):
if X[i, 0] >= c:
x1 = (X[i, 0] - c) / beta
Y[i, 0] = np.exp(-abs(x1**3))
elif X[i, 0] <= c:
x2 = (c - X[i, 0]) / alpha
Y[i, 0] = np.exp(-abs(x2**3))
plt.plot(X, Y, linewidth=5.0)
plt.ylim(0, 1)
plt.ylabel("Membership Grades")
plt.gca().set_fontname("Times New Roman")
plt.gca().tick_params(labelsize=15)
plt.show()

You might also like