TRAPMF
Generates a trapezoidal membership function for a given universe. The shape of the trapezoid is defined by four parameters: left foot, left ceiling, right ceiling, and right foot.
The membership value is 0 outside the feet, climbs linearly to 1 at the left ceiling, stays flat at 1 until the right ceiling, and falls linearly back to 0.
Excel Usage
=TRAPMF(x, a, b, c, d)
x(list[list], required): Array of independent universe variables.a(float, required): Left foot of the trapezoid (membership begins to climb from zero).b(float, required): Left ceiling of the trapezoid (membership attains 1).c(float, required): Right ceiling of the trapezoid (membership begins to fall).d(float, required): Right foot of the trapezoid (membership reattains zero).
Returns (list[list]): Array of membership values corresponding to the input universe.
Example 1: Trapezoidal membership with a flat top
Inputs:
| x | a | b | c | d | |||||
|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 |
Excel formula:
=TRAPMF({0,1,2,3,4,5}, 1, 2, 3, 4)
Expected output:
| Result |
|---|
| 0 |
| 0 |
| 1 |
| 1 |
| 0 |
| 0 |
Example 2: Trapezoidal membership at a plateau point
Inputs:
| x | a | b | c | d |
|---|---|---|---|---|
| 2.5 | 1 | 2 | 3 | 4 |
Excel formula:
=TRAPMF(2.5, 1, 2, 3, 4)
Expected output:
1
Example 3: Trapezoidal membership across both sloped edges
Inputs:
| x | a | b | c | d | |||||
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1.5 | 2 | 3 | 3.5 | 4 | 1 | 2 | 3 | 4 |
Excel formula:
=TRAPMF({1,1.5,2,3,3.5,4}, 1, 2, 3, 4)
Expected output:
| Result |
|---|
| 0 |
| 0.5 |
| 1 |
| 1 |
| 0.5 |
| 0 |
Example 4: Trapezoidal membership on a negative universe
Inputs:
| x | a | b | c | d | ||||
|---|---|---|---|---|---|---|---|---|
| -4 | -3 | -2 | -1 | 0 | -3 | -2 | -1 | 0 |
Excel formula:
=TRAPMF({-4,-3,-2,-1,0}, -3, -2, -1, 0)
Expected output:
| Result |
|---|
| 0 |
| 0 |
| 1 |
| 1 |
| 0 |
Python Code
Show Code
import numpy as np
from skfuzzy import trapmf as fuzz_trapmf
def trapmf(x, a, b, c, d):
"""
Generate a trapezoidal fuzzy membership function.
See: https://pythonhosted.org/scikit-fuzzy/api/skfuzzy.html#skfuzzy.trapmf
This example function is provided as-is without any representation of accuracy.
Args:
x (list[list]): Array of independent universe variables.
a (float): Left foot of the trapezoid (membership begins to climb from zero).
b (float): Left ceiling of the trapezoid (membership attains 1).
c (float): Right ceiling of the trapezoid (membership begins to fall).
d (float): Right foot of the trapezoid (membership reattains zero).
Returns:
list[list]: Array of membership values corresponding to the input universe.
"""
try:
def to1d(arr):
if isinstance(arr, list):
flat = []
for row in arr:
row_list = row if isinstance(row, list) else [row]
for val in row_list:
try:
flat.append(float(val))
except (TypeError, ValueError):
continue
return np.array(flat)
return np.array([float(arr)])
x_arr = to1d(x)
if len(x_arr) == 0:
return "Error: Input mapping array x cannot be empty"
if not (a <= b <= c <= d):
return "Error: Parameters must satisfy a <= b <= c <= d"
result = fuzz_trapmf(x_arr, [a, b, c, d])
return [[float(val)] for val in result]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Array of independent universe variables.
Left foot of the trapezoid (membership begins to climb from zero).
Left ceiling of the trapezoid (membership attains 1).
Right ceiling of the trapezoid (membership begins to fall).
Right foot of the trapezoid (membership reattains zero).