• 【模糊神经网络】基于matlab的模糊神经网络仿真


    1.软件版本

    matlab2013b

    2.系统概述

    ·第一个模型:

    ·第二个模型

    ,U=13.012

    第一:隶属函数的设计

        隶属函数的设计,可以通过模糊编辑器,也可以通过如上的代码进行设计。

    第二:模糊规则的设计

    通过输入模糊规则量化表进行设计,所得到的模糊规则如下所示:

    1. If (e is NB) and (ec is NB) then (u is PB) (1)

    2. If (e is NB) and (ec is NM) then (u is PB) (1)

    3. If (e is NB) and (ec is NS) then (u is PM) (1)

    4. If (e is NB) and (ec is Z) then (u is PM) (1) 

    5. If (e is NB) and (ec is PS) then (u is PS) (1)

    6. If (e is NB) and (ec is PM) then (u is PS) (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 PB) (1)

    9. If (e is NM) and (ec is NM) then (u is PM) (1)

    10. If (e is NM) and (ec is NS) then (u is PM) (1)

    11. If (e is NM) and (ec is Z) then (u is PS) (1)

    12. If (e is NM) and (ec is PS) then (u is PS) (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 NS) (1)

    15. If (e is NS) and (ec is NB) then (u is PM) (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 Z) (1)

    20. If (e is NS) and (ec is PM) then (u is NS) (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 PM) (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 PS) (1)

    25. If (e is Z) and (ec is Z) then (u is Z) (1)  

    26. If (e is Z) and (ec is PS) then (u is NS) (1)

    27. If (e is Z) and (ec is PM) then (u is NS) (1)

    28. If (e is Z) and (ec is PB) then (u is NM) (1)

    29. If (e is PS) and (ec is NB) then (u is PS) (1)

    30. If (e is PS) and (ec is NM) then (u is PS) (1)

    31. If (e is PS) and (ec is NS) then (u is Z) (1)

    32. If (e is PS) and (ec is Z) then (u is NS) (1)

    33. If (e is PS) and (ec is PS) then (u is NS) (1)

    34. If (e is PS) and (ec is PM) then (u is NM) (1)

    35. If (e is PS) and (ec is PB) then (u is NM) (1)

    36. If (e is PM) and (ec is NB) then (u is PS) (1)

    37. If (e is PM) and (ec is NM) then (u is PS) (1)

    38. If (e is PM) and (ec is NS) then (u is Z) (1)

    39. If (e is PM) and (ec is Z) then (u is NS) (1)

    40. If (e is PM) and (ec is PS) then (u is NM) (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 NB) (1)

    43. If (e is PB) and (ec is NB) then (u is Z) (1)

    44. If (e is PB) and (ec is NM) then (u is NS) (1)

    45. If (e is PB) and (ec is NS) then (u is NS) (1)

    46. If (e is PB) and (ec is Z) then (u is NM) (1)

    47. If (e is PB) and (ec is PS) then (u is NM) (1)

    48. If (e is PB) and (ec is PM) then (u is NB) (1)

    49. If (e is PB) and (ec is PB) then (u is NB) (1)

    第三:控制闭环的设计

    通常,一个传统的模糊控制器的闭环结构如下所示:

    模糊控制器的基本结构:

    3.部分源码

    1. addpath 'func\'
    2. title_function
    3. %初始化
    4. fnn_parameter;
    5. %被控对象
    6. a1 = 1.2;
    7. b1 = 1;
    8. b2 = 0.8;
    9. b3 = 0;
    10. ta = 40;
    11. sys = tf(a1,[b1,b2,b3]);
    12. dsys = c2d(sys,0.1,'z');
    13. [num,den] = tfdata(dsys,'v');
    14. ts = 0.1;%采样时间T=0.1
    15. %闭环控制器
    16. for k=1:SIM_times
    17. k
    18. time(k) = k*ts;
    19. %定义输入信号
    20. yd(k) = 2;
    21. %定义输出信号
    22. if k < ta
    23. yn = 0;
    24. else
    25. yn = -den(2)*y1 - den(3)*y2 + num(2)*u1 + num(3)*u2;
    26. end
    27. y2 = y1;
    28. y1 = yn;
    29. y(k) = yn;
    30. u2 = u1;
    31. e2 = e1;
    32. e1 = yd(k)-yn;
    33. e(k) = e1;
    34. ec =(e1-e2);
    35. x1 =(1-exp(-10*e1))/(1+exp(-10*e1));
    36. x2 =(1-exp(-ec))/(1+exp(-ec));
    37. %第1层输出
    38. for i=1:7
    39. o11(i) = x1;
    40. o12(i) = x2;
    41. end
    42. o1=[o11;o12];
    43. %第2层输出
    44. for i=1:2
    45. for j=1:7
    46. z1(i,j) =-((o1(i,j)-a(i,j))^2)/(b(i,j));
    47. o2(i,j) = exp(z1(i,j));
    48. end
    49. end
    50. %第3层输出
    51. for j=1:7
    52. for l=1:7
    53. o3((j-1)*7+l)=o2(1,j)*o2(2,l);
    54. end
    55. end
    56. %第4层输出
    57. I=0;
    58. for i=1:49
    59. I = I + o3(i)*Weight(i)/4;
    60. end
    61. o4 = I/(sum(o3));
    62. u(k) = o4;
    63. u1 = o4;
    64. %梯度下降法调整权值
    65. for i=1:49
    66. dwp = e1*du*o3(i)/(sum(o3));
    67. %迭代
    68. Weight(i) = Weight(i) + eta*dwp;
    69. end
    70. %中心值更新
    71. da11=zeros(1,7);
    72. for j=1:7
    73. for l=1:7
    74. da11(j) = da11(j)+(o2(2,l)*((Weight((j-1)*7+l)*sum(o3))-I));
    75. end
    76. da12(1,j) = -e1*du*(2*(o1(1,j)-a(1,j))*(o2(1,j)))/((b(1,j)^2)*(sum(o3))^2);
    77. da1(j) = (da12(1,j))*(da11(j));
    78. end
    79. da21 = zeros(1,7);
    80. for j=1:7
    81. for l=1:7
    82. da21(j) = da21(j)+(o2(1,l)*((Weight((l-1)*7+j)*sum(o3))-I));
    83. end
    84. da22(2,j) = -e1*du*(2*(o1(2,j)-a(2,j))*(o2(2,j))/((b(2,j)^2)*(sum(o3))^2));
    85. da2(j) = (da22(2,j))*(da21(j));
    86. end
    87. da=[da1;da2];
    88. for i=1:2
    89. for j=1:7
    90. a(i,j)=a(i,j)-eta*da(i,j);
    91. end
    92. end
    93. a_s(:,:,k) = a;
    94. if k == 1
    95. a_(:,:,k) = a_s(:,:,1);
    96. else
    97. for i = 1:2
    98. for j = 1:7
    99. dist_tmp(i,j) = (a_s(i,j,k) - a_(i,j))^2;
    100. end
    101. end
    102. dist = sqrt(sum(sum(dist_tmp)));
    103. if dist < 0.1
    104. tmps(:,:,1) = a_(:,:,k-1);
    105. tmps(:,:,2) = a_s(:,:,k);
    106. a_(:,:,k) = mean(tmps(:,:,1:2),3);
    107. else
    108. a_(:,:,k) = a_(:,:,k-1);
    109. end
    110. end
    111. a = a_(:,:,k);
    112. %宽度更新
    113. db11=zeros(1,7);
    114. for j=1:7
    115. for l=1:7
    116. db11(j)=db11(j)+(o2(2,l)*((Weight((j-1)*7+l)*sum(o3))-I));
    117. end
    118. db12(1,j)=-e1*du*(2*(o1(1,j)-a(1,j))^2)*(o2(1,j))/((b(1,j)^3)*(sum(o3))^2);
    119. db1(j)=(db12(1,j))*(db11(j));
    120. end
    121. db21=zeros(1,7);
    122. for j=1:7
    123. for l=1:7
    124. db21(j)=db21(j)+(o2(1,l)*((Weight((l-1)*7+j)*sum(o3))-I));
    125. end
    126. db22(2,j)=-e1*du*(2*(o1(2,j)-a(2,j))^2)*(o2(2,j))/((b(2,j)^3)*(sum(o3))^2);
    127. db2(j)=(db22(2,j))*(db21(j));
    128. end
    129. db=[db1;db2];
    130. for i=1:2
    131. for j=1:7
    132. b(i,j)=b(i,j)-eta*db(i,j);
    133. end
    134. end
    135. b_s(:,:,k) = b;
    136. if k == 1
    137. b_(:,:,k) = b_s(:,:,1);
    138. else
    139. for i = 1:2
    140. for j = 1:7
    141. dist_tmp(i,j) = (b_s(i,j,k) - b_(i,j))^2;
    142. end
    143. end
    144. dist = sqrt(sum(sum(dist_tmp)));
    145. if dist < 0.1
    146. tmps(:,:,1) = b_(:,:,k-1);
    147. tmps(:,:,2) = b_s(:,:,k);
    148. b_(:,:,k) = mean(tmps(:,:,1:2),3);
    149. else
    150. b_(:,:,k) = b_(:,:,k-1);
    151. end
    152. end
    153. b = b_(:,:,k);
    154. %算法
    155. s11 = y1;
    156. s12 = y2;
    157. s13 = u1;
    158. s14 = u2;
    159. s1 =[s11;s12;s13;s14];
    160. for i=1:5
    161. net2(i) = w2(i,:)*s1 + theta2(i);
    162. s2(i) = (1-exp(-net2(i)))/(1+exp(-net2(i)));
    163. end
    164. net3 = w3*s2+theta3;
    165. yg = am*(1-exp(-net3))/(1+exp(-net3));
    166. for i=1:5
    167. delta2(i)=0.5*(1-s2(i))*(1+s2(i));
    168. end
    169. delta3=0.5*am*(1-yg/am)*(1+yg/am);
    170. for i=1:5
    171. theta22(i) = theta2(i)-theta21(i);
    172. theta21(i) = theta2(i);
    173. theta2(i) = theta2(i)+eta1*(yn-yg)*delta3*w3(i)*delta2(i)+beta1*theta22(i);
    174. end
    175. theta32 = theta3-theta31;
    176. theta31 = theta3;
    177. theta3 = theta3+eta1*(yn-yg)*delta3+beta1*theta32;
    178. for i=1:5
    179. for j=1:4
    180. w22(i,j) = w2(i,j)-w21(i,j);
    181. w21(i,j) = w2(i,j);
    182. w2(i,j) = w2(i,j)-eta1*(yn-yg)*delta3*w3(i)*delta2(i)*s1(j)+beta1*w22(i,j);
    183. end
    184. w32(i) = w3(i)-w31(i);
    185. w31(i) = w3(i);
    186. w3(i) = w3(i)-eta1*(yn-yg)*delta3*s2(i)+beta1*w32(i);
    187. end
    188. a2 = am-a1;
    189. a1 = am;
    190. am = am+eta1*(yn-yg)*yg/am+beta1*a2;
    191. sum1 = 0;
    192. for i=1:5
    193. sum1 = sum1 + w3(i)*delta2(i)*w2(i,3);
    194. end
    195. du = delta3*sum1;
    196. end
    197. figure;
    198. plot(time,y,'r', time,yd,'b');
    199. grid on
    200. figure;
    201. subplot(121);
    202. plot(a_s(1,:,SIM_times),a_s(2,:,SIM_times),'o');
    203. grid on
    204. axis square
    205. subplot(122);
    206. plot(b_s(1,:,SIM_times),b_s(2,:,SIM_times),'o');
    207. grid on
    208. axis square
    209. save Simu_Results\fnn_result.mat time y
    210. save Simu_Results\nfis.mat a b

        这里重点介绍一下模糊神经网络控制器的设计,

    第一:四层化神经网络层的结构设计:

    第1层:

    第2层:

    第3层:

    第4层:

    第二:利用梯度下降法进行权值更新

    4.仿真结果

    模糊控制效果图(模型一):

     模糊控制效果图(模型二):

        隶属函数如下所示:

     

     

     

     

     

     A05-06

  • 相关阅读:
    Java国密加密SM3代码
    【C++】STL详解(十四)—— bitset(位图)的模拟实现
    安装与脏数据绕过_安全狗
    Java教程:只会懒汉式和饿汉式,那你还不算真的懂单例模式
    Django测试与持续集成:从入门到精通
    Elasticsearch查询过程
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统功能清单
    【ENOVIA的使用】CESA空客集团数据统一管理案例
    文件操作安全之-文件读取原理篇
    【AAAI2022】Efficient Non-Local Contrastive Attention for Image Super-Resolution
  • 原文地址:https://blog.csdn.net/ccsss22/article/details/125490278