function x = r83t_jac_sl ( n, a, b, x, it_max )

%*****************************************************************************80
%
%% R83T_JAC_SL solves an R83T system using Jacobi iteration.
%
%  Discussion:
%
%    The R83T storage format is used for an MxN tridiagonal matrix.
%    The superdiagonal is stored in entries (1:M-1,3), the diagonal in
%    entries (1:M,2), and the subdiagonal in (2:M,1).  Thus, the
%    the rows of the original matrix slide horizontally to form an
%    Mx3 stack of data.
%
%    An R83T matrix of order 3x5 would be stored:
%
%       *  A11 A12
%      A21 A22 A23
%      A32 A33 A34
%
%    An R83T matrix of order 5x5 would be stored:
%
%       *  A11 A12
%      A21 A22 A23
%      A32 A33 A34
%      A43 A44 A45
%      A54 A55  *
%
%    An R83T matrix of order 5x3 would be stored:
%
%       *  A11 A12
%      A21 A22 A23
%      A32 A33  *
%      A43  *   *
%       *   *   *
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    28 May 2016
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrix.
%    N must be at least 2.
%
%    Input, real A(N,3), the R83T matrix.
%
%    Input, real B(N), the right hand side of the linear system.
%
%    Input/output, real X(N), an approximate solution 
%    to the system.
%
%    Input, integer IT_MAX, the maximum number of iterations.
%

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

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

  for it_num = 1 : it_max

    x_new(1) = b(1) - a(1,3) * x(2);
    for i = 2 : n - 1
      x_new(i) = b(i) - a(i,1) * x(i-1) - a(i,3) * x(i+1);
    end
    x_new(n) = b(n) - a(n,1) * x(n-1);
%
%  Divide by diagonal terms.
%
    x_new(1:n) = x_new(1:n) ./ a(1:n,2);
%
%  Update.
%
    x(1:n) = x_new(1:n);

  end

  return
end

