|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
clc
clear
%step 1=========================
%定义输入样本;
P1=[0.251 0.503 0.952 1.335
0.724 0.692 0.584 0.448
2.002 1.964 1.812 1.613
1.735 1.684 1.513 1.289
3.287 3.225 3.025 2.746
0.243 0.495 0.901 1.337
0.703 0.661 0.570 0.433
1.983 1.924 1.863 1.671
1.659 1.588 1.458 1.269
3.277 3.211 2.992 2.703
0.253 0.501 0.941 1.310
0.872 0.843 0.727 0.576
2.032 1.993 1.853 1.659
1.400 1.362 1.211 1.019
3.268 3.202 2.983 2.690
0.247 0.500 0.891 1.291
0.726 0.667 0.607 0.390
1.891 1.813 1.776 1.510
1.237 1.186 1.106 1.005
3.255 3.216 3.105 2.912
0.262 0.511 0.973 1.359
0.697 0.664 0.575 0.412
1.781 1.743 1.612 1.449
1.125 1.081 1.031 1.982
3.303 3.254 3.048 2.759
];
%归一化;
tt=max(P1');
for i=1:25
P(i,:)=P1(i,:)/tt(i);
end
mubiao1=[0 0 0 1
0 0 0 1
0 0 0 1
0 0 0 1
0 0 0 1
0 0 1 0
0 0 1 0
0 0 1 0
0 0 1 0
0 0 1 0
0 0 1 1
0 0 1 1
0 0 1 1
0 0 1 1
0 0 1 1
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 1
0 1 0 1
0 1 0 1
0 1 0 1
0 1 0 1
];
aerfa=1;eta=1;
%初始化连接权 wjh(输出层和隐层的连接权);whi(隐层和输出层的连接权);
%假设小波函数节点数为:H 个;样本数为 P;
%输出节点数为:J 个;输入节点数为:I 个;
H=10;%隐层节点数;
A=size(mubiao1);B=A(1);%样本数;
I1=size(P1);I=I1(2);%输入节点数;
J1=size(mubiao1);J=J1(2);%输出节点数; %初始化小波参数
b=0.9*rand(H,1);
a=rand(H,1);
%初始化权系数;
whi=rand(I,H);wjh=rand(H,J);
%阈值初始化;
b1=rand(H,1);
b2=rand(J,1);p=1;%将 p 初始化;
%保存的误差;
Err_NetOut=[];ppp=0;
flag=1;count=0;%count 为迭代的次数;
while flag>0
flag=0;
x=P(p,:);
mubiao=mubiao1(p,:);
%step 2=================================
xhp1=0;
for h=1:H
for i=1:I
xhp1=xhp1+whi(i,h)*x(i);
end
ixhp(h)=xhp1+b1(h);
xhp1=0;
end
for h=1:H
oxhp(h)=fai((ixhp(h)-b(h))/a(h));
end
%step 3====================================
ixjp1=0;
for j=1:J
for h=1:H
ixjp1=ixjp1+wjh(h,j)*oxhp(h);
end
ixjp(j)=ixjp1+b2(j);
ixjp1=0;
end
oxjp=[ixjp(1) ixjp(2) ixjp(3) ixjp(4)];
%step 6==保存每次误差=====
wuchayy=abs(oxjp-mubiao)/mubiao;
E_x=1/2*sumsqr(x);
Err_NetOut=[Err_NetOut wuchayy];%保存每次的误差;
Err_rate=Err_NetOut/E_x;
%Err_rate
%oxjp
%求 detaj ,detab2==================================
for j=1:J
detaj(j)=-(oxjp(j)-mubiao(j))*oxjp(j)*(1-oxjp(j));
end
for j=1:J
for h=1:H
detawjh(h,j)=eta*detaj(j)*oxhp(h);
end
end
detab2=eta*detaj;
%求 detah, detawhi detab1 detab detaa;========================
sum=0;
for h=1:H
for j=1:J
sum=detaj(j)*wjh(h,j)*diffai((ixhp(h)-b(h))/a(h))/a(h)+sum;
end
detah(h)=sum;
sum=0;
end
for h=1:H
for i=1:I
detawhi(i,h)=eta*detah(h)*x(i);
end
end
detab1=eta*detah;
detab=-eta*detah;
for h=1:H
detaa(h)=-eta*detah(h)*((ixhp(h)-b(h))/a(h));
end
%引入动量因子 aerfa,修正各个系数=========================
wjh=wjh+1*detawjh;
whi=whi+1*detawhi;
a=a+1*detaa';
b=b+1*detab';
b1=b1+1*detab1';
b2=b2+1*detab2';
%============================================
%判断所有的样本是否计算完=============================
if p==B
count=count+1;
ppp=ppp+1;
error=0;
for i=1:length(Err_NetOut)
error=error+Err_NetOut(i);
end
wuchaquxian(ppp)=error;%记入个次误差;
Err_NetOut=[];
if (error<0.5|count>100)
disp('程序结束');
else
p=1;
flag=flag+1;
end
%disp('第一次训练完毕');
else
p=p+1;%计算下一组样本;
flag=flag+1;
end
end
%%=================================================================
%将训练好的网络用于故障诊断
p_test1=[0.254 0.491 0.912 1.383
0.252 0.491 0.913 1.383
0.253 0.491 0.914 1.383
0.254 0.491 0.912 1.383
0.254 0.491 0.912 1.383
0.776 0.652 0.593 0.474
0.776 0.651 0.590 0.473
0.775 0.651 0.592 0.471
0.775 0.651 0.592 0.471
2.003 1.938 1.807 1.660
1.981 1.922 1.869 1.670
1.659 1.588 1.458 1.269
1.606 1.551 1.482 1.331
1.400 1.362 1.211 1.019
3.313 3.264 3.148 2.957
3.277 3.211 2.992 2.703
3.263 3.212 3.038 3.818];
kkk=max(p_test1');
for i=1:17
p_test(i,:)=p_test1(i,:)/kkk(i);
end
%===========================================
ll=size(p_test);
for p=1:ll(1);
x=p_test(p,:);
%step 2=================================
xhp1=0;
for h=1:H
for i=1:I
xhp1=xhp1+whi(i,h)*x(i);
end
ixhp(h)=xhp1+b1(h);
end
for h=1:H
oxhp(h)=fai((ixhp(h)-b(h))/a(h));
end
%step 3====================================
ixjp1=0;
for j=1:J
for h=1:H
ixjp1=ixjp1+wjh(h,j)*oxhp(h);
end
ixjp(j)=ixjp1+b2(j);
end
oxjp=[ixjp(1) ixjp(2) ixjp(3) ixjp(4)];
yuce(p,:)=oxjp;
end
disp('诊断结果');
disp(yuce);
p=1:ppp;
wuchaquxian
xlabel('迭代次数');
ylabel('误差');
plot(p,wuchaquxian)
%fai和diffai 的函数调用如下:
function y=diffai(t);
y = -1.75*sin(1.75*t).*exp(-(t.^2)/2)-t* cos(1.75*t).*exp(-(t.^2)/2);
function y=fai(t);
y = exp(-(t.^2)/2) * cos(1.75*t);
[ 本帖最后由 eight 于 2008-5-5 10:22 编辑 ] |
|