How can I control the output range using the anfis() function?
이전 댓글 표시
hi I'm trying to train an ANFIS using a dataset with no negative input or target. But after training, some output membership functions have negative intervals which is not desired. I want my output to change between zero and one. So my question is HOW can I control the output range using ANFIS toolbox? thanx
답변 (1개)
In a standard ANFIS, one cannot directly restrict the output universe of discourse or the final output to a specific range such as
. The reason is that, like other unconstrained curve-fitting methods, the ANFIS optimization algorithms (hybrid or backpropagation) adjust the coefficients mathematically to minimize the Root Mean Squared Error (RMSE) across the entire training dataset simultaneously, not individual error points one by one. The algorithms have absolutely no concept of what "zero" means, and therefore, they do not inherently prevent the tunable parameters from taking negative values.
Example: Fuzzy approximation of squared sine function
The example below shows how to approximate the non-negative squared sine function using a Sugeno-type fuzzy system via the ANFIS method. In the zoomed-in plot, the negative values clearly appear near the boundaries.
%% Squared Sine Function
x = linspace(0, 1, 101)';
y = sinpi(x).^2; % y = sin²(πx)
%% ANFIS
% set up initial FIS
genOpt = genfisOptions('GridPartition');
genOpt.NumMembershipFunctions = 3; % design choice: can also try 5, 7, 9
genOpt.InputMembershipFunctionType = 'gaussmf'; % design choice: 'trimf', 'gbellmf'
genOpt.OutputMembershipFunctionType = 'constant'; % design choice: "linear" (default), but is not recommended
iniFIS = genfis(x, y, genOpt);
% specify ANFIS options for tuning fuzzy systems
opt = anfisOptions('InitialFIS', iniFIS);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;
% tune Sugeno FIS using training data
outFIS = anfis([x y], opt);
%% Plot results
figure
plotmf(outFIS, 'input', 1, 1001),
grid on, ylim([-0.2, 1.2])
xlabel('Input, x')
title('3 Input Gaussian MFs')
delete(findobj(gca, 'Type', 'text'));
raw_output = evalfis(outFIS, x);
figure
plot(x, y, 'o'), hold on
plot(x, raw_output, 'linewidth', 1.25), hold off
grid on
legend('Training Data', 'Raw ANFIS Output', 'location', 'south')
axis equal
xlabel('x'), ylabel('y')
title('Fuzzification of Squared Sine Function')
figure
plot(x, y, 'o'), hold on
plot(x, evalfis(outFIS, x), 'linewidth', 1.25), hold off
grid on
legend('Training Data', 'ANFIS Output', 'location', 'south')
ylim([-0.04 0.1])
xlabel('x'), ylabel('y')
title('Zoomed-in plot')
To force the ANFIS output to stay between 0 and 1, there are several effective approaches.
Method 1: Clip the final outputs
The first is to simply clip the final outputs after the ANFIS model makes a prediction. This is straightforward to implement, but it may lead to an unnatural-looking curve if ANFIS produces many negative values in the middle of the curve. Therefore, hard clipping is most suitable when only a few negative values occur near the boundaries.
%% Method 1: Pass the ANFIS output through a saturation function (or conditional If–Else)
clip_output = max(0, min(raw_output, 1)); % 0 < final output < 1
figure
plot(x, y, 'o'), hold on
plot(x, clip_output, 'linewidth', 1.25), hold off
grid on
legend('Training Data', 'Clipped ANFIS Output', 'location', 'south')
axis equal
xlabel('x'), ylabel('y')
title('Method 1: Clipped ANFIS Output')
Method 2: Apply Min-Max scaling to map the range to [0, 1]
The next approach is to use min–max normalization to compress the smallest value to 0 and the largest value to 1. This method is also easy to implement.
%% Method 2: Apply Min-Max scaling to map the range to [0, 1]
min_value = min(raw_output);
max_value = max(raw_output);
scale_output = (raw_output - min_value)/(max_value - min_value); % 0 < final output < 1
figure
plot(x, y, 'o'), hold on
plot(x, scale_output, 'linewidth', 1.25), hold off
grid on
legend('Training Data', 'Scaled ANFIS Output', 'location', 'south')
axis equal
xlabel('x'), ylabel('y')
title('Method 2: Scaled ANFIS Output')
Method 3: Manually adjust the Constant Output MFs
If linear output MFs are used, their slopes can sometimes extrapolate to negative values outside the training data range. In the ANFIS GUI (old version) or in a script, the output MF type can be changed from linear to constant. The 3rd method requires manually adjusting the constant output MFs (singletons). This works well when the number of output MFs to be adjusted is small. Otherwise, the task becomes tedious.
%% Method 3: Manually adjust the Constant Output MFs
outFIS.Outputs.MembershipFunctions
singleton1n3 = -0.0794;
outFIS.Outputs.MembershipFunctions(1).Parameters = singleton1n3;
outFIS.Outputs.MembershipFunctions(3).Parameters = singleton1n3;
figure
plot(x, y, 'o'), hold on
plot(x, evalfis(outFIS, x), 'linewidth', 1.25), hold off
grid on
legend('Training Data', 'ANFIS Output', 'location', 'south')
axis equal
xlabel('x'), ylabel('y')
title('Method 3: Re-adjust the Constant Output MFs')
Method 4: Increase the number of MFs
For a univariate function (single input and single output), such as in this example, increasing the number of MFs is a highly effective solution. Mathematically, if the ANFIS model produces unwanted negative values where the target function is expected to be non-negative, this typically indicates that the initialized fuzzy system lacks sufficient structural capacity (i.e., not enough MFs). Adding more MFs enables the model to better capture the complex positive shape of the target data. This approach is based on the Universal Approximation Theorem.
%% Method 4: Increase the number of MFs
% set up initial FIS
genOpt = genfisOptions('GridPartition');
genOpt.NumMembershipFunctions = 9;
genOpt.InputMembershipFunctionType = 'gaussmf';
genOpt.OutputMembershipFunctionType = 'constant';
iniFIS = genfis(x, y, genOpt);
% specify ANFIS options for tuning fuzzy systems
opt = anfisOptions('InitialFIS', iniFIS);
opt.DisplayANFISInformation = 0;
opt.DisplayErrorValues = 0;
opt.DisplayStepSize = 0;
opt.DisplayFinalResults = 0;
% tune Sugeno FIS using training data
outFIS2 = anfis([x y], opt);
raw_output2 = evalfis(outFIS2, x);
%% Plot results
figure
plotmf(outFIS2, 'input', 1, 1001),
grid on, ylim([-0.2, 1.2])
xlabel('Input, x')
title('9 Input Gaussian MFs')
delete(findobj(gca, 'Type', 'text'));
figure
plot(x, y, 'o'), hold on
plot(x, raw_output2, 'linewidth', 1.25), hold off
grid on
legend('Training Data', 'ANFIS Output', 'location', 'south')
axis equal
xlabel('x'), ylabel('y')
title('Method 4: Increase the number of MFs')
카테고리
도움말 센터 및 File Exchange에서 Fuzzy Logic Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







