www.pudn.com > PSO_GA_RBF.rar > forecast.asv, change:2009-05-01,size:2010b

```clear all
close all

%分别使用粒子群算法，遗传算法和未经过优化权值的RBF网络做预测

%

%学习系数
alfa = 0.05;
xite = 0.85;
x = [0,0]';

for M=1:3
if M==1   %取粒子群算法进化的权值
b=[gbest(1);gbest(2);gbest(3)];
c=[gbest(4) gbest(5) gbest(6);
gbest(7) gbest(8) gbest(9)];
w=[gbest(10);gbest(11);gbest(12)];
elseif M==2   %取遗传算法进化的权值
b=[p(1);p(2);p(3)];
c=[p(4) p(5) p(6);
p(7) p(8) p(9)];
w=[p(10);p(11);p(12)];
elseif M==3   %权值重新初始化
b=3*rand(3,1);
c=3*rands(2,3);
w=rands(3,1);
end

w_1 = w;w_2 = w_1;
c_1 = c;c_2 = c_1;
b_1 = b;b_2 = b_1;

y_1 = 0;

ts = 0.001;
for k = 1:1:1500
time(k) = k*ts;

%RBF网络的输入，控制量和系统上一个输入量
u(k) = sin(5*2*pi*k*ts);
y(k) = u(k)^3 + y_1/(1 + y_1^2);
x(1) = u(k);
x(2) = y(k);

%网络预测的输入
for j = 1:1:3
h(j) = exp(-norm(x - c(:,j))^2/(2*b(j)*b(j)));
end
ym(M,k) = w_1'*h';

%预测输出和实际输出的误差
e(M,k) = y(k) - ym(M,k);

%调整权值
d_w = 0*w;d_b = 0*b;d_c = 0*c;
for j = 1:1:3
d_w(j) = xite*e(M,k)*h(j);
d_b(j) = xite*e(M,k)*w(j)*h(j)*(b(j)^-3)*norm(x-c(:,j))^2;
for i = 1:1:2
d_c(i,j) = xite*e(M,k)*w(j)*h(j)*(x(i) - c(i,j))*(b(j)^-2);
end
end
w = w_1 + d_w + alfa*(w_1 - w_2);
b = b_1 + d_b + alfa*(b_1 - b_2);
c = c_1 + d_c + alfa*(c_1 - c_2);

y_1 = y(k);
w_2 = w_1;
w_1 = w;
c_2 = c_1;
c_1 = c;
b_2 = b_1;
b_1 = b;
end
end
figure(1)
plot(e(1,:));
hold on
plot(e(2,:),'r');
hold on
plot(e(3,:),'g');
title('各种算法对应的预测误差')
legend('PSO优化误差','GA优化误差','无优化误差')
xlabel('进化系数');
ylabel('预测误差');

figure(2)
plot(y);
hold on
plot(ym(1,:),'');
hold on
plot(ym(2,:),'r');
hold on
plot(ym(3,:),'g');```