模糊控制

一、代码实现

%模糊控制器设计
a=newfis('fuzzf');                   %创建新的模糊推理系统 命名为a

%输入1
f1=1; 
a=addvar(a,'input','e',[-15*f1,15*f1]);               %e:error    
 %添加 e 的模糊语言变量
a=addmf(a,'input',1,'NB','zmf',[-15*f1,-3*f1]);     %z型     
 %添加 e 的模糊语言变量的隶属度函数(z型)
a=addmf(a,'input',1,'NM','trimf',[-15*f1,-9*f1,0]);      
  %隶属度函数为三角形
a=addmf(a,'input',1,'NS','trimf',[-15*f1,-6*f1,5*f1]); 
a=addmf(a,'input',1,'Z','trimf',[-10*f1,0,10*f1]); 
a=addmf(a,'input',1,'PS','trimf',[-8*f1,6*f1,15*f1]);
a=addmf(a,'input',1,'PM','trimf',[0,10*f1,15*f1]);
a=addmf(a,'input',1,'PB','smf',[8*f1,15*f1]); 

%输入2
f2=1;
a=addvar(a,'input','ec',[-18*f2,18*f2]);                   
 %添加 ec 的模糊语言变量
a=addmf(a,'input',2,'NB','zmf',[-18*f2,-8*f2]); 
a=addmf(a,'input',2,'NM','trimf',[-18*f2,-15*f2,0]);
a=addmf(a,'input',2,'NS','trimf',[-18*f2,-9*f2,10*f2]);
a=addmf(a,'input',2,'Z','trimf',[-15*f2,0,12*f2]);
a=addmf(a,'input',2,'PS','trimf',[-8*f2,8*f2,18*f2]);
a=addmf(a,'input',2,'PM','trimf',[0,15*f2,18*f2]);
a=addmf(a,'input',2,'PB','smf',[6*f2,18*f2]); 

%输出
f3=1.5;
a=addvar(a,'output','u',[-20*f3,20*f3]);                 
   %添加 u 的模糊语言变量
a=addmf(a,'output',1,'NB','zmf',[-20*f3,-7*f3]); 
a=addmf(a,'output',1,'NM','trimf',[-20*f3,-15*f3,0]);
a=addmf(a,'output',1,'NS','trimf',[-18*f3,-9*f3,9*f3]);
a=addmf(a,'output',1,'Z','trimf',[-15*f3,0,15*f3]);
a=addmf(a,'output',1,'PS','trimf',[-10*f3,8*f3,20*f3]);
a=addmf(a,'output',1,'PM','trimf',[0,15*f3,20*f3]);
a=addmf(a,'output',1,'PB','smf',[11*f3,20*f3]);


%规则库
rulelist=[     1 1 1 1 1;             %编辑模糊规则,后俩个数分别是规则权重和AND OR选项
               1 2 1 1 1;
               1 3 1 1 1;
               1 4 2 1 1;
               1 5 2 1 1;
               1 6 3 1 1;
               1 7 4 1 1;
          
               2 1 1 1 1;
               2 2 2 1 1;
               2 3 2 1 1;
               2 4 2 1 1;
               2 5 3 1 1;
               2 6 4 1 1;
               2 7 5 1 1;
               
               3 1 7 1 1;
               3 2 6 1 1;
               3 3 5 1 1;
               3 4 5 1 1;
               3 5 3 1 1;
               3 6 1 1 1;
               3 7 3 1 1;
               
               4 1 4 1 1;
               4 2 5 1 1;
               4 3 7 1 1;
               4 4 5 1 1;
               4 5 2 1 1;
               4 6 5 1 1;
               4 7 1 1 1;
               
               5 1 2 1 1;
               5 2 1 1 1;
               5 3 7 1 1;
               5 4 6 1 1;
               5 5 6 1 1;
               5 6 1 1 1;
               5 7 5 1 1;
               
               6 1 3 1 1;
               6 2 4 1 1;
               6 3 5 1 1;
               6 4 6 1 1;
               6 5 1 1 1;
               6 6 2 1 1;
               6 7 7 1 1;
               
               7 1 5 1 1;
               7 2 7 1 1;
               7 3 4 1 1;
               7 4 3 1 1;
               7 5 4 1 1;
               7 6 2 1 1;
               7 7 1 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);

二、结果展示

e的隶属度函数:
在这里插入图片描述
ec的隶属度函数:
在这里插入图片描述
u的隶属度函数:
在这里插入图片描述

