% Script MkSparseEx.m % use spdiags to make Poisson matrix close all; n = 5; e = ones(n^2,1); % make a vector for the diagonal A = spdiags([-e -e 4*e -e -e],[-n -1 0 1 n],n^2,n^2); B = gallery('poisson',5); spy(A-B) % not quite right; need to fix up A for k = n:n:n^2-n A(k,k+1) = 0; A(k+1,k) = 0; end figure; spy(A-B) disp(' If 0, then A and B are the same: ') norm(A-B,inf) disp(' If 1, A is sparse: ') issparse(A) disp(' If 0, full(A) is not sparse: ') issparse(full(A))