%% Add Membership Function to Fuzzy Inference System
% Create a Mamdani fuzzy system, and add three input variables and one output variable. For this example, give the second input variable and the output variable the same name.

fis = mamfis;
fis = addInput(fis,[0 80],"Name","speed");
fis = addInput(fis,[0 100],"Name","throttle");
fis = addInput(fis,[0 10],"Name","distance");
fis = addOutput(fis,[0 100],"Name","throttle");

% Add a membership function to the first input variable, specifying a trapezoidal membership function, and set the membership function parameters.

fis = addMF(fis,"speed","trapmf",[-5 0 10 30]);

% You can also specify the name of your membership when you add it to a fuzzy system. Add a membership function called "high" to the first input variable.

fis = addMF(fis,"speed","trapmf",[50 70 80 85],'Name',"high");

% View the membership functions for the first input variable.

plotmf(fis,"input",1)

% If your system has an input variable with the same name as an output variable, you must specify the variable type when adding a membership function. For example, add a membership function to the output variable.

fis = addMF(fis,"throttle","trimf",[0 20 40],'VariableType',"output");
plotmf(fis,"output",1)

% Alternatively, you can add a default membership function to a fuzzy system and set its parameters using dot notation. For example, add and configure a membership function for the third input variable.

fis = addMF(fis,"distance");
fis.Inputs(3).MembershipFunctions(1).Type = "trapmf";
fis.Inputs(3).MembershipFunctions(1).Parameters = [-1 0 2 4];
plotmf(fis,"input",3)

% Copyright 2018 The MathWorks, Inc.