function fjac = p06_jac ( n, x )

%*****************************************************************************80
%
%% p06_jac() sets the jacobian for problem 6.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the number of variables and equations.
%
%    real X(N), the evaluation point.
%
%  Output:
%
%    real FJAC(N,N), the jacobian matrix.
%
  fjac = zeros ( n, n );

  for i = 1 : 29

    ti = i / 29.0;

    sum1 = 0.0;
    temp = 1.0;
    for j = 2 : n
      sum1 = sum1 + ( j - 1 ) * temp * x(j);
      temp = ti * temp;
    end

    sum2 = 0.0;
    temp = 1.0;
    for j = 1 : n
      sum2 = sum2 + temp * x(j);
      temp = ti * temp;
    end

    temp1 = 2.0 * ( sum1 - sum2 * sum2 - 1.0 );
    tk = 1.0;

    for k = 1 : n
      tj = tk;
      for j = k : n
        fjac(k,j) = fjac(k,j) ...
          + tj * ( ( ( k - 1 ) / ti - 2.0 * sum2 ) ...
          * (  ( j - 1 ) / ti - 2.0 * sum2 ) - temp1 );
        tj = ti * tj;
      end
      tk = ti * ti * tk;
    end

  end

  fjac(1,1) = fjac(1,1) + 3.0 - 2.0 * x(2) + 6.0 * x(1)^2;
  fjac(1,2) = fjac(1,2) - 2.0 * x(1);

  fjac(2,1) = fjac(2,1) - 2.0 * x(1);
  fjac(2,2) = fjac(2,2) + 1.0;

  for k = 1 : n
    fjac(k:n,k) = fjac(k,k:n);
  end

  return
end

