Mastering Fuzzy Mathematics: Membership Functions and Python Implementations
This article introduces fuzzy mathematics, explains how fuzzy sets and membership functions quantify vague concepts, and provides Python code using the scikit‑fuzzy library to implement various membership functions such as triangular, Gaussian, S‑shaped, and polynomial types, complete with plotting examples.
Fuzzy Mathematics
In production practice, scientific experiments and daily life, people often encounter fuzzy concepts such as big/small, heavy/light, fast/slow, etc. As science advances, quantitative analysis of these fuzzy concepts is needed, which is provided by fuzzy mathematics, a modern applied mathematics discipline that follows classical and statistical mathematics.
Statistical mathematics expands the application of mathematics from deterministic to random phenomena, while fuzzy mathematics expands it from deterministic to fuzzy phenomena. In all scientific fields, quantities can be classified as deterministic or uncertain; uncertain can be random or fuzzy. Fuzzy mathematics studies the variation laws of quantities that are uncertain and fuzzy.
Fuzzy sets and membership functions are used to characterize the degree of fuzziness. Below we introduce types of membership functions and their Python implementation.
Fuzzy Sets and Membership Functions
Classical sets are crisp containers that either completely include or exclude an element.
Fuzzy sets, defined by fuzzy mathematics, have no clear boundaries and can partially contain elements, represented by membership functions.
Note: Classical set ↔ characteristic function, fuzzy set ↔ membership function; they are one-to-one representations of the same concept.
Membership functions map each element of a universe to a degree between 0 and 1; the function can be any curve as long as it stays within this range.
Accurately determining membership functions is the basis for quantifying fuzzy concepts and solving real problems. Common methods include binary comparison sorting, fuzzy statistics, fitting fuzzy distribution, minimum fuzziness, and expert experience.
Specific Membership Functions
We use the Python third‑party library scikit-fuzzy to implement various membership functions. Install it with:
<code>pip install scikit-fuzzy</code>Import the library:
<code>import skfuzzy as fuzz
import numpy as np</code>Triangular (trimf) and Trapezoidal (trapmf) Functions
These are the simplest piecewise‑linear membership functions. Parameters for trimf: a=3,b=6,c=8 ; for trapmf: a=1,b=5,c=7,d=8 . Plotting code:
<code>xarray = np.linspace(0,10,200)
plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
yarray1 = fuzz.trimf(xarray,[3,6,8])
plt.plot(xarray,yarray1,label='Triangular')
plt.legend()
plt.subplot(1,2,2)
yarray2 = fuzz.trapmf(xarray,[1,5,7,8])
plt.plot(xarray,yarray2,label='Trapezoidal')
plt.legend()
</code>Gaussian (gaussmf), Double‑Gaussian (gauss2mf) and Generalized Bell (gbellmf) Functions
Gaussian and bell functions are smooth and simple; the bell function adds an extra parameter for flexibility. Parameters: gaussmf c=2, σ=5 ; gauss2mf c1=1, σ1=3, c2=3, σ2=4 ; gbellmf a=2, b=4, c=6 . Plotting code:
<code>xarray = np.linspace(0,10,200)
plt.figure(figsize=(15,4))
plt.subplot(1,3,1)
yarray1 = fuzz.gaussmf(xarray,2,5)
plt.plot(xarray,yarray1,label='Gaussian')
plt.legend(loc='upper right')
plt.subplot(1,3,2)
yarray2 = fuzz.gauss2mf(xarray,1,3,3,4)
plt.plot(xarray,yarray2,label='Double Gaussian')
plt.legend(loc='upper right')
plt.subplot(1,3,3)
yarray3 = fuzz.gbellmf(xarray,2,4,6)
plt.plot(xarray,yarray3,label='Generalized Bell')
plt.legend(loc='upper right')
</code>S‑shaped Functions: sigmf, dsigmf, psigmf
sigmf represents a single S‑shaped curve; dsigmf the difference of two S‑curves; psigmf the product of two S‑curves. Parameters: sigmf a=2,c=4 ; dsigmf c1=5,b1=2,c2=6,b2=7 ; psigmf c1=2,b1=3,c2=5,b2=8 . Plotting code:
<code>xarray = np.linspace(0,10,200)
plt.figure(figsize=(15,4))
plt.subplot(1,3,1)
yarray1 = fuzz.sigmf(xarray,2,4)
plt.plot(xarray,yarray1,label='Single S')
plt.legend(loc='upper right')
plt.subplot(1,3,2)
yarray2 = fuzz.dsigmf(xarray,5,2,6,7)
plt.plot(xarray,yarray2,label='Difference of S')
plt.legend(loc='upper right')
plt.subplot(1,3,3)
yarray3 = fuzz.psigmf(xarray,2,3,5,8)
plt.plot(xarray,yarray3,label='Product of S')
plt.legend(loc='upper right')
</code>Polynomial‑Based Functions: zmf, pimf, smf
These functions produce Z‑shaped, π‑shaped, and S‑shaped curves respectively. Parameters: zmf a=2,b=4 ; pimf a=1,b=4,c=5,d=10 ; smf a=1,b=8 . Plotting code:
<code>xarray = np.linspace(0,10,200)
plt.figure(figsize=(15,4))
plt.subplot(1,3,1)
yarray1 = fuzz.zmf(xarray,2,4)
plt.plot(xarray,yarray1,label='Z-shaped')
plt.legend(loc='upper right')
plt.subplot(1,3,2)
yarray2 = fuzz.pimf(xarray,1,4,5,10)
plt.plot(xarray,yarray2,label='π-shaped')
plt.legend(loc='upper right')
plt.subplot(1,3,3)
yarray3 = fuzz.smf(xarray,1,8)
plt.plot(xarray,yarray3,label='S-shaped')
plt.legend(loc='upper right')
</code>References
Fuzzy mathematics, Baidu Baike
Introduction and evaluation of fuzzy mathematics Python libraries, Zhihu
scikit‑fuzzy documentation
Mathematical Modeling Algorithms and Programming Implementation, Zhang et al.
Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.