function x_new = r83v_gs_sl ( n, a, b, c, ax, x, it_max )

%*****************************************************************************80
%
%% R83V_GS_SL solves a R83V system using Gauss-Seidel iteration.
%
%  Discussion:
%
%    The R83V storage format is used for a tridiagonal matrix.
%    The subdiagonal is in A(min(M-1,N)).
%    The diagonal is in B(min(M,N)).
%    The superdiagonal is in C(min(M,N-1)).
%
%  Example:
%
%    An R83V matrix of order 3x5 would be stored:
%
%      B1  C1  **  **  **
%      A1  B2  C2  **  **
%      **  A2  B3  C3  **
%
%    An R83 matrix of order 5x5 would be stored:
%
%      B1  C1  **  **  **
%      A1  B2  C2  **  **
%      **  A2  B3  C3  **
%      **  **  A3  B4  C4
%      **  **  **  A4  B5
%
%    An R83 matrix of order 5x3 would be stored:
%
%      B1  C1  **
%      A1  B2  C2
%      **  A2  B3
%      **  **  A3
%      **  **  **
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 February 2016
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrix.
%    N must be at least 2.
%
%    Input, real A(min(M-1,N)), B(min(M,N)), C(min(M,N-1)), the R83V matrix.
%
%    Input, real AX(N), the right hand side of the linear system.
%
%    Input, real X(N), an approximate solution to the system.
%
%    Input, integer IT_MAX, the maximum number of iterations.
%
%    Output, real X_NEW(N), an approximate solution to the system.
%

%
%  No diagonal matrix entry can be zero.
%
  for i = 1 : n
    if ( b(i) == 0.0 )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'R83V_GS_SL - Fatal error!\n' );
      fprintf ( 1, '  Zero diagonal entry, index = %d\n', i );
      error ( 'R83V_GS_SL - Fatal error!' );
    end
  end

  for it_num = 1 : it_max

    x_new(1) = ( ax(1) - c(1) * x(2)   ) / b(1);
    for i = 2 : n - 1
      x_new(i) = ( ax(i) - a(i-1) * x_new(i-1) - c(i) * x(i+1) ) ...
        / b(i);
    end
    x_new(n) = ( ax(n) - a(n-1) * x_new(n-1) ) / b(n);

    x(1:n) = x_new(1:n);

  end

  return
end
