Guide to AI and Data Courses · Lecture Notes
Chapter 9: Fuzzy Logic and Fuzzy Systems
The technician who sets up an air conditioner gets this recipe from an old hand: "If the room is hot and humid, run the fan fast; if it is only warm, run it at medium." There is not a single number in that sentence, yet a good air conditioner follows it. Is 27 °C "hot"? Not quite, but it is not simply "warm" either. Fuzzy logic is the course about expressing such in-between cases as degrees between 0 and 1 and turning verbal rules into a working controller. These notes introduce the course and work through its core ideas with small examples you can compute by hand.
Learning outcomes
- Fuzzy sets
- Compute the membership degrees of a temperature with triangular and trapezoidal membership functions; apply the fuzzy set operations (minimum, maximum, 1 − µ).
- Linguistic rules
- Write expert knowledge as linguistic variables and IF-THEN rules; compute the firing degree of each rule.
- Mamdani inference
- Clip and combine rule outputs and defuzzify the result to a single number with the centroid.
- Sugeno inference
- Compute the weighted average of rules with constant outputs; show that the output changes smoothly with the input, in contrast to a crisp threshold rule.
- Place of the course
- State what separates fuzzy logic from probability, expert systems, neural networks and robot control.
9.1What question does the course ask?
How do we reason with concepts like "hot" or "fast" that have no sharp boundary?
Fuzzy logic drops the sharp line between belonging and not belonging to a set; it gives each element a degree of membership between 0 and 1. Rules can then be written with linguistic terms such as "hot" or "fairly fast", and these rules produce a numeric decision or control signal.
In a classical set an element is either in or out: 27 °C either is "hot" or it is not. This sharp line does not match the way people use concepts. "Hot" has no sharp boundary; 26 °C is slightly hot, 32 °C quite hot, 38 °C certainly hot. Fuzzy logic takes this partial membership seriously and shows the boundary as a smooth transition instead of a sharp line.
The question of the course has two parts. First: how do we represent a concept with a vague boundary by a number? The answer is the membership function. Second: how do we compute with rules written in such concepts and end up with a numeric decision? The answer is fuzzy inference and defuzzification. The computation is exact from start to finish; what is vague is the boundary of the concept, not the calculation. The same input always gives the same output.
9.1.1One example, three sections
The three core sections below build a single air-conditioner problem step by step. The inputs are temperature and relative humidity, the output is the fan speed (in percent). The membership functions are the same as in the interactive slide of the course; you can change temperature and humidity there and see the same numbers. The numbers are for teaching, not the settings of a real air conditioner.
9.1.2At a glance
| Criterion | Fuzzy Logic and Fuzzy Systems |
|---|---|
| Approach | partial membership and linguistic rules |
| Input | measurements, expert rules |
| Output | control signal or decision |
| Success measure | control quality, expert agreement |
| Math load | low-medium: set theory |
| Data needs | little; expert knowledge |
9.2A short history
| Year | Milestone |
|---|---|
| 1965 | Zadeh publishes "Fuzzy Sets", introducing sets with partial membership. |
| 1973 | Zadeh introduces linguistic variables and approximate reasoning with fuzzy IF-THEN rules. |
| 1975 | Mamdani and Assilian control a steam engine with the first fuzzy controller. |
| 1985 | Takagi and Sugeno propose the fuzzy model whose rule outputs are functions (TS model). |
| 1987 | The Sendai subway in Japan opens, accelerating and braking under fuzzy control. |
| 1993 | Jang publishes ANFIS, which trains fuzzy rules like a neural network. |
9.3Core concepts
The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.
| Concept | Meaning |
|---|---|
| Fuzzy set | Each element has a membership degree between 0 and 1. |
| Membership function | Triangle, trapezoid, Gaussian: maps a value to a degree. |
| Linguistic variable | A variable whose values are words: low, medium, high. |
| Fuzzy operators | Minimum for AND, maximum for OR, 1-µ for NOT. |
| Rule base | Rules like IF temperature is high THEN fan is fast. |
| Mamdani inference | Rule outputs are fuzzy sets, later defuzzified. |
| Sugeno inference | Rule outputs are functions of the inputs; fast to compute. |
| Defuzzification | Reducing the fuzzy result to one number: the centroid. |
9.4Fuzzy sets and membership functions
A classical set is defined by a function that says "yes" or "no" for every element (0 or 1). A fuzzy set extends this function to every number between 0 and 1: µA(x) is the degree of membership of x in the set A. In practice this function is usually piecewise linear: a trapezoid with corners a ≤ b ≤ c ≤ d (a triangle when b = c).
µ(x) = 0 (x < a or x > d); (x − a)/(b − a) (a ≤ x < b); 1 (b ≤ x ≤ c); (d − x)/(d − c) (c < x ≤ d).
Let us define a temperature variable with three linguistic terms (corners (a, b, c, d) in order, in °C):
| Term | Corners (a, b, c, d) | Shape |
|---|---|---|
| cold | (10, 10, 16, 22) | left shoulder |
| warm | (18, 24, 24, 30) | triangle |
| hot | (26, 32, 40, 40) | right shoulder |
The room is at 27 °C with relative humidity 50%. For humidity we use the terms "dry" (20, 20, 40, 60) and "humid" (40, 60, 100, 100).
- Cold. 27 °C is outside the base of cold (22): µ = 0.
- Warm. 27 lies on the falling side of warm (between 24 and 30): µ = (30 − 27)/(30 − 24) = 3/6 = 0.5.
- Hot. 27 lies on the rising side of hot (between 26 and 32): µ = (27 − 26)/(32 − 26) = 1/6 ≈ 0.17.
- Humidity. Dry: (60 − 50)/(60 − 40) = 0.5; humid: (50 − 40)/(60 − 40) = 0.5.
- AND (minimum). "warm AND humid" = min(0.5, 0.5) = 0.5; "hot AND humid" = min(0.17, 0.5) = 0.17.
- OR (maximum), NOT. "warm OR hot" = max(0.5, 0.17) = 0.5; "NOT warm" = 1 − 0.5 = 0.5.
Note that at 27 °C the degrees add up to 0 + 0.5 + 0.17 ≈ 0.67, not 1. A probability distribution could not do that; membership degrees are measured independently of each other.
Fuzzy operations keep the classical ones exactly at the values 0 and 1: min(1, 0) = 0, max(1, 0) = 1, 1 − 1 = 0. In between, minimum and maximum are not the only choice. There are other operation pairs, such as the product for AND and the probabilistic sum for OR (the family of t-norms and t-conorms). These notes use minimum and maximum; the course studies the whole family.
Applying the classical rule "a set united with its complement gives everything, intersected with it gives nothing" to fuzzy sets. In the example above, "warm OR NOT warm" = max(0.5, 0.5) = 0.5, not 1; "warm AND NOT warm" = min(0.5, 0.5) = 0.5, not 0. Neither law holds for fuzzy sets; this is not an error in the calculation but a natural consequence of partial membership.
9.5Mamdani inference and defuzzification
Expert knowledge is written as IF-THEN rules in linguistic variables. The antecedent (the IF part) of each rule in the rule base gives a membership degree; this is called the rule's firing degree. If several conditions are joined by AND, the minimum is taken. In Mamdani inference the consequent is also a fuzzy set: it is clipped at the firing degree. All the clipped sets are combined with the maximum, and the resulting shape is defuzzified to a single number.
For the fan speed let us define three terms (corners, %): low (0, 0, 20, 40), medium (25, 50, 50, 75), high (60, 80, 100, 100). The rule base has five rules:
| Rule | IF | THEN fan |
|---|---|---|
| R1 | cold | low |
| R2 | warm AND dry | low |
| R3 | warm AND humid | medium |
| R4 | hot AND dry | medium |
| R5 | hot AND humid | high |
y* = Σ µ(yj) · yj / Σ µ(yj), where µ is the value of the combined output set at the points yj. As the points get denser, the ratio approaches the centroid of the shape.
Degrees from the previous section: warm 0.5; hot 1/6; dry 0.5; humid 0.5; cold 0.
- Firing degrees. R1 = 0; R2 = min(0.5, 0.5) = 0.5; R3 = min(0.5, 0.5) = 0.5; R4 = min(1/6, 0.5) = 1/6; R5 = min(1/6, 0.5) = 1/6.
- Maximum per term. Low: max(0, 0.5) = 0.5. Medium: max(0.5, 1/6) = 0.5. High: 1/6 ≈ 0.17.
- Clipping and combining. Each fan term's membership function cannot exceed its own degree; then the three terms are combined with the maximum. At the points y = 0, 10, …, 100 the combined set is: 0.5 at the seven points from 0 to 60 (low or medium reaches 0.5 or more before clipping); 0.2 at 70 (medium is 0.2; high is 0.5 but clipped to 1/6, and the maximum is 0.2); 1/6 at 80, 90 and 100.
- Sums. Σµ = 7·0.5 + 0.2 + 3·(1/6) = 3.5 + 0.2 + 0.5 = 4.2. Σµ·y = 0.5·(0 + 10 + … + 60) + 0.2·70 + (1/6)(80 + 90 + 100) = 105 + 14 + 45 = 164.
- Defuzzify. y* = 164 / 4.2 ≈ 39. With much denser points (step 0.05) one gets y* ≈ 40.6; the coarse grid lands almost in the same place.
The fan speed is about 40%: between low and medium, a decision that suits mild weather. If you set humidity to 50% and temperature to 27 °C in the slide, you will see the same degrees and a similar fan speed.
Picking only the strongest rule. At 27 °C and 50% humidity, R2 and R3 fire to the same degree (0.5) and their outputs differ (low and medium). Choosing one throws information away; the centroid instead blends the outputs of all rules in proportion to their degrees. The strength of fuzzy inference is this smooth mixture.
9.6Sugeno inference and fuzzy control
Mamdani inference combines output sets as shapes, and defuzzification is an expensive step. In Sugeno inference the consequent of a rule is a number (or a function of the inputs). In the simplest case each rule has a constant zi and the output is the weighted average of the firing degrees:
y = Σ wi zi / Σ wi, where wi is the firing degree of the rule and zi its constant consequent. No shape combination or centroid integral is needed, which is why it is often chosen for embedded controllers.
For the same air conditioner take the consequents z = 10 (low), 50 (medium), 90 (high) (the values are illustrative): R1 → 10, R2 → 10, R3 → 50, R4 → 50, R5 → 90.
At 27 °C and 50% humidity the firing degrees are as in the previous section: R1 = 0, R2 = 0.5, R3 = 0.5, R4 = 1/6, R5 = 1/6.
- Numerator. 0.5·10 + 0.5·50 + (1/6)·50 + (1/6)·90 = 5 + 25 + 8.33 + 15 = 53.33.
- Denominator. 0.5 + 0.5 + 1/6 + 1/6 = 4/3 ≈ 1.33.
- Output. y = 53.33 / 1.33 = 40.
- At 28 °C. Warm (30 − 28)/6 = 1/3, hot (28 − 26)/6 = 1/3: R2 = R3 = R4 = R5 = 1/3. y = (10 + 50 + 50 + 90)/3 ÷ (4/3) = 50.
- At 29 °C. Warm 1/6, hot 3/6: R2 = R3 = 1/6, R4 = R5 = 0.5. y = (10/6 + 50/6 + 25 + 45) ÷ (4/3) = 80 ÷ (4/3) = 60.
As the temperature goes 27, 28, 29 °C the fan speed goes 40%, 50%, 60%: it rises by an equal step per degree. At 30 °C warm is 0 and R4 = R5 = 0.5, so y = (0.5·50 + 0.5·90)/1 = 70.
| Temperature (°C), humidity 50% | Fuzzy (Sugeno) fan speed | Crisp rule: "above 28 °C → 90, else 10" |
|---|---|---|
| 27 | 40% | 10% |
| 28 | 50% | 10% |
| 29 | 60% | 90% |
| 30 | 70% | 90% |
With the crisp threshold rule the speed jumps by 80 points between 28 and 29 °C, and the compressor keeps switching on and off. In a fuzzy controller the output changes gradually when the input changes. This is why fuzzy control has been chosen for appliances and subways: without deriving the process model that a PID controller needs, the expert's knowledge is turned into rules, giving a smooth and understandable controller.
9.6.1Tuning a controller
What is tuned in a design is the corners of the membership functions, the number of terms, the rule consequents and the choice of operations. These are usually tuned by trial and error or with data (by neuro-fuzzy methods such as ANFIS). It matters that the membership functions overlap with their neighbors: without overlap, for some inputs no rule fires at all and the denominator is zero.
Assuming the number of rules will stay small by itself. The full rule base is found by multiplying the number of terms of each input: with three inputs of 5 terms each there are 5 × 5 × 5 = 125 rules, with four inputs 625. For an omitted combination the controller may produce an unforeseen output. This is why the numbers of inputs and terms are kept small.
9.7How is the course taught?
9.7.1A typical workflow
An air conditioner measures 27 °C in a room; this value belongs to "warm" with degree 0.5 and to "hot" with degree about 0.17. The rules "if warm, cool moderately" and "if hot, cool strongly" fire with these degrees, their outputs are aggregated, and the centroid gives the compressor a single power value.
9.7.2Topics over the semester
- Crisp and fuzzy sets; degree of membership
- Membership functions: triangular, trapezoidal, Gaussian
- Fuzzy set operations; t-norms and t-conorms
- Fuzzy relations and max-min composition
- The extension principle and fuzzy numbers
- Linguistic variables and hedges
- Fuzzy IF-THEN rules and approximate reasoning
- Mamdani inference and defuzzification methods
- Takagi-Sugeno inference
- Fuzzy controller design; comparison with PID
- Fuzzy clustering: fuzzy c-means
- Neuro-fuzzy systems: ANFIS
This is a typical semester; the order and coverage vary between universities.
9.7.3Skills you will gain
- building fuzzy systems with scikit-fuzzy
- MATLAB Fuzzy Logic Toolbox
- choosing and tuning membership functions
- writing and testing a rule base
- simulating closed-loop control
- tuning rules from data with ANFIS
9.7.4Try it yourself
The Try It: Fuzzy Rules Run the Air Conditioner slide in the presentation lets you solve a typical problem of this course yourself: The air conditioner follows verbal rules such as "if it is hot and humid, run the fan fast". Change temperature and humidity; watch how strongly each rule fires and the resulting fan speed.
9.8Are you ready for this course?
9.8.1Prerequisites
| Course | Why it is needed |
|---|---|
| Artificial Intelligence | approximate reasoning |
| Discrete Mathematics | sets, relations and propositional logic |
| Programming | writing functions and building simple simulations |
| Control Systems | feedback and the PID controller |
9.8.2Ask yourself
For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.
| Question | If not, start with |
|---|---|
| Can you explain union, intersection and complement of sets? | Sets and propositional logic from discrete mathematics |
| Can you compute the area under a piecewise-defined function? | Integrals and the centroid from calculus |
| Can you explain a feedback loop and what a PID controller does? | An intro to control systems: feedback and PID |
| Can you write Python functions and step through a simple simulation? | An intro programming course, then a short NumPy tutorial |
9.9Neighboring courses
9.9.1Where it overlaps
| Relation | Course | Shared ground |
|---|---|---|
| subfield | This course is a subfield of Artificial Intelligence | approximate reasoning |
| shares methods | Shares methods with Expert Systems and Knowledge Representation | IF-THEN rule base, inference engine |
| shares methods | Shares methods with Robotics and Robot Vision | fuzzy controllers |
9.9.2Where it differs
- Probability and Statistics: Probability measures uncertainty about whether an event will happen; this course measures how far a concept applies, that is, a vague boundary.
- Expert Systems and Knowledge Representation: Expert systems mostly run rules with crisp logic; this course builds approximate reasoning where rules can be partly true.
- Robotics and Robot Vision: Robotics covers control as a whole; this course teaches the fuzzy controller as a model-free option.
- Artificial Neural Networks: Neural networks learn rules from data as unreadable weights; a fuzzy system's rules come from an expert and are readable.
9.10In practice
9.10.1Applications
| Area | What is done |
|---|---|
| Home appliances | Washing machines, air conditioners, rice cookers. |
| Transportation | Subway acceleration and braking, automatic gearboxes. |
| Industrial control | Kiln temperature, cement and chemical processes. |
| Cameras | Autofocus and image stabilization. |
| Decision support | Risk assessment, supplier selection. |
| Medicine | Approximate diagnosis from symptoms, dose advice. |
9.10.2Risks and ethics
| Risk | Explanation |
|---|---|
| Arbitrary membership functions | Who placed the corners of a triangle, and why, is rarely documented; other shapes make the same rules decide differently. |
| Rule explosion | Rule count grows exponentially with inputs and terms; on combinations nobody wrote, the controller gives unforeseen outputs. |
| Stability without proof | A controller tuned by trial and error is seldom proven stable in closed loop, which is risky in systems such as brakes. |
| Subjectivity dressed as numbers | A fuzzy score in medicine or credit, presented as an exact measurement, turns an expert's bias into a scientific-looking number. |
These issues are discussed in depth in Explainable AI and AI Ethics.
9.10.3Tools and datasets
Tools: MATLAB Fuzzy Logic Toolbox, scikit-fuzzy, jFuzzyLogic, fuzzylite, Simpful.
Classic benchmark problems: Tipping problem, Inverted pendulum, Truck backer-upper, Washing machine control, Water tank level, Mackey-Glass time series.
Fuzzy systems are usually built from expert knowledge rather than data; data is used to fine-tune the rules in methods such as ANFIS.
9.11Common misconceptions
Fuzzy logic is imprecise, sloppy logic.
Rules use vague concepts, but the computation is exact; the same input always gives the same output.
A membership degree is a probability.
0.5 is the degree of being "warm", not the chance of being warm; degrees need not sum to 1 (at 27 °C, 0.5 + 0.17).
Fuzzy logic is an old, abandoned field.
It is widely used in appliances and industrial control, and its combination with neural networks continues.
A fuzzy system learns from data.
A basic fuzzy system takes its rules from an expert; learning is added by hybrid methods such as ANFIS.
9.12Summary
- A fuzzy set gives each element a membership degree between 0 and 1; the degree is not a probability, and degrees need not add up to 1.
- A membership function (triangle, trapezoid, Gaussian) turns a number into a degree; in fuzzy operations AND is the minimum, OR the maximum, and NOT is 1 − µ.
- In Mamdani inference each rule's output set is clipped at its firing degree, the sets are combined and defuzzified to a single number with the centroid.
- In Sugeno inference the consequents are constants and the output is the weighted average of the firing degrees; it is fast and the output changes smoothly with the input.
- A fuzzy system takes its rules from an expert and is readable; but the number of rules grows exponentially with the number of inputs, and stability usually has to be checked separately.
9.13Test Yourself
Using the membership functions of these notes, find the degrees of "warm" and "hot" at 29 °C. What are "warm AND hot", "warm OR hot" and "NOT hot"?
Answer
Warm (30 − 29)/6 = 1/6 ≈ 0.17; hot (29 − 26)/6 = 0.5. AND min = 0.17; OR max = 0.5; NOT hot 1 − 0.5 = 0.5.
The fuzzy number "about 5" is a triangle with corners 3, 5, 7. What is the membership degree at 4, 6.5 and 8?
Answer
On the left side µ(4) = (4 − 3)/(5 − 3) = 0.5. On the right side µ(6.5) = (7 − 6.5)/(7 − 5) = 0.25. 8 lies outside the triangle: µ(8) = 0.
The combined output set of a Mamdani controller takes the values 0.4, 0.5, 0.1 at the points y = 20, 50, 80. What is the centroid?
Answer
Σµ·y = 0.4·20 + 0.5·50 + 0.1·80 = 8 + 25 + 8 = 41; Σµ = 1.0. y* = 41.
In a Sugeno controller at 30 °C and 50% humidity, warm is 0, hot is 2/3, dry is 0.5 and humid is 0.5. If the consequents are R4 → 50 and R5 → 90, what is the output?
Answer
R4 = min(2/3, 0.5) = 0.5, R5 = min(2/3, 0.5) = 0.5; the other rules are 0. y = (0.5·50 + 0.5·90)/(0.5 + 0.5) = 70.
A student says: "If the degree of hot at 27 °C is 0.17, the probability of not being hot is 0.83." Why is this wrong?
Answer
A membership degree is not a probability; it measures how far a concept applies. The temperature of the room is known; what is vague is the boundary of "hot". The degree of NOT hot is computed as 1 − 0.17 = 0.83, but that does not mean "83% chance of not being hot"; it means "not hot to degree 0.83".
A fuzzy controller has three inputs with 4, 3 and 5 terms. How many rules does the full rule base have? How many if a fourth input with 2 terms is added?
Answer
4 × 3 × 5 = 60 rules. With the fourth input, 60 × 2 = 120: the number of rules grows multiplicatively as inputs are added (rule explosion).
9.14After this course
Fuzzy logic shares the rule base with expert systems and control with robotics. After this course you can move to neuro-fuzzy systems to learn rules from data, or to probabilistic graphical models to handle uncertainty with probability.
Related careers: Control systems engineer, Embedded software engineer, Automation engineer, Decision support analyst.
9.15Further reading
- L. A. Zadeh, "Fuzzy Sets", Information and Control 8(3), 1965: the original definition of a fuzzy set; the source of the idea of membership degree used in these notes.
- T. J. Ross, Fuzzy Logic with Engineering Applications (4th ed., 2017): covers membership functions, Mamdani and Sugeno inference and controller design with engineering examples.
- G. J. Klir and B. Yuan, Fuzzy Sets and Fuzzy Logic: Theory and Applications (1995): treats the theoretical foundations of fuzzy sets, families of operations and fuzzy relations in detail.
9.16References
- Zadeh, "Fuzzy Sets", Information and Control 8(3), 1965.
- Klir and Yuan, Fuzzy Sets and Fuzzy Logic: Theory and Applications, Prentice Hall, 1995.
- Ross, Fuzzy Logic with Engineering Applications, 4th ed., Wiley, 2017.
- Jang, Sun and Mizutani, Neuro-Fuzzy and Soft Computing, Prentice Hall, 1997.
Related pages: Slides · Slides (PDF) · Course page