How to fix errors when evaluating a Fuzzy System using custom membership functions

6 views (last 30 days)
Hi.
Can anyone help me with fixing these error? I really dont know what to do.
Incorrect size for expression '<output of feval>': expected [1x:?] but found [101x1].
Error in evaluateNonhomogenousFIS_double_mex
Error in fuzzy.internal.utility.evalfis (line 97)
[varargout{1:nargout}] = fuzzy.internal.codegen. ...
Error in evalfis (line 98)
[varargout{1:nargout}] = fuzzy.internal.utility.evalfis(varargin{:});
Error in ruleview (line 427)
[v,irr,orr,arr,rfs,orrSug] = evalfis(fis,inputVector,options);
Error in ruleview (line 306)
ruleview #update
Error in fisgui (line 100)
eval([tag '(fis);']);
Error while evaluating Menu Callback.
I wrote a custom membership function based on an example, and used them in the Membership Function Editor:
function out = stairsfn(x,params)
if params(1) == 1
v = 0;
else
v = 1;
end
for i = 1:length(x)
y(i) = 0;
if params(1) ~= 1
y(i) = 1;
end
if x(i) <= (params(2)+ 2) && x(i) >= (params(2))
v = y(i)+0.5;
if params(1) ~= 1
v = y(i)-0.5;
end
y(i) = v;
elseif x(i) <= (params(3)+ 2) && x(i) >= (params(3))
v = y(i)+1;
if params(1) ~= 1
v = y(i)-1;
end
y(i) = v;
elseif x(i) >= (params(3)+ 2)
y(i) = 1;
if params(1) ~= 1
y(i) = 0;
end
else
y(i) = v;
end
end
out = y';
%fprintf('out %.4f \n', out);
end
In the Editor, the functions display the expected staircase graphs, http://prntscr.com/set2jy .
But when I added the fuzzy rules in the Editor, the errors appeared, http://prntscr.com/set303 .
  1 Comment
Sam Chak
Sam Chak on 22 Apr 2025
I have tested the stairsfn() function, which I named mf2_o in the editor. I also inserted my staircase function (mf1_s), highlighted with a bold red line in the editor, for comparison purposes. Two rules have been successfully added:
  • Rule 1: If input1 is mf1_s, then output1 is mf1.
  • Rule 2: If input1 is mf2_o, then output1 is mf2.
When the Rule Viewer is opened, Rule 2 is not triggered for unknown reasons, despite the input value of 200 applied to mf2_o, which should have resulted in a firing strength of 0.5. Only Rule 1 is triggered correctly, causing the centroid to incorrectly shift to output1 = -1.

Sign in to comment.

Answers (1)

Sam Chak
Sam Chak on 22 Apr 2025
Designing custom membership functions can be enjoyable and provide a sense of satisfaction to fuzzy logic aficionados. However, using conditional statements and for-loops can be challenging. Therefore, I typically prefer elementary mathematical functions, as they are sufficient for most applications. Both of my Heaviside-based staircase membership functions (stairsmf & stairzmf) work well in the Fuzzy System Editor.
x = linspace(0, 400, 4001)';
mf1 = stairzmf(x, [125, 275]);
mf2 = stairsmf(x, [125, 275]);
figure
plot(x, [mf1, mf2]), grid on, ylim([-0.2, 1.2])
text( 50, 1.1, 'mf1')
text(325, 1.1, 'mf2')
xlabel('x'), ylabel('Degree of membership')
title('My Staircase membership functions')
%% S-shaped Staircase function
function out = stairsmf(x, params)
stairs1 = 0.5*heaviside(x - params(1));
stairs2 = 0.5*heaviside(x - params(2));
out = stairs1 + stairs2;
end
%% Z-shaped Staircase function
function out = stairzmf(x, params)
stairs1 = 0.5*(1 - heaviside(x - params(1)));
stairs2 = 0.5*(1 - heaviside(x - params(2)));
out = stairs1 + stairs2;
end
My Staircase functions

Categories

Find more on Fuzzy Logic in Simulink in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!