clear;a=7;mu=0.01;K=1000;%K=#iterations
%Enter truth table. Last row=d[j].
%F=[0 0 1 1;0 1 0 1;0 1 1 1];%OR
%F=[0 0 1 1;0 1 0 1;0 0 0 1];%AND
%F=[1 0;0 1;0 1;1 0;0 1];%2X2 images.
[M,N]=size(F);X=F(1:M-1,:);D=F(M,:);
W=0.1*ones(M,1);%Initialization 
for I=1:K;for J=1:N;%N=#trial signals.
%Run NN forward. W(1)=constant offset:
v=W'*[1;X(:,J)];y=1/(1+exp(-a*v));
%Backpropagation:
delta=-a*(D(J)-y)*y*(1-y);
dEdW=delta*[1;X(:,J)];W=W-mu*dEdW;
end;end;%Check result:
for J=1:N;v=W'*[1;X(:,J)];y(J)=1/(1+exp(-a*v));end;[y;D]

%   0.0815    0.9496    0.9495    0.9998   W=[-0.346;0.765;0.766]
%   0         1.0000    1.0000    1.0000

 
%    0.0009    0.0819    0.0822    0.9033   W=[-1.01;06645;0.6639]
%         0         0         0    1.0000


%    0.0237    0.9763   W=[-0.0500;-0.2406;0.2907;0.2907;-0.2406];
%         0    1.0000