📄 fuzzy.m
字号:
% ==========================================================
%
% Neural Networks A Classroom Approach
% Satish Kumar
% Copyright Tata McGraw Hill, 2004
%
% MATLAB code that implements the fuzzification and
% defuzzification in fuzzy approximation example
% Reference: Table 14.6 and 14.7;Pages 642-643
%
% ==========================================================
uodx = [-3 3];
uody = [-3 3];
uodz = [0 1];
xsets = [-3 -3 -2 % Left-center-right.
-2.5 -1.5 -0.5 % y-y-x or x-y-y means a half triangle
-1 0 1
0.5 1.5 2.5
2 3 3];
ysets = xsets; % symmetry used here
zsets = [0 0 0.25
0.15 0.33 0.55
0.45 0.66 0.85
0.75 1 1];
% Find number of sets on each UOD
numxsets = size(xsets,1);
numysets = size(ysets,1);
numzsets = size(zsets,1);
for i = 1:numzsets
areaz(i) = 0.5 * (zsets(i,3)-zsets(i,1)) * 1;
end
rulebase = [1 1 2 1 1
1 2 3 2 1
2 3 4 3 2
1 2 3 2 1
1 1 2 1 1];
[x,y] = meshgrid(-3:0.2:3,-3:0.2:3);
inx = x(1,:);
iny = inx;
for m = 1:size(x,1)
inputx = inx(m);
for n = 1:size(y,1)
inputy = iny(n);
for i = 1:numxsets
if ((inputx - xsets(i,1))*(xsets(i,3) - inputx) >= 0)
firex(i) = mem(inputx, xsets(i,:));
else
firex(i) = 0;
end
end
for i = 1:numysets
if ((inputy - ysets(i,1))*(ysets(i,3) - inputy) >= 0)
firey(i) = mem(inputy, ysets(i,:));
else
firey(i) = 0;
end
end
for i = 1:numxsets
for j = 1:numysets
rulefire(i,j) = min(firex(i),firey(j));
end
end
[k,l] = find(rulefire > 0);
num = 0;
den = 0;
for i = 1:size(k)
num = num + rulefire(k(i),l(i)) * zsets(rulebase(k(i),l(i)),2) * ...
areaz(rulebase(k(i),l(i)));
den = den + rulefire(k(i),l(i)) * areaz(rulebase(k(i),l(i)));
end
if den ~= 0
func(m,n) = num/den;
else
func(m,n) = 0;
end
clear k l;
end
end
figure(2)
subplot(2,2,2)
mesh(x,y,func)
xlabel('x');
ylabel('y');
zlabel('z');
title('(b)')
colormap(cool);
%generate the Gaussian
sigma = 1.5;
for i=1:31
for j=1:31
gauss(i,j) = exp(-((x(i,j))^2 + (y(i,j))^2)/(2*sigma^2));
end
end
subplot(2,2,1)
mesh(x, y, gauss);
title('(a)')
xlabel('x');
ylabel('y');
zlabel('z');
subplot(2,2,3)
mesh(x,y,(gauss-func))
axis([-3 3 -3 3 -1 1])
title('(c)')
xlabel('x');
ylabel('y');
zlabel('Linear error');
subplot(2,2,4)
mesh(x,y,(gauss-func).^2)
title('(d)')
xlabel('x');
ylabel('y');
zlabel('Squared error');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -