function [f,fg] = mlp_cgd(v,k,n1,x,y)
%
%  Calculates the value of the cost functional J and the
%  gradient-matrices w.r.t. the unknown weights of the MLP-network.
%
%  Input
%    v         entries of the weight matrices W_1 and W_2 
%              as one long vector. Matrix W_1 comes first and then W_2.
%    k         vector of parameters for sigmoid
%    n1        dimension of the hidden layer
%    x         input-vectors of the training data; 
%              one column is one vector
%    y         output-vectors of the training data
%
%  Output
%    f         value of the cost functional
%    fg        gradient vector
%

[n0,NX] = size(x);
[n2,NY] = size(y);

if (NX~=NY)
   error('training inputs and outputs incompatible.');
end 

N = NX;
w1_dim = n1*(n0+1);
w2_dim = n2*(n1+1);
if (length(v)~=w1_dim+w2_dim)
   error('training data and weight matrices incompatible.');
end

%
% Reshape the weight matrices from the vector v
%
w1 = reshape(v(1:w1_dim),n1,n0+1);
w2 = reshape(v(w1_dim+1:w1_dim+w2_dim),n2,n1+1);

cof = 1/N;
f = 0;
dw1 = zeros(size(w1));
dw2 = zeros(size(w2));
for i=1:N
   [o,o1,d1] = mlp_out(x(:,i),k,w1,w2);
   e = o - y(:,i);
   f = f + 0.5*cof*e'*e;
   dw1 = dw1 + cof*diag(d1)*w2(:,2:n1+1)'*e*[1 x(:,i)'];
   dw2 = dw2 + cof*e*[1 o1'];
end

% reshape the two gradient matrices into one long vector
fg = [ reshape(dw1,w1_dim,1) ; reshape(dw2,w2_dim,1) ];





