function [ nz, row, col, val ] = r8crs_dif2 ( n )

%*****************************************************************************80
%
%% r8crs_dif2() sets up an R8CRS second difference matrix.
%
%  Discussion:
%
%    The R8CRS storage format stores the nonzero entries of row I in 
%    entries ROW(I) through ROW(I+1)-1 of VAL.  
%    COL(J) records the column index of the entry in VAL(J).
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    14 Febuary 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the order of the matrix.
%
%  Output:
%
%    integer NZ, the number of nonzero elements.
%
%    integer ROW(N+1).  The nonzero offdiagonal elements 
%    of row I of A are contained in A(ROW(I)) through A(ROW(I+1)-1).
%
%    integer COL(NZ), the column indices of the nonzero elements of A.
%
%    real VAL(NZ), the nonzero elements of A.
%
  nz = 3 * n - 2;

  row = zeros ( n + 1, 1 );
  col = zeros ( nz, 1 );
  val = zeros ( nz, 1 );

  row(1) = 1;
  k = 0;

  for i = 1 : n

    if ( 1 < i )
      k = k + 1;
      col(k) = i - 1;
      val(k) = -1.0;
    end

    k = k + 1;
    col(k) = i;
    val(k) = 2.0;

    if ( i < n )
      k = k + 1;
      col(k) = i + 1;
      val(k) = -1.0;
    end

    row(i+1) = k + 1;

  end

  return
end

