% function fap2D
%  fap2D.m       main loop
%         25 March 1999
% run  fap2Di  to initalise
% Example of approximation of two 2-dimensional functions
% using backpropagation in a batch mode
%
% ---------------,
% Andrew P. Paplinski,      app@dgs.monash.edu.au
% Comp.Sci.& Soft.Eng., http://www.dgs.monash.edu.au/~app/
% Monash University, Clayton 3168, [Melbourne,] AUSTRALIA 

% **********************  main loop *********************
figure(2), clf reset, hold off
tic
for c = 1:C  % the epoch loop

 % The forward pass
 % Hidden signals  (L by N) with appended bias signals
 H = ones(L-1, N)./(1+exp(-Wh*X));	
 Hp = H.*(1-H);       % Derivatives of hidden signals
 H = [H ; ones(1,N)] ;
 Y = tanh(Wy*H)  ;    % Output signals        (m by N)
 Yp = 1 - Y.^2 ;	  % Derivatives of output signals
                                      
 % The backward pass

 Ey = D - Y;		     % The output errors      (m by N)
 JJ = (sum((Ey.*Ey)'))'; % The total error after one epoch
 J(:,c) = JJ ;           % the performance function  m by 1
 delY = Ey.*Yp;			 % Output delta signal    (m by N)
 dWy = delY*H';			 % Update of the output matrix
                         % dWy  is  m by L 
 Eh = Wy(:,1:L-1)'*delY; % The backpropagated hidden error
                         % Eh  is  L-1 by N 
 delH = Eh.*Hp ;         % Hidden delta signals  (L-1 by N)
 dWh = delH*X';			 % Update of the hidden matrix
                         % dWh  is  L-1 by  p                       
 % The batch update of the weights:
 Wy = Wy+eta(1)*dWy ;  Wh = Wh+eta(2)*dWh ;

 D1(:)=Y(1,:)'; D2(:)=Y(2,:)';

 % Two 2-D aproximated functions are ploted side by side
 surfc([X1-2 X1+2], [X2 X2], [D1 D2]), grid on, ...
 title(['epoch: ', num2str(c), ', error: ', num2str(JJ','%4.3f  '), ...
       ' eta: ', num2str(eta,'%4.3f  ')]), drawnow
end  % of the epoch loop
toc

figure(3), clf reset
plot(J'), grid
title('The approximation error')
xlabel('number of training epochs')

% print -depsc2 -f3 dFap2De

