function [W, Ythat] = nnexp(X, Y, Xt, Yt, L)
%
%       NNEXP(X, Y, XT, YT, L):
%       - X and Y are the training input and output,
%       - XT and YT are the test input and output,
%       - L is the regularisation.
%
if (nargin < 5)
   L = 1e-6 ;
end

Nhidden = 15 ;

[N, P] = size(X) ;

W = nninit(P, Nhidden) ;
Wist = W ;
iter = 30 ;
Etrain = [] ;
Etest = [] ;

figure(1), clf,
figure(2), clf,

while iter > 10
  [W, fW, iter] = conjgrad(W, 'nncostr', 30, X, Y, L) ;
  Wist = [Wist, W] ;
  Yhat = nnreg(X, W) ;
  Etrain = [Etrain, mean((Yhat - Y).^2)] ;
  Ythat = nnreg(Xt, W) ;
  Etest = [Etest, mean((Ythat - Yt).^2)] ;
  figure(1),
  plot(Etrain, '-') ;
  hold on ;
  plot(Etest, '--') ;
  title('Training and test error') ;
  figure(2),
  plot(abs(Wist)') ;
  title('Network weights') ;
end
