function A = SparseMatrixExample(n)
% generate an example of a sparse matrix A 
% of dimension n^2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

h=1;    
      
bdry_x_ind = [[1:n]'; n*ones(n-2,1); [n:-1:1]'; ones(n-2,1)];
bdry_y_ind = [ones(n,1); [2:n]'; n*ones(n-1,1); [n-1:-1:2]']; 
      
bdry = sub2ind([n,n],bdry_x_ind,bdry_y_ind);
      
% the main diagonal
main_diag = ones(n*n,1);
main_diag(bdry) = 0; % zeros on the diagonal
      
% diagonal corresponding to x_{i+1}
% dummy value at the beginning
up_x_diag = [NaN; main_diag(1:n*n-1)];
      
% diagonal corresponding to x_{i-1}
% dummy value at the end
low_x_diag = [main_diag(2:n*n);NaN];
      
% diagonal corresponding to y_{j+1}
% dummy values at the beginning
up_y_diag = [NaN(n,1); main_diag(1:n*n-n)];      
            
% diagonal corresponding to y_{j-1}
% dummy values at the end
low_y_diag = [main_diag(n+1:n*n); NaN(n,1)];      
           
% put everything together
A = spdiags([low_y_diag low_x_diag -4*main_diag up_x_diag up_y_diag],...
             [-n -1 0 1 n],n*n,n*n); 
     
% add ones on the boundary
bdry_diag = zeros(n*n,1);
bdry_diag(bdry) = 1;
      
A = A + spdiags(bdry_diag,0,n*n,n*n);
	
A = sparse(A); % reoptimise the memory      	  
    