在这里插入图片描述
模糊规则:

	'1. If (e is NB) and (ec is NB) then (u is NB) (1) '
    '2. If (e is NB) and (ec is NM) then (u is NB) (1) '
    '3. If (e is NB) and (ec is NS) then (u is NB) (1) '
    '4. If (e is NB) and (ec is Z) then (u is NM) (1)  '
    '5. If (e is NB) and (ec is PS) then (u is NM) (1) '
    '6. If (e is NB) and (ec is PM) then (u is NS) (1) '
    '7. If (e is NB) and (ec is PB) then (u is Z) (1)  '
    '8. If (e is NM) and (ec is NB) then (u is NB) (1) '
    '9. If (e is NM) and (ec is NM) then (u is NM) (1) '
    '10. If (e is NM) and (ec is NS) then (u is NM) (1)'
    '11. If (e is NM) and (ec is Z) then (u is NM) (1) '
    '12. If (e is NM) and (ec is PS) then (u is NS) (1)'
    '13. If (e is NM) and (ec is PM) then (u is Z) (1) '
    '14. If (e is NM) and (ec is PB) then (u is PS) (1)'
    '15. If (e is NS) and (ec is NB) then (u is PB) (1)'
    '16. If (e is NS) and (ec is NM) then (u is PM) (1)'
    '17. If (e is NS) and (ec is NS) then (u is PS) (1)'
    '18. If (e is NS) and (ec is Z) then (u is PS) (1) '
    '19. If (e is NS) and (ec is PS) then (u is NS) (1)'
    '20. If (e is NS) and (ec is PM) then (u is NB) (1)'
    '21. If (e is NS) and (ec is PB) then (u is NS) (1)'
    '22. If (e is Z) and (ec is NB) then (u is Z) (1)  '
    '23. If (e is Z) and (ec is NM) then (u is PS) (1) '
    '24. If (e is Z) and (ec is NS) then (u is PB) (1) '
    '25. If (e is Z) and (ec is Z) then (u is PS) (1)  '
    '26. If (e is Z) and (ec is PS) then (u is NM) (1) '
    '27. If (e is Z) and (ec is PM) then (u is PS) (1) '
    '28. If (e is Z) and (ec is PB) then (u is NB) (1) '
    '29. If (e is PS) and (ec is NB) then (u is NM) (1)'
    '30. If (e is PS) and (ec is NM) then (u is NB) (1)'
    '31. If (e is PS) and (ec is NS) then (u is PB) (1)'
    '32. If (e is PS) and (ec is Z) then (u is PM) (1) '
    '33. If (e is PS) and (ec is PS) then (u is PM) (1)'
    '34. If (e is PS) and (ec is PM) then (u is NB) (1)'
    '35. If (e is PS) and (ec is PB) then (u is PS) (1)'
    '36. If (e is PM) and (ec is NB) then (u is NS) (1)'
    '37. If (e is PM) and (ec is NM) then (u is Z) (1) '
    '38. If (e is PM) and (ec is NS) then (u is PS) (1)'
    '39. If (e is PM) and (ec is Z) then (u is PM) (1) '
    '40. If (e is PM) and (ec is PS) then (u is NB) (1)'
    '41. If (e is PM) and (ec is PM) then (u is NM) (1)'
    '42. If (e is PM) and (ec is PB) then (u is PB) (1)'
    '43. If (e is PB) and (ec is NB) then (u is PS) (1)'
    '44. If (e is PB) and (ec is NM) then (u is PB) (1)'
    '45. If (e is PB) and (ec is NS) then (u is Z) (1) '
    '46. If (e is PB) and (ec is Z) then (u is NS) (1) '
    '47. If (e is PB) and (ec is PS) then (u is Z) (1) '
    '48. If (e is PB) and (ec is PM) then (u is NM) (1)'
    '49. If (e is PB) and (ec is PB) then (u is NB) (1)'

推理结果:

Ulist =

    5.0440    4.2997    3.2584    2.2472    1.2259    0.2722   -0.6236
    5.4975    4.4612    3.4394    2.4453    1.4397    0.5206   -0.2315
    5.5509    4.6198    3.6520    2.6731    1.7789    1.0254    0.2130
    5.6697    4.7398    3.8034    3.0231    2.2442    1.4603    0.6395
    5.8474    4.9392    4.0920    3.2830    2.5747    1.8345    1.0155
    6.0800    5.0198    4.0934    3.2817    2.5712    1.9702    1.3231
    5.9201    4.8529    3.9208    3.1045    2.3900    1.7858    1.1352

三、小结

1.模糊控制系统工作原理

以水位模糊控制为例:
设目标水位为O处,按照日常操作经验,有以下规则:
1、 若当前水位高于目标水位,则向外排水,差值越大,排水越快;
2、 若当前水位低于目标水位,则向内注水,差值越大,注水越快;

根据上述经验,按下列步骤设计模糊控制器:
1)确定观测量和控制量
在这个实例中,观测量为液位差e=h0-h(h0:期望水位),控制量:u(注水量-排水量)。

2)输入量和输出量的模糊化
例如将偏差e分为五个模糊集:负大(NB),负小(NS),零(Z),正小(PS),正大(PB)。控制量u为调节阀门开度的变化,将其分为五个模糊集:负大(NB),负小(NS),零(Z),正小(PS),正大(PB)。

3)模糊规则的描述
根据日常的经验,设计模糊规则:“若e负大,则u负大”…
上述规则采用“IF A THEN B”形式来描述:“if e=NB then u=NB” …

4)求模糊关系
模糊控制规则是一个多条语句,它可以表示为u×e上的模糊子集,即模糊关系R:
R=(NBe×NBu)∪(NSe×NSu)∪(Ze×Zu)∪(PSe×PSu)∪(PBe×PBu)
其中规则内的模糊集运算取交集,规则间的模糊集运算取并集。

5)模糊决策
模糊控制器的输出为误差向量和模糊关系的合成:
u=e ○ R

2.实验分析

本次实验为两输入(e、ec)、一输出(u)模型。首先构造模糊集:
对e:将偏差e划分为7个模糊集,负大(NB)、负小(NS)、负中(NM)、零(Z)、 正小(PS)、正中(PM)、 正大(PB),设定e的取值范围为[-15,15]。
对ec:将偏差ec划分为7个模糊集,负大(NB)、负小(NS)、负中(NM)、零(Z)、 正小(PS)、正中(PM)、 正大(PB),设定ec的取值范围为[-18,18]。
对u:将u划分为7个模糊集,负大(NB)、负小(NS)、负中(NM)、零(Z)、 正小(PS)、正中(PM)、 正大(PB),设定u的取值范围为[-30,30]。
对于建立模糊规则:规则库rulelist中的5列分别表示:前两列(两个输入e和ec)、第3列(输出u)、第4列(信赖度)、第5列(AND、OR选项,AND用1表示,OR用0表示)。由于两输入、一输出都各有7个模糊集,因此总的应该有49种模糊规则。

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值