Getting the intermediate solutions in an iterative solver

3 Ansichten (letzte 30 Tage)
Nathan
Nathan am 16 Jul. 2017
Beantwortet: Josh Meyer am 23 Sep. 2019
I would like Matlab's iterative solvers to return the solution every 100 or so iterations. IS there a way to do this?

Antworten (2)

Walter Roberson
Walter Roberson am 16 Jul. 2017
Sorry, that is not possible for qmr or pcg.

Josh Meyer
Josh Meyer am 23 Sep. 2019
While the functions themselves don't return intermediate results, consider that the resvec output of all the iterative solvers returns the history of the relative residual, which tells you how good all of the intermediate solutions were:
[x,flag,relres,iter,resvec] = pcg(A,b);
If that doesn't satisfy your needs, you can get the intermediate results programmatically by calling PCG in a for-loop. Each call to PCG does a few iterations and stores the calculated solution, then you use that solution as the initial vector for the next iteration. For example, this code performs 100 iterations ten times and returns the solution vector every 100 iterations in a cell array:
A = sprand(1000,1000,0.1);
A = A'*A;
b = full(sum(A,2));
x0 = zeros(size(A,2),1);
tol = 1e-8;
maxit = 100;
for k = 1:10
[x,flag,relres] = pcg(A,b,tol,maxit,[],[],x0);
X{k} = x;
R(k) = relres;
x0 = x;
end
After you run this, X{k} is the solution vector computed at iteration k, and R(k) is the relative residual of that solution. (The coefficient matrix in this example is not interesting and was just chosen to make the code run)

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by