function delta=backward(net, ix, y)
%BACKWARD	Feed back, get delta.
%
%	y = BACKWARD (net, ix, y);
%
%	Inputs:
%	  ix: internal status from forward
%	  y : expected output. each row is an output.
%	Outputs:
%	  delta: dE/d(sum)
%
%	See also: NEWNET, INITNET, FORWARD, EPOCH.

%	Ling Li, Caltech
%	10/21/96, 10/08/00

% Check inputs
lm = net.layers;
if size(y, 2) ~= net.neurons(lm)
	error('Input matrix dimension not agree');
end

% delta{l}: row vector of length net.neurons(l+1)
delta = cell(1, lm - 1);
delta{lm - 1} = 2 * (ix{lm} - y); % .* theta'(s{lm-1})

% backward loop
for l = lm-1:-1:2
	tmpdel = delta{l} * net.weight{l}';
	delta{l-1} = tmpdel(:, 2:net.neurons(l)+1) .* (1 - ix{l}.^2);
end

