Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 36 ยท Membership Functions and Operations

Triangular and Trapezoidal Membership Functions

Learn the two most common fuzzy membership shapes, the triangle and the trapezoid, with formulas, shoulder variants, partitions and runnable Python code.

When you build a real fuzzy system you rarely invent exotic curves. Nine times out of ten you use triangles and trapezoids. They are cheap to compute, easy to explain to a domain expert ("it is fully warm between 20 and 24 degrees"), and easy to tune with just three or four numbers. This lesson gives you the formulas, a reusable implementation, and the design rules for building whole term sets from them.

The triangular function

A triangle is defined by three numbers a <= b <= c: the left foot, the peak and the right foot.

TEXT
mu(x) = 0                    if x <= a or x >= c
mu(x) = (x - a) / (b - a)    if a < x <= b
mu(x) = (c - x) / (c - b)    if b < x < c

The membership rises linearly from 0 at a to 1 at b, then falls linearly to 0 at c. A single formula covers all cases with min and max:

TEXT
mu(x) = max( min( (x - a)/(b - a), (c - x)/(c - b) ), 0 )

The trapezoidal function

A trapezoid has four numbers a <= b <= c <= d: it rises from a to b, stays at 1 between b and c (the core, a flat top), and falls from c to d. It is the natural choice for concepts that have a whole range of "fully true" values, such as "comfortable room temperature".

TEXT
mu(x) = max( min( (x - a)/(b - a), 1, (d - x)/(d - c) ), 0 )

The triangle is the special case b = c.

Python Reusable triangle and trapezoid functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two printed shapes show the difference at a glance: a single peak versus a flat plateau. The guard if b > a else ones handles shoulders, explained next.

Shoulders: open-ended terms

At the edges of a universe you often need a term that stays at 1 up to the boundary: "cold" should be fully true for every temperature below some value, not fall back to 0 at the lowest point. You get this by setting two parameters equal:

  • Left shoulder: a = b, for example trapmf(x, 0, 0, 10, 20) is 1 from 0 to 10.
  • Right shoulder: c = d, for example trapmf(x, 20, 30, 40, 40) is 1 from 30 to 40.

Another common trick is to set the foot outside the universe, for instance a = -10 for a universe starting at 0. Our implementation handles equal parameters safely; a naive formula would divide by zero.

Python Shoulders for the ends of a variable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At 0 degrees, "cold" is exactly 1, and at 40 degrees "hot" is exactly 1. Without shoulders, the controller would behave oddly at the extremes of the sensor range.

Designing a partition

A common recipe for a variable with n terms is a fuzzy partition: place the peaks evenly and make each foot reach the neighbouring peak. Then the memberships of neighbouring terms add up to exactly 1 everywhere, so every input activates at most two terms, and the sum is constant. This keeps the system balanced and easy to analyse.

Python A five-term partition with triangles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The names NB, NS, ZE, PS, PB (negative big, negative small, zero, positive small, positive big) are the traditional labels for a control error. Every row sums to 1.00, and between two peaks exactly two terms are active. Fuzzy Controllers (chapter 5) uses this pattern heavily.

Choosing parameters

  • Overlap. Neighbouring terms should overlap by about 25 to 50 percent. No overlap means the rules switch abruptly (crisp behaviour); too much overlap blurs the distinction between terms.
  • Core width. Make the core wide where precision does not matter and narrow where it does.
  • Cover the universe. Every possible input must activate at least one term, or no rule will fire (the output is undefined).
  • Start simple. Begin with triangles; move to trapezoids where you need a flat zone, and to smooth curves (Gaussian, Bell and Sigmoid Functions) only when you need differentiability.

Tip: Because triangle and trapezoid are piecewise linear, you can describe them with just the corner points and use np.interp(x, [a, b, c, d], [0, 1, 1, 0]). It is fast and gives the same result.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How many parameters define a trapezoidal membership function?

    A trapezoid uses a, b, c, d: left foot, start of the plateau, end of the plateau and right foot.
  2. 2. What is a triangle in terms of a trapezoid?

    When the plateau shrinks to a single point (b = c), the trapezoid becomes a triangle.
  3. 3. What is a left shoulder?

    Setting a = b makes the set fully true from the start of the universe until c, then it falls to 0 at d.
  4. 4. In a triangular partition with evenly spaced peaks, what is the sum of the memberships at any input?

    Each foot reaches the neighbouring peak, so the two active terms always add to 1.
  5. 5. What goes wrong if the terms of a variable do not cover the whole universe?

    An uncovered input has membership 0 in every term, so no rule is activated.

Summary

  • A triangle is defined by (a, b, c), a trapezoid by (a, b, c, d); both are piecewise linear and cheap.
  • Equal parameters create shoulders for the ends of a universe.
  • Evenly spaced triangles form a partition where memberships sum to 1.
  • Keep 25 to 50 percent overlap and make sure the terms cover the universe.
  • Use np.interp or the min/max formula; guard against division by zero.

Next: Gaussian, Bell and Sigmoid Functions