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

%*****************************************************************************80
%
%% R83S_JAC_SL solves an R83S system using Jacobi iteration.
%
%  Discussion:
%
%    The R83S storage format is used for a tridiagonal scalar matrix.
%    The vector A(3) contains the subdiagonal, diagonal, and superdiagonal
%    values that occur on every row.
%    RGE A(I,J) = R83S A(I-J+2).
%
%    This routine simply applies a given number of steps of the
%    iteration to an input approximate solution.  On first call, you can
%    simply pass in the zero vector as an approximate solution.  If
%    the returned value is not acceptable, you may call again, using
%    it as the starting point for additional iterations.
%
%  Example:
%
%    Here is how an R83S matrix of order 5, stored as (A1,A2,A3), would
%    be interpreted:
%
%      A2  A1   0   0   0
%      A3  A2  A1   0   0
%       0  A3  A2  A1   0 
%       0   0  A3  A2  A1
%       0   0   0  A3  A2
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    05 September 2015
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of the matrix.
%
%    Input, real A(3), the R83S matrix.
%
%    Input, real B(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(N), an approximate solution 
%    to the system.
%

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

  for it_num = 1 : it_max

    it = it_num;

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

  end

  return
end