**
模糊逻辑的计算
**
1、算法例子介绍
设计一个放映机的自动控制系统,根据设备内的温度和室内温度决定设备还可以运行的时间长短,输入变量是设备温度和室内温度,输出为运转时间。
室内温度的模糊标记有3个:低、中、高,设备温度的模糊标记有3个:冷、中、热,运转时间的模糊标记也有3个:短、中、长。
2、算法流程
2.1运行代码:
%模糊控制器设计
a=newfis('fuzzf'); %创建新的模糊推理系统
%输入1
f1=1;
a=addvar(a,'input','勤奋度',[0*f1,100*f1]);
%添加 e 的模糊语言变量
a=addmf(a,'input',1,'懒惰','zmf',[0*f1,25*f1]);
%添加 e 的模糊语言变量的隶属度函数(z型)
a=addmf(a,'input',1,'一般','trimf',[20*f1,40*f1,50]);
%隶属度函数为三角形
a=addmf(a,'input',1,'优秀','trimf',[45*f1,65*f1,75]);
a=addmf(a,'input',1,'非常人','trimf',[70*f1,85,100]);
%输入2
f2=1;
a=addvar(a,'input','天赋',[0*f2,100*f2]);
%添加 ec 的模糊语言变量
a=addmf(a,'input',2,'普通','zmf',[0*f2,30*f2]);
a=addmf(a,'input',2,'尚可','trimf',[25*f2,40*f2,60]);
a=addmf(a,'input',2,'优秀','trimf',[50*f2,70*f2,90*f2]);
a=addmf(a,'input',2,'传奇','trimf',[85*f2,90,100*f2]);
%输出
f3=1;
a=addvar(a,'output','成就',[0*f3,100*f3]);
%添加 u 的模糊语言变量
a=addmf(a,'output',1,'平民','zmf',[0*f3,25*f3]);
a=addmf(a,'output',1,'白领','trimf',[20*f3,35*f3,45]);
a=addmf(a,'output',1,'成功人士','trimf',[40*f3,60*f3,80*f3]);
a=addmf(a,'output',1,'富豪','trimf',[75*f3,85,100*f3]);
%规则库
rulelist=[1 1 1 1 1; %编辑模糊规则,后俩个数分别是规则权重和AND OR选项
1 2 2 1 1;
1 3 3 1 1;
1 4 4 1 1;
2 1 2 1 1;
2 2 3 1 1;
2 3 3 1 1;
2 4 4 1 1;
3 1 2 1 1;
3 2 3 1 1;
3 3 4 1 1;
3 4 4 1 1;
4 1 3 1 1;
4 2 3 1 1;
4 3 4 1 1;
4 4 4 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(4,4); %全零矩阵
for i=1:4
for j=1:4
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);
2.2代码解释
输入变量模糊化 有以下16条规则:
- If (勤奋度 is 懒惰) and (天赋 is 普通) then (成就 is 平民)
- If (勤奋度 is 懒惰) and (天赋 is 尚可) then (成就 is 白领)
- If (勤奋度 is 懒惰) and (天赋 is 优秀) then (成就 is 成功人士)
- If (勤奋度 is 懒惰) and (天赋 is 传奇) then (成就 is 富豪)
- If (勤奋度 is 一般) and (天赋 is 普通) then (成就 is 白领)
- If (勤奋度 is 一般) and (天赋 is 尚可) then (成就 is 成功人士)
- If (勤奋度 is 一般) and (天赋 is 优秀) then (成就 is 成功人士)
- If (勤奋度 is 一般) and (天赋 is 传奇) then (成就 is 富豪)
- If (勤奋度 is 优秀) and (天赋 is 普通) then (成就 is 白领)
- If (勤奋度 is 优秀) and (天赋 is 尚可) then (成就 is 成功人士)
- If (勤奋度 is 优秀) and (天赋 is 优秀) then (成就 is 富豪)
- If (勤奋度 is 优秀) and (天赋 is 传奇) then (成就 is 富豪)
- If (勤奋度 is 非常人) and (天赋 is 普通) then (成就 is 成功人士)
- If (勤奋度 is 非常人) and (天赋 is 尚可) then (成就 is 成功人士)
- If (勤奋度 is 非常人) and (天赋 is 优秀) then (成就 is 富豪)
- If (勤奋度 is 非常人) and (天赋 is 传奇) then (成就 is 富豪)
2.3代码运行结果
模糊控制规则 及矩阵
————————————————
版权声明:本文为CSDN博主「4oo」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45583603/article/details/102683323