1.定义:模糊逻辑推理可以认为是一种不精确的推理,是通过模糊规则将给定输入转化为输出的过程。
2.“如果—则”规则
“如果—则”规则(if—then规则)是一种包含了模糊逻辑的条件陈述语句。
该实验的模糊集合我用的是ABCDE和abcde,ABCDE是三角形函数,abcde是Z形函数,规则为大小写字母abcde之间的相互匹配,小写字母和对应大写匹配为xX(先小写字母后大写字母),其他都是按照顺序如a和B就是aB,而bA、cA、cB、dA、dB、dC、eA、eB、eC、eD不存在即为NULL。
输入:
a=addmf(a,'input',1,'a','trimf',[-5*f1,-4*f1,-3*f1]);
a=addmf(a,'input',1,'b','trimf',[-3*f1,-2*f1,-1*f1]);
a=addmf(a,'input',1,'c','trimf',[-1*f1,0,1*f1]);
a=addmf(a,'input',1,'d','trimf',[1*f1,2*f1,3*f1]);
a=addmf(a,'input',1,'e','trimf',[3*f1,4*f1,5*f1]);
a=addmf(a,'input',2,'A','zmf',[-5*f2,-2*f2]);
a=addmf(a,'input',2,'B','zmf',[-3*f2,0]);
a=addmf(a,'input',2,'C','zmf',[-1*f2,2*f2]);
a=addmf(a,'input',2,'D','zmf',[1*f2,3*f2]);
a=addmf(a,'input',2,'E','zmf',[2*f2,5*f2]);
输出规则如下:
a=addmf(a,'output',1,'Aa','smf',[-5*f3,-4*f3]);
a=addmf(a,'output',1,'Bb','smf',[-4*f3,-3*f3]);
a=addmf(a,'output',1,'Cc','smf',[-3*f3,-2*f3]);
a=addmf(a,'output',1,'Dd','smf',[-2*f3,-1*f3]);
a=addmf(a,'output',1,'Ee','smf',[-1*f3,0]);
a=addmf(a,'output',1,'aB','zmf',[0,1*f3]);
a=addmf(a,'output',1,'aC','zmf',[1*f3,2*f3]);
a=addmf(a,'output',1,'aD','zmf',[2*f3,3*f3]);
a=addmf(a,'output',1,'aE','zmf',[3*f3,4*f3]);
a=addmf(a,'output',1,'bC','zmf',[4*f3,5*f3]);
a=addmf(a,'output',1,'bD','trimf',[-5*f3,-4*f3,-3*f3]);
a=addmf(a,'output',1,'bE','trimf',[-3*f3,-2*f3,-1*f3]);
a=addmf(a,'output',1,'cD','trimf',[-1*f3,0,1*f3]);
a=addmf(a,'output',1,'cE','trimf',[1*f3,2*f3,3*f3]);
a=addmf(a,'output',1,'dE','trimf',[3*f3,4*f3,5*f3]);
a=addmf(a,'output',1,'NULL','zmf',[-1*f3,0,1*f3]);
运行结果如下:
输入图形:
输出图形:
>> fuzzyInference_updated
ans =
1. If (e is a) and (ec is A) then (u is Aa) (1)
2. If (e is a) and (ec is B) then (u is aB) (1)
3. If (e is a) and (ec is C) then (u is aC) (1)
4. If (e is a) and (ec is D) then (u is aD) (1)
5. If (e is a) and (ec is E) then (u is aE) (1)
6. If (e is b) and (ec is A) then (u is NULL) (1)
7. If (e is b) and (ec is B) then (u is Bb) (1)
8. If (e is b) and (ec is C) then (u is bC) (1)
9. If (e is b) and (ec is D) then (u is bD) (1)
10. If (e is b) and (ec is E) then (u is bE) (1)
11. If (e is c) and (ec is A) then (u is NULL) (1)
12. If (e is c) and (ec is B) then (u is NULL) (1)
13. If (e is c) and (ec is C) then (u is Cc) (1)
14. If (e is c) and (ec is D) then (u is cD) (1)
15. If (e is c) and (ec is E) then (u is cE) (1)
16. If (e is d) and (ec is A) then (u is NULL) (1)
17. If (e is d) and (ec is B) then (u is NULL) (1)
18. If (e is d) and (ec is C) then (u is NULL) (1)
19. If (e is d) and (ec is D) then (u is Dd) (1)
20. If (e is d) and (ec is E) then (u is dE) (1)
21. If (e is e) and (ec is A) then (u is NULL) (1)
22. If (e is e) and (ec is B) then (u is NULL) (1)
23. If (e is e) and (ec is C) then (u is NULL) (1)
24. If (e is e) and (ec is D) then (u is NULL) (1)
25. If (e is e) and (ec is E) then (u is Ee) (1)
fuzzy Controller table:e=[-3,+3],ec=[-3,+3]
Ulist =
0 0 0 0 0 0 0
-0.0000 -0.1286 -0.3600 -0.4073 -1.7391 -4.2857 -3.0000
0 0 0 0 0 0 0
-0.0000 0.3616 1.4205 1.8450 1.6206 1.7143 3.0000
0 0 0 0 0 0 0
-0.0000 -0.0000 -0.0000 0.4324 1.8803 2.7735 6.0000
0 0 0 0 0 0 0
>>
三、源代码
%模糊控制器设计
a=newfis('fuzzf'); %创建新的模糊推理系统
%输入1
f1=1;
a=addvar(a,'input','e',[-5*f1,5*f1]);
%添加 e 的模糊语言变量
%隶属度函数为三角形
a=addmf(a,'input',1,'a','trimf',[-5*f1,-4*f1,-3*f1]);
a=addmf(a,'input',1,'b','trimf',[-3*f1,-2*f1,-1*f1]);
a=addmf(a,'input',1,'c','trimf',[-1*f1,0,1*f1]);
a=addmf(a,'input',1,'d','trimf',[1*f1,2*f1,3*f1]);
a=addmf(a,'input',1,'e','trimf',[3*f1,4*f1,5*f1]);
%输入2
f2=1;
a=addvar(a,'input','ec',[-5*f2,5*f2]);
%添加 ec 的模糊语言变量
a=addmf(a,'input',2,'A','zmf',[-5*f2,-2*f2]);
a=addmf(a,'input',2,'B','zmf',[-3*f2,0]);
a=addmf(a,'input',2,'C','zmf',[-1*f2,2*f2]);
a=addmf(a,'input',2,'D','zmf',[1*f2,3*f2]);
a=addmf(a,'input',2,'E','zmf',[2*f2,5*f2]);
%输出
f3=1.5;
a=addvar(a,'output','u',[-5*f3,5*f3]);
%添加 u 的模糊语言变量
a=addmf(a,'output',1,'Aa','smf',[-5*f3,-4*f3]);
a=addmf(a,'output',1,'Bb','smf',[-4*f3,-3*f3]);
a=addmf(a,'output',1,'Cc','smf',[-3*f3,-2*f3]);
a=addmf(a,'output',1,'Dd','smf',[-2*f3,-1*f3]);
a=addmf(a,'output',1,'Ee','smf',[-1*f3,0]);
a=addmf(a,'output',1,'aB','zmf',[0,1*f3]);
a=addmf(a,'output',1,'aC','zmf',[1*f3,2*f3]);
a=addmf(a,'output',1,'aD','zmf',[2*f3,3*f3]);
a=addmf(a,'output',1,'aE','zmf',[3*f3,4*f3]);
a=addmf(a,'output',1,'bC','zmf',[4*f3,5*f3]);
a=addmf(a,'output',1,'bD','trimf',[-5*f3,-4*f3,-3*f3]);
a=addmf(a,'output',1,'bE','trimf',[-3*f3,-2*f3,-1*f3]);
a=addmf(a,'output',1,'cD','trimf',[-1*f3,0,1*f3]);
a=addmf(a,'output',1,'cE','trimf',[1*f3,2*f3,3*f3]);
a=addmf(a,'output',1,'dE','trimf',[3*f3,4*f3,5*f3]);
a=addmf(a,'output',1,'NULL','zmf',[-1*f3,0,1*f3]);
%规则库
rulelist=[1 1 1 1 1; %编辑模糊规则,后俩个数分别是规则权重和AND OR选项
1 2 6 1 1;
1 3 7 1 1;
1 4 8 1 1;
1 5 9 1 1;
2 1 16 1 1;
2 2 2 1 1
2 3 10 1 1;
2 4 11 1 1;
2 5 12 1 1;
3 1 16 1 1;
3 2 16 1 1;
3 3 3 1 1;
3 4 13 1 1;
3 5 14 1 1;
4 1 16 1 1;
4 2 16 1 1;
4 3 16 1 1;
4 4 4 1 1;
4 5 15 1 1;
5 1 16 1 1;
5 2 16 1 1;
5 3 16 1 1;
5 4 16 1 1;
5 5 5 1 1;];
a=addrule(a,rulelist); %添加模糊规则函数
showrule(a) %显示模糊规则函数
a1=setfis(a,'DefuzzMethod','centroid'); %设置解模糊方法
writefis(a1,'fuzzf'); %保存模糊系统
a2=readfis('fuzzf'); %从磁盘读出保存的模糊系统
disp('fuzzy Controller table:e=[-3,+3],ec=[-3,+3]');%显示矩阵和数组内容
%推理
Ulist=zeros(7,7); %全零矩阵
for i=1:7
for j=1:7
e(i)=-4+i;
ec(j)=-4+j;
Ulist(i,j)=evalfis([e(i),ec(j)],a2); %完成模糊推理计算
end
end
% Ulist=ceil(Ulist) %朝正无穷方向取整
Ulist %朝正无穷方向取整
%画出模糊系统
figure(1); plotfis(a2);
figure(2);plotmf(a,'input',1);
figure(3);plotmf(a,'input',2);
figure(4);plotmf(a,'output',1);