TRIMF
Generates a triangular membership function for a given universe. The shape of the triangle is defined by three parameters: left foot, peak, and right foot.
The membership value is 0 outside the feet, climbs linearly to 1 at the peak, and falls linearly back to 0.
Excel Usage
=TRIMF(x, a, b, c)
x(list[list], required): Array of independent universe variables.a(float, required): Left foot of the triangle (membership begins to climb from zero).b(float, required): Peak of the triangle (membership equals 1).c(float, required): Right foot of the triangle (membership reattains zero).
Returns (list[list]): Array of membership values corresponding to the input universe.
Example 1: Triangular membership with a centered peak
Inputs:
| x | a | b | c | ||||
|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 1 | 2 | 3 |
Excel formula:
=TRIMF({0,1,2,3,4}, 1, 2, 3)
Expected output:
| Result |
|---|
| 0 |
| 0 |
| 1 |
| 0 |
| 0 |
Example 2: Triangular membership at the peak point
Inputs:
| x | a | b | c |
|---|---|---|---|
| 2 | 1 | 2 | 3 |
Excel formula:
=TRIMF(2, 1, 2, 3)
Expected output:
1
Example 3: Triangular membership with a left shoulder
Inputs:
| x | a | b | c | ||||
|---|---|---|---|---|---|---|---|
| 0 | 0.5 | 1 | 1.5 | 2 | 0 | 0 | 2 |
Excel formula:
=TRIMF({0,0.5,1,1.5,2}, 0, 0, 2)
Expected output:
| Result |
|---|
| 1 |
| 0.75 |
| 0.5 |
| 0.25 |
| 0 |
Example 4: Triangular membership with a right shoulder
Inputs:
| x | a | b | c | |||
|---|---|---|---|---|---|---|
| 0 | 1 | 1.5 | 2 | 0 | 2 | 2 |
Excel formula:
=TRIMF({0,1,1.5,2}, 0, 2, 2)
Expected output:
| Result |
|---|
| 0 |
| 0.5 |
| 0.75 |
| 1 |
Python Code
Show Code
import numpy as np
from skfuzzy import trimf as fuzz_trimf
def trimf(x, a, b, c):
"""
Generate a triangular fuzzy membership function.
See: https://pythonhosted.org/scikit-fuzzy/api/skfuzzy.html#skfuzzy.trimf
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 triangle (membership begins to climb from zero).
b (float): Peak of the triangle (membership equals 1).
c (float): Right foot of the triangle (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):
return "Error: Parameters must satisfy a <= b <= c"
result = fuzz_trimf(x_arr, [a, b, c])
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 triangle (membership begins to climb from zero).
Peak of the triangle (membership equals 1).
Right foot of the triangle (membership reattains zero).