You are on page 1of 1

function [w, b]=neural_1(alpha)

p=[0 1 0 1;0 0 1 1]';


% t=[0 1 1 1];%or
t=[0 0 0 1];%and
% t=[0 1 1 0];%xor
close all
w(1:2)=rand(2,1);
b=rand(1);
a=[0];
% t=[5];
hold on
epoch=0;
uu=sum(abs(a-t).^2)/length(p);
while uu>0 | epoch==0
epoch=epoch+1;
plot(epoch,sum(abs(a-t).^2)/length(p),'o')
drawnow
if mod(epoch,150)==0
w(1:2)=rand(2,1)-0.5;
b=rand(1)-1;
end
for i=1:length(p)
a(i)=hardlim(w*p(i,:)'+b);
e=(-a(i)+t(i));
if abs(e)>0.1
w(1)=w(1)+alpha*(t(i)-a(i))*p(i,1);
w(2)=w(2)+alpha*(t(i)-a(i))*p(i,2);
b=b+alpha*abs(t(i)-a(i))*(-1);
end
q(i+4*(epoch-1),1:9)=[epoch p(i,1) p(i,2) t(i) a(i) e w(1) w(2) b];

end
uu=sum(abs(a-t).^2)/length(p);
end
epoch=epoch+1;
plot(epoch,sum(abs(a-t).^2)/length(p),'o')
xlswrite('net.xlsx',repmat(' ',[100 9]),1,'A1')
xlswrite('net.xlsx',{'epoch' 'x1' 'x2' 'yd' 'ya' 'e' 'w1' 'w2' 'b'},1,['A1'])
xlswrite('net.xlsx',q,1,['A2'])

You might also like