error in CG with Preconditioner
I tried to run Example 8.8.3 from the book T. A. Driscoll, R. J. Braun Fundamentals of Numerical Computation Julia Edition. Whereas cg without preconditioner seems to work, I receive an error message when using cg with a preconditioner.

using MAT, LinearAlgebra, SparseArrays, IterativeSolvers, Preconditioners
file = matopen("A.mat");
A = read(file,"A");
close(file);
n = size(A,1);
@show n,nnz(A)
b = ones(n);
M = DiagonalPreconditioner(diag(A));
plain(b) = cg(A,b,maxiter=200,reltol=1.e-4,log=true)
time_plain = @elapsed x,hist1 = plain(b)
prec(b) = cg(A,b,P1=M,maxiter=200,reltol=1.e-4,log=true)
time_prec = @elapsed x,hist2 = prec(b)
@show time_plain, time_prec
rr = hist1[:resnorm]
plot(0:length(rr),rr/rr[1],yscale=:log10,label="plain")
rr = hist2[:resnorm]
plot!(0:length(rr),rr/rr[1],yscale=:log10,label="preconditioned")
title!("Diagonal preconditioning in CG")

关闭于 2023-02-20 1 条评论