I'm trying to do a plot of convergence of this GMRES alghorithm. I managed to create a class that make me print the residual at each iteration but I can't find a way to extract this data into an array so that i can plot it with matplotlib.
Here is my code:
matrixSize = 25
A = Atridiag(2, -1, -1, matrixSize)
A = scipy.sparse.csc_matrix (A)
b = np.matrix(np.ones((matrixSize, 1)))
x1 = np.matrix(np.ones((matrixSize, 1)))
M_i=scipy.sparse.linalg.spilu(A)
M2=scipy.sparse.linalg.LinearOperator((matrixSize,matrixSize),M_i.solve)
nmax_iter = 1
rstart = 1
tol = 1e-12
e = np.zeros((nmax_iter + 1, 1))
rr = 1
class gmres_counter(object):
def __init__(self, disp=True):
self._disp = disp
self.niter = 0
self.callbacks = []
def __call__(self, rk=None):
self.callbacks.append(str(rk))
self.niter += 1
if self._disp:
print('%s' %(str(rk)))
counter = gmres_counter()
x, info = scipy.sparse.linalg.gmres(A, b, x0=x1, tol=tol, restart=rstart,
M=M2, callback=counter)