
Patterns = zeros(4,16);
Patterns(4,[ 2  4  6  8 10 12 14 16]) = 1;
Patterns(3,[ 3  4  7  8 11 12 15 16]) = 1;
Patterns(2,[ 5  6  7  8 13 14 15 16]) = 1;
Patterns(1,[ 9 10 11 12 13 14 15 16]) = 1;
Desired = mod(sum(Patterns,1),2)*1.6 - 0.8;

[NINPUTS,NPATS] = size(Patterns);
NHIDDENS = 5;
[NOUTPUTS,NPATS] = size(Desired);

LearnRate = 0.04;
Momentum = 0;
DerivIncr = 0.02;
deltaW1 = 0;
deltaW2 = 0;

Inputs1 = [ones(1,NPATS); Patterns];

Weights1 = rand(NHIDDENS,1+NINPUTS)-0.5;
Weights2 = rand(NOUTPUTS,1+NHIDDENS)-0.5;

TSS_Limit = 0.02;

for epoch = 1:10000
  bp_innerloop

  if rem(epoch,10) ==0
    fprintf('Epoch %4d:  Error = %f\n',epoch,TSS);
    if TSS < TSS_Limit, break, end
    clf, whitebg(gcf,[0 0 0]), hold on
    axis([0 16 -1 1])
    plot(0:NPATS-1,Desired,'o')
    plot(0:NPATS-1,Result2,'g*-')
    plot(0:NPATS-1,Result1(1,:),'mx')
    drawnow
  end
end

legend('Desired Output','Actual Output','Hidden Unit 1');
