% Script: pcgcond_example.m % adds poisson and wathen matrices to matrices with % controlled condition numbers and plots iteration progress n = 600; b = rand(n,1); b = b/norm(b); kappa = [2 10 100 1000]; % these will be condition numbers for k=1:4 d = linspace(1,kappa(k),n)'; A = spdiags(d,0,n,n); [x,flag,err,m,normr] = pcg(A,b,1e-14,500); semilogy( 0:length(normr)-1, normr ); hold on; iter(k) = m; end hold on n = 25; A = n^2*gallery('poisson',n); b = -ones(n^2,1); [x,flag,err,m,normr] = pcg(A,b,1e-14,500); semilogy( 0:length(normr)-1, normr, '--'); condest(A) % for a sparse matrix hold on; A = gallery('wathen',n,n); A = length(A)*A; b = -ones(length(A),1); [x,flag,err,m,normr] = pcg(A,b,1e-14,500); semilogy( 0:length(normr)-1, normr, '-.' ); condest(A) % for a sparse matrix