%% Validate ANFIS Training
% Load training data.

load fuzex1trnData.dat

%
% Load validation data.

load fuzex1chkData.dat

%
% Specify the following training options:
% 4 input membership functions
% 30 training epochs
% Suppress training progress display

opt = anfisOptions('InitialFIS',4,'EpochNumber',30);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;

%
% Add the validation data to the training options.

opt.ValidationData = fuzex1chkData;

%
% Train the FIS, and return the validation results.

[fis,trainError,stepSize,chkFIS,chkError] = anfis(fuzex1trnData,opt);

%
% The training error, trainError, and validation error, chkError, arrays each contain one error value per training epoch. Plot the training error and the validation error.

x = [1:30];
plot(x,trainError,'.b',x,chkError,'*r')

% The minimum validation error occurs at epoch 17. The increase in validation error after this point indicates overfitting of the model parameters to the training data. Therefore, the tuned FIS at epoch 17, chkFIS, exhibits the best generalization performance.
% Copyright 2012 The MathWorks, Inc.