level 1
function
bp
sin
%初始化各参数
l=0.05;
n=30;
cells=6;
times=3000;
x=(linspace(0,2*pi,n))
%选其样本点
t=sin(x);
w1=rand(cells,1)*0.05;
w2=rand(1,cells)*0.05;
yw1=rand(cells,1)*0.05;
yw2=rand*0.05;
y=rand(1,n)*0.05;
counts=1;
e=zeros(1,times);
%学习过程
for i=1:times
ei=0;
for a=1:n
net1=w1*x(a)-yw1;
out=logsig(net1);
net2=w2*out-yw2;
y(a)=net2;
det2=t(a)-y(a);
det1=((det2*(w2)').*out).*(1-out);
w1=w1+det1*x(a)*1;
w2=w2+(det2*out)'*1;
yw1=-det1*1+yw1;
yw2=-det2*1+yw2;
ei=ei+det2^2/2;
e(i)=ei;
end
end
%逼近曲线
for a=1:n
net1=w1*x(a)-yw1;
out=logsig(net1);
net2=w2*out-yw2;
y(a)=net2;
end
subplot(2,1,1);
plot(x,t,'b-',x,y,'k*-');
grid on;
title('BP学习方法逼近y=sin(x)');
xlabel('x轴');
ylabel('y=sin(x)');
%误差曲线
if(counts<times)
count=1:counts;
sum=counts;
else
count=1:times;
sum=times;
end
subplot(2,1,2);
plot(count,e(1:sum));
grid on;
title('BP算法曲线');
xlabel('迭代次数');
ylabel('均方误差');
return;
2014年05月12日 08点05分