Generate MATLAB Code for Simulating Fuzzy Systems
Once you interactively build or tune a fuzzy system using Fuzzy Logic Designer, you can now generate MATLAB® code to programmatically simulate that system. You can then use or modify the generated code for your applications.
You can generate code for all the analysis simulations defined in Analyze Fuzzy System Using Fuzzy Logic Designer.
Control Surface — Creates a surface plot for a FIS using
gensurf
.Rule Inference — Displays a rule inference diagram for a Mamdani or Sugeno FIS using
plotrule
.System Validation — Plots the output of one or more FIS objects plot for specified input data using
comparefis
. You can also specify output reference data and view the resulting error analysis. For this simulation, you must import data into the app before generating code.Error Distribution — Plots the distribution of FIS output errors for different input combinations using
plotfiserr
. For this simulation, you must import input and output data into the app before generating code.FIS Tree Data Flow — Displays the propagation of inference results through a FIS tree using
plotfis
.
For this example, you generate code for simulating the tipper system created in Build Fuzzy Systems Using Fuzzy Logic Designer. Open the app and import the tipper system.
fuzzyLogicDesigner("tipper.fis")
To generate simulation code for the active system in Fuzzy Logic Designer, on the Design tab, select Export > Generate Simulation Function.
In the Generate MATLAB Code for a Simulation dialog box, in the
Simulation drop-down list, select the type of simulation. For this
example, select Rule Inference
to generate code for viewing the
rule inference diagram for the FIS.
Click OK.
To generate code, click OK.
The MATLAB Editor opens a generated script with the following code.
function ruleInferenceSimulation(x) % This function evaluates a fuzzy inference system using an % input data point and creates a visual representation of the rule % inference process using the simulation results. % % Input data x is a row vector of n input values for a fuzzy system. This % simulation uses n=2. if nargin==0 x = [5 5]; end fis = constructFuzzySystem(); plotrule(fis,Inputs=x,NumSamplePoints=101); end function tipper = constructFuzzySystem() %% Constructs a fuzzy system. % Construct FIS tipper = mamfis(Name="tipper"); % Input 1 tipper = addInput(tipper,[0 10],Name="service"); tipper = addMF(tipper,"service","gaussmf",[1.5 0], ... Name="poor",VariableType="input"); tipper = addMF(tipper,"service","gaussmf",[1.5 5], ... Name="good",VariableType="input"); tipper = addMF(tipper,"service","gaussmf",[1.5 10], ... Name="excellent",VariableType="input"); % Input 2 tipper = addInput(tipper,[0 10],Name="food"); tipper = addMF(tipper,"food","trapmf",[0 0 1 3], ... Name="rancid",VariableType="input"); tipper = addMF(tipper,"food","trapmf",[7 9 10 10], ... Name="delicious",VariableType="input"); % Output 1 tipper = addOutput(tipper,[0 30],Name="tip"); tipper = addMF(tipper,"tip","trimf",[0 5 10], ... Name="cheap",VariableType="output"); tipper = addMF(tipper,"tip","trimf",[10 15 20], ... Name="average",VariableType="output"); tipper = addMF(tipper,"tip","trimf",[20 25 30], ... Name="generous",VariableType="output"); % Rules tipper = addRule(tipper,[1 1 1 1 2; ... 2 0 2 1 1; ... 3 2 3 1 2]); end
This code:
Constructs the FIS object using the same construction code as Generate MATLAB Code for Building Fuzzy Systems.
Visualizes the rule inference diagram using
plotrule
. TheNumSamplePoints
input toplotrule
corresponds to the Number of Samples parameter in the app.Accepts a vector of input values for evaluation. By default, the function uses the midpoint of each input variable range as the input.
You can save and modify the generated code for your applications.
Generate Code for Simulations that Require Data
The preceding rule-inference example does not depend on simulation data. When you generate code for the System Validation and Error Distribution analysis methods, you must first import data into the app.
When you generate code for these simulations, the app exports the data as a MAT file.
For example, for a system-validation simulation, the MAT file has the name
sysValidSimData_<random_string>.mat
.
The following code shows an example generated function for system validation simulation.
function systemValidationSimulation(x,y) % This function simulates a fuzzy system using an % input-output dataset and plots the results for comparison. % % Input data x is an m-by-n matrix, where m is the number of data % points and n is the number of inputs of a fuzzy system. Output data y % is an m-by-p matrix, where p is the number of outputs of the fuzzy % system. This simulation uses n=2 and p=1. if nargin<1 data = load('sysValidSimData_tpee830729_af66_4421_9c54_0b07ae48c968.mat','x','y'); x = data.x; y = data.y; elseif nargin<2 y = []; end fis = constructFuzzySystem(); comparefis(fis,x,OutputData=y,ShowLegend=true,ShowError=false, ... MergeInputs=true,MergeOutputs=true,MergeErrors=true,SelectedInputs=[1 2], ... SelectedOutputs=1,SelectedFIS=1,NumSamplePoints=101); end ...
The function accepts input and output data as input arguments. If you do not specify the data, the function loads the data from the exported MAT file.
After constructing the FIS, the generated code plots the system validation using
comparefis
. The
name-value arguments for comparefis
are derived from the options
selected in the System Validation document of the app.
See Also
Apps
Functions
plotfis
|comparefis
|plotrule
|plotfiserr
|gensurf