In [11]:
from cg import cg
import numpy as np
import scipy.sparse as sparse
import matplotlib.pyplot as plt
%matplotlib inline
In [28]:
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)
In [29]:
plt.figure(figsize=(8,8))
plt.semilogy(res)
plt.semilogy(err)
Out[29]:
[<matplotlib.lines.Line2D at 0x105878240>]
In [30]:
plt.plot(x)
Out[30]:
[<matplotlib.lines.Line2D at 0x105c30908>]
In [32]:
A = A.todense()
In [44]:
#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)
In [45]:
plt.figure(figsize=(8,8))
plt.semilogy(res)
plt.semilogy(err)
Out[45]:
[<matplotlib.lines.Line2D at 0x106851978>]
In [ ]: