function Yhat = nnet(X, W)
%NNREG Neural network estimation
%
%       NNREG(X, W) gives the neural net regression for input X and
%       parameters W.
%       X is a matrix containing a number of P-dimensional input column
%       vectors, and W is a column vector containing (P+2)Nh+1 weight
%       parameters, where Nh is the number of hidden units.
%
% See also: NNINIT, NNCOST, NNCOSTR.
if (nargin ~= 2)
  error('NNREG: wrong number of arguments.')
end

[N P] = size(X) ;  % P: dimension. N: number of vectors.
[L H] = size(W) ;
if (H ~= 1)
  error('NNREG: W should be a column vector.')
end

Nh = (L - 1) / (P + 2) ; % Nb. hidden units.

if (Nh ~= round(Nh))
  error(['NNREG: wrong number of arguments: ', num2str(Nh), ' hidden units?'])
end
W1 = reshape(W(1:((P+1)*Nh)), Nh, P+1) ;
W2 = reshape(W(((P+1)*Nh+1):L), 1, Nh+1) ;

%% The forward NN calculation.

Yhat = [ones(N,1), tanh([ones(N,1), X] * W1')] * W2' ;

% (c) 10/97, 15/01/99, CG.
