function [ c, it ] = gradient_descent_data_fitting ( xdata, ydata, it_max, ...
  alpha, tol )

%*****************************************************************************80
%
%% gradient_descent_data_fitting() solves a data fitting problem.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 July 2022
%
%  Author:
%
%    John Burkardt.
%
%  Reference:
%
%    James Howard,
%    Computational Methods for Numerical Analysis with R,
%    CRC Press, 2017,
%    ISBN13: 978-1-4987-2363-3.
%
%  Input:
%
%    real XDATA(M,N): the independent data.
%
%    real YDATA(M): the dependent data.
%
%    integer IT_MAX: the maximum number of steps.
%
%    real ALPHA: the stepsize.
%
%    real TOL: if the norm of the difference in two successive solutions
%    is less than TOL, the iteration terminates early.
%
%  Output:
%
%    real C(N+1): the intercept in C(1), and coefficients in C(2:N+1).
%
%    integer IT: the number of iterations taken.
%
  [ m, n ] = size ( xdata );

  A = zeros ( m, n + 1 );
  A(1:m,1) = 1.0;
  A(1:m,2:n+1) = xdata(1:m,1:n);

  c = rand ( n + 1, 1 )

  for it = 1 : it_max

    e = A * c - ydata;
    d = A' * e;
    cold = c;
    c = c - alpha * d

    norm ( c - cold )
    if ( norm ( c - cold ) < tol )
      break;
    end

  end

  return
end

