function precon_demo2()
for i=0:2
    run_precon_demo2(100,i);
end

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

[A,b]=matrix5p(k,1e3);
n=size(A,1);
fprintf(1,'\n%d x %d sparse indefinite matrix (5-point Helmholtz)\n',n,n);

restart = 200;   % size of Krylov subspace 

tic
x = gmres(A,b,restart,tol,maxit,@mfun);
toc


function y=mfun(x)
global keisi eka A 
global D L U P
switch keisi
    case 0
        if eka
            fprintf(1,'\n*** No preconditioning:\n');
        end
        y=x;
    case 1
        if eka
            fprintf(1,'\n*** Incomplete LU preconditioning:\n')
            SETUP.type='nofill';
            [L,U,P]=ilu(A,SETUP);  % no fill-in allowed
        end
        z=L\(P*x);
        y=U\z;
    case 2
        if eka
            fprintf(1,'\n*** SSOR preconditioning:\n')
            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
        error('unknown preconditioner...');
end

eka =0;