function precon_demo()
for i=0:2
    precon_demo_run(500,i);
end

function precon_demo_run(k,kase)
global keisi eka A R
keisi = kase;
eka = 1;
tol=1e-5;
maxit=1500;

[A,b]=matrix5p(k);
n=size(A,1);
fprintf(1,'\n%d x %d sparse pos.def. matrix (5-point Laplace)\n',n,n);
% 
% tic
% x=pcg(A,b,tol,maxit);   % no preconditioning
% toc
tic
x=pcg(A,b,tol,maxit,@mfun);  % preconditioning matrix defined by function
toc


function y=mfun(x)
global keisi eka A R
global D L U
switch keisi
    case 0
        if eka, disp('No preconditioning:'); end
        y=x;
    case 1
        if eka
            disp('Incomplete cholesky preconditioning:')
            OPTS.type='ict';
            OPTS.droptol=0.001;
            L=ichol(A,OPTS); 
        end
        z=L\x;
        y=(L')\z;
    case 2
        if eka
            disp('SSOR preconditioning:')
            D=diag(A); D=diag(D); L=D+tril(A,-1); U=D+triu(A,1);
        end
        z=L\x;
        w=D*z;
        y=U\w;
    otherwise
        disp('tuntematon keisi');
end
eka =0;