clear
clc
load ('C:\Users\wab\Desktop\神经网络\第一次实验Adaline\lms_samp.mat');
X=samp(1:200,1:2);
X(:,3)=1;
d=samp(:,3);
W(1:3)=0;
counts=0;
P=1;
alpha=0.002;
e(1:P)=0;
ex(1:P,1:3)=0;
mse=3;
MSE=0;
ex1(1:200)=0;
while mse>=0.2627
n=randperm(200);
N=n(1:P);
for K=1:length(N);
e(K)=d(N(K))-W*(X(N(K),1:3)');
ex(K,:)=e(K)*X(N(K),1:3);
end
if P==1
ave=ex;
else ave=mean(ex);
end
W=W+2*alpha*ave;
counts=counts+1;
for j=1:length(samp)
ex1(j)=d(j)-W*X(j,:)';
end
MSE(counts)=mean(ex1.^2);
mse=MSE(counts);
if counts>=1000
break
end
end
plot(MSE);legend('alpha=0.002');
xlabel('迭代次数');ylabel('均方误差MSE');
title('随机逼近算法');grid on;
disp('W=');disp(W);disp('counts=');disp(counts);
评论1