from cg import cg
import numpy as np
import scipy.sparse as sparse
import matplotlib.pyplot as plt
%matplotlib inline
its = 78
n = 100
A = sparse.diags([-1, 2, -1], [-1, 0, 1], shape=(n,n), format='csr')
b = np.zeros((n,))
b = np.zeros((n,))
x0 = np.random.rand(n)
res = []
err = []
x, returncode = cg(A, b, x0=x0, maxiter=its, residuals=res, errs=err)
plt.figure(figsize=(8,8))
plt.semilogy(res)
plt.semilogy(err)
plt.plot(x)
A = A.todense()
#A[0,1] = 1.0
#A[0,1] = 0.000001
A[0,1] = 0
A[1,0] = 0
A[0,0] = 2
res = []
err = []
x, returncode = cg(A, b, x0=x0, maxiter=its, residuals=res, errs=err)
plt.figure(figsize=(8,8))
plt.semilogy(res)
plt.semilogy(err)