주요 콘텐츠

Simulate Fuzzy Inference Systems in Simulink

You can simulate a fuzzy inference system (FIS) in Simulink® using one of the following blocks:

For more information on creating fuzzy inference systems, see:

Simulate Fuzzy Inference System

Once you have created a fuzzy inference system using Fuzzy Logic Designer or at the command line, you can simulate the system in Simulink.

For this example, you control the level of water in a tank using a fuzzy inference system implemented using a Fuzzy Logic Controller block. Open the sltank model.

open_system("sltank")

For this system, you control the water that flows into the tank using a valve. The outflow rate depends on the diameter of the output pipe, which is constant, and the pressure in the tank, which varies with water level. Therefore, the system has nonlinear characteristics.

The two inputs to the fuzzy system are the water level error, level, and the rate of change of the water level, rate. The output of the fuzzy system is the rate at which the control valve is opening or closing, valve.

To implement a fuzzy inference system, specify the FIS name parameter of the Fuzzy Logic Controller block as the name of a FIS object in the MATLAB® workspace. In this example, the block uses the mamfis object tank.

For more information on this system, see Water Level Control in a Tank.

As a first attempt to control the water level, set the following rules in the FIS. These rules adjust the valve based on only the water level error.

  • 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.

Specify the rules by creating a vector of fisrule objects and assigning it to the Rules property of the tank FIS object.

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

Simulate the model, and view the water level.

open_system("sltank/Comparison")
sim("sltank")

Figure Tank Demo contains an axes object and other objects of type uicontrol. The hidden axes object with xlabel Water Level Control contains 4 objects of type line, patch.

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

To reduce the oscillations, add two more rules to the system. 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 add these rules, use the addRule function.

rule4 = "If level is okay and rate is positive then valve is close_slow";
rule5 = "If level is okay and rate is negative then valve is open_slow";
newRules = [rule4 rule5];
tank = addRule(tank,newRules);

Simulate the model.

sim("sltank")

Figure Tank Demo contains an axes object and other objects of type uicontrol. The hidden axes object with xlabel Water Level Control contains 8 objects of type line, patch.

The water level now tracks the setpoint without oscillating.

You can also simulate fuzzy systems using the Fuzzy Logic Controller with Ruleviewer block. The sltankrule model is the same as the sltank model, except that it uses the Fuzzy Logic Controller with Ruleviewer block.

open_system("sltankrule")

During simulation, this block displays a rule inference diagram that you can use to understand the FIS behavior.

sim("sltankrule",50)

Figure Tank Demo contains an axes object and other objects of type uicontrol. The hidden axes object with xlabel Water Level Control contains 12 objects of type line, patch.

MATLAB figure

If you pause the simulation, you can examine the FIS behavior by manually adjusting the input variable values in the rule inference display and observing the inference process and output.

Simulation Modes

The Fuzzy Logic Controller and FIS Tree blocks have the following two simulation modes:

  • Interpreted execution — Simulate fuzzy systems using precompiled MEX files. Using this option reduces the initial compilation time of the model.

  • Code generation — Simulate fuzzy system without precompiled MEX files. Use this option when simulating fuzzy systems for code generation applications. Doing so simulates your system using the same code path used for generated code.

To select a simulation mode, set the Simulate using parameter of the block. By default, the block uses Interpreted execution mode for simulation.

Access Intermediate Inference Results for Fuzzy Logic Controller Block

You can access intermediate fuzzy inference results when simulating a system using the Fuzzy Logic Controller block. You can use this data to visualize the fuzzy inference process or troubleshoot the performance of your FIS. To access this data, enable the corresponding parameters in the block, and connect signals to the corresponding output ports. For more information, see Fuzzy Logic Controller.

Block Parameter DescriptionOutput Port
Fuzzified InputsFuzzified input values, obtained by evaluating the input membership functions of each rule at the current input values.fi
Rule firing strengthsRule firing strengths, obtained by evaluating the antecedent of each rule.rfs
Rule outputsRule outputs, obtained by evaluating the consequent of each rule.ro
Aggregated outputsAggregate output for each output variable, obtained by combining the corresponding outputs from all the rules.ao

Map Command-Line Functionality to Block Configuration

Most parameters and ports of the Fuzzy Logic Controller and FIS Tree blocks map to the input and output arguments of evalfis or the properties of evalfisOptions.

The following table shows the Fuzzy Logic Controller and FIS Tree block parameters and ports that map to evalfis arguments.

evalfis ArgumentBlock Parameter or PortDescription
fisFIS nameFuzzy inference system
input, when a single rowinInput variable values
output, when a single rowoutOutput variable values
fuzzifiedInfi

Fuzzified inputs

(Fuzzy Logic Controller block only)

ruleOutro

Rule outputs

(Fuzzy Logic Controller block only)

aggregateOutao

Aggregated outputs

(Fuzzy Logic Controller block only)

ruleFiringrfs

Rule firing strengths

(Fuzzy Logic Controller block only)

The following table shows the Fuzzy Logic Controller and FIS Tree block parameters that map to evalfisOptions properties.

evalfisOptions PropertyBlock ParameterDescription
NumSamplePointsNumber of samples for output discretizationNumber of points in output fuzzy sets
OutOfRangeInputValueMessageOut of range input valueDiagnostic message behavior when an input is out of range
NoRuleFiredMessageNo rule firedDiagnostic message behavior when no rules fire
EmptyOutputFuzzySetMessageEmpty output fuzzy setDiagnostic message behavior when an output fuzzy set is empty

The remaining block parameters do not map to arguments of evalfis. Also, unlike the Fuzzy Logic Controller and FIS Tree blocks, evalfis does not support fixed-point data for simulation or code generation.

See Also

Blocks

Topics