 %  fap2dLM.m
 %            28 March 2005
 % Approximation of two 2D functions using the Levenberg-Marquardt
 %             algorithm
 clear
 % Generation of training points
 na = 16 ;  N = na^2; nn = 0:na-1;
 X1 = nn*4/na - 2;
 [X1 X2] = meshgrid(X1);
 R  = -(X1.^2 + X2.^2 +0.00001);
 D1 = X1 .* exp(R);  D = (D1(:))';
 D2 = 0.25*sin(2*R)./R ;  D = [D ; (D2(:))'];
 Y = zeros(size(D)) ;
 X = [ X1(:)'; X2(:)'];
 figure(1), % myfigA(1, 11, 10)
 surfc([X1-2 X1+2], [X2 X2], [D1 D2]),
 title('Two 2-D target functions'), 
 grid on, drawnow
 % print(1, '-depsc2', 'p2LM1')
 
 % network specification
 p = 2 ; % Number of inputs excluding bias 
 L = 12; % Number of hidden signals excluding bias
 m = 2 ; % Number of outputs
 MnMx = [-2 2; -2 2] ;
    Nnet = newff(MnMx, [L, m]) ; % Network creation
    Nnet.trainParam.epochs = 50;
    Nnet.trainParam.goal = 0.0016;
    Nnet = train(Nnet, X, D) ;  % Training
  grid on, hf = gcf ;
  set(hf,  'PaperUnits', 'centimeters', 'PaperPosition', [0 0 10 11] );
  % print(hf, '-depsc2', 'p2LM3')
    Y = sim(Nnet , X) ;         % Validation
 D1(:)=Y(1,:)'; D2(:)=Y(2,:)';
 % Two 2-D approximated functions are plotted side by side
 figure(2), % myfigA(2, 11, 10)
 surfc([X1-2 X1+2], [X2 X2], [D1 D2]) 
 title('function approximation') 
 grid on, drawnow
 % print(2, '-depsc2', 'p2LM2')
