%% Train Fuzzy Inference System Using ANFIS
% Load training data. This data has a single input and a single output.

load fuzex1trnData.dat

%
% Generate and train a fuzzy inference system. By default, the FIS structure is created using a grid partition of the input variable range with two membership functions.

fis = anfis(fuzex1trnData);

%
% Plot the ANFIS output and training data.

x = fuzex1trnData(:,1);
anfisOutput = evalfis(fis,x);
plot(x,fuzex1trnData(:,2),'*r',x,anfisOutput,'.b')
legend('Training Data','ANFIS Output','Location','NorthWest')

%
% The ANFIS data does not match the training data well. To improve the match:
% Increase the number of membership functions in the FIS structure to 4. Doing so adds fuzzy rules and tunable parameters to the system.
% Increase the number of training epochs.

opt = anfisOptions('InitialFIS',4,'EpochNumber',40);

%
% Suppress the error and step size Command Window display.

opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;

%
% Train the FIS.

fis = anfis(fuzex1trnData,opt);

%
% Plot the ANFIS output and training data.

figure
anfisOutput = evalfis(fis,x);
plot(x,fuzex1trnData(:,2),'*r',x,anfisOutput,'.b')
legend('Training Data','ANFIS Output','Location','NorthWest')

% The match between the training data and ANFIS output has improved.