function cost = cost_function ( W2, W3, W4, b2, b3, b4, x1, x2, y )

%*****************************************************************************80
%
%% cost_function() evaluates the cost function.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    23 July 2022
%
%  Author:
%
%    Catherine Higham, Desmond Higham.
%    Modifications by John Burkardt.
%
%  Input:
%
%    real W2(), W3(), W4(): the weights.
%
%    real b2(), b3(), b4(): the shifts.
%
%    real x1(x_num), x2(x_num):
%
%    real y(y_num):
%
%  Output:
%
%    real cost: the current value of the cost function.
%
  x_num = length ( x1 );

  cost_vec = zeros ( x_num, 1 );
  for i = 1 : x_num
    x = [ x1(i); x2(i) ];
    a2 = activate ( x, W2, b2 );
    a3 = activate ( a2, W3, b3 );
    a4 = activate ( a3, W4, b4 );
    cost_vec(i) = norm ( y(:,i) - a4, 2 );
  end

  cost = norm ( cost_vec, 2 )^2;

  return
end

