function value = watson_f ( x, n )

%*****************************************************************************80
%
%% watson_f() evaluates the Watson function.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 July 2016
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X(N), the evaluation point.
%
%    integer N, the number of variables.
%
%  Output:
%
%    real VALUE, the function value.
%
  value = 0.0;

  for i = 1 : 29

    s1 = 0.0;
    d = 1.0;
    for j = 2 : n
      s1 = s1 + ( j - 1 ) * d * x(j);
      d = d * i / 29.0;
    end

    s2 = 0.0;
    d = 1.0;
    for j = 1 : n
      s2 = s2 + d * x(j);
      d = d * i / 29.0;
    end

    value = value + ( s1 - s2 * s2 - 1.0 ) ^ 2;

  end

  value = value + x(1) ^ 2 + ( x(2) - x(1) ^ 2 - 1.0 ) ^ 2;

  return
end
