Main Content

Design Fuzzy Inference System in Simulink

Since R2025a

When designing a fuzzy inference system (FIS) for a Simulink® model, you can modify your FIS using Fuzzy Logic Designer by opening the app directly from the Fuzzy Logic Controller block. You can then export the updated FIS to the block and simulate the model.

Open Model

Open the Simulink model. For this example, use the sltank model, which uses a FIS to control the water level in a tank.

openExample("sltank")

Simulink model diagram showing Fuzzy Logic Controller block

When you open this model, the tank FIS used by the Fuzzy Logic Controller block is imported to the MATLAB® workspace. In this example, you modify the rule base for this FIS, keeping the variable and membership function definitions unchanged. For more information about the tank FIS, see Water Level Control in a Tank.

Define Initial FIS

To open Fuzzy Logic Designer from the Fuzzy Logic Controller block, you must have an initial FIS object defined and referenced from the FIS name block parameter.

For this example, create an initial FIS design that adjusts the valve based on only the water-level error using the following rules.

  • If the water level is okay, then do not adjust the valve.

  • If the water level is low, then open the valve quickly.

  • If the water level is high, then close the valve quickly.

To do so, overwrite the existing rules in the tank FIS.

rules = ["If level is okay then valve is no_change"
         "If level is low then valve is open_fast"
         "If level is high then valve is close_fast"];
tank.Rules = fisrule(rules);

Simulate the model for 100 seconds using this initial design and examine the water-level signal.

set_param("sltank","StopTime","100")
sim("sltank")
open_system("sltank/Comparison")

Simulink scope showing square wave reference signal and the water-level signal with large oscillations

These rules are insufficient for controlling the system since the water level oscillates around the setpoint.

Modify FIS Design

Once you have an initial FIS defined for the Fuzzy Logic Controller block, you can use Fuzzy Logic Designer to:

  • Modify input and output variable ranges

  • Add or modify membership functions

  • Add or modify fuzzy rules

  • Change the FIS type

For more information on designing a FIS in the app, see Build Fuzzy Systems Using Fuzzy Logic Designer.

In this example, to reduce the oscillations, you add the following rules to the rule base. These rules adjust the valve based on the rate of change of the water level when the water level is near the setpoint.

  • If the water level is okay and increasing, then close the valve slowly.

  • If the water level is okay and decreasing, then open the valve slowly.

To open Fuzzy Logic Designer, double-click the Fuzzy Logic Controller block. In the block dialog box, on the General tab, click Design.

Fuzzy Logic Controller block dialog with the Design button to the right of the FIS name text box.

The app opens and imports the tank FIS from the MATLAB workspace.

Fuzzy Logic Designer app with the tank FIS listed in the Design Browser on the far right and the Fuzzy Inference System plot document in the center. In the document area, the Rule Editor is the third tab

To modify the rule base, add rules using the Rule Editor document and specify the rule antecedents and consequents in the Property Editor. For more information on specifying rules, see Define Fuzzy Rules Using Fuzzy Logic Designer.

Add the first rule — If the water level is okay and increasing (positive rate), then close the valve slowly.

On the left, the Rule Editor shows the new rule as the fourth rule in the list — If level is okay and rate is positive then valve is close_slow. On the right, the rule linguistic variables are selected in the level, rate, and valve lists.

Add the second rule — If the water level is okay and decreasing (negative rate), then open the valve slowly.

On the left, the Rule Editor shows the new rule as the fifth rule in the list — If level is okay and rate is negative then valve is open_slow. On the right, the rule linguistic variables are selected in the level, rate, and valve lists.

Update Block and Simulate Model

After you design your FIS, update the block and simulate the model. On the Design tab, select Export > Update Block and Run Simulation.

App toolstrip showing the Update Block and Run Simulation selection in the Export menu on the far right side of the toolstrip

The app:

  1. Exports the FIS design to the MATLAB workspace.

  2. Updates the FIS name parameter of the Fuzzy Logic Controller to use the exported FIS.

  3. Simulates the model.

The simulation result shows that the added rules improve the controller performance. The water level now tracks the setpoint without oscillating.

Simulink scope showing the water-level signal tracking the reference square wave with small overshoot

To export the FIS and update the block without simulating the model, select Export > Update Block Only instead.

See Also

Topics