In [1]:
import numpy as np
import scipy as sp
import scipy.sparse as sparse
import scipy.sparse.linalg as sla
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
n = 100
A = sparse.diags([-1, 2, -1], [-1, 0, 1], shape=(n,n), format='csr')
b = np.zeros((n,))
In [3]:
I = sparse.eye(n, format='csr')
Dinv = 0.5 * I
D = 2 * I
E = -sparse.tril(A, -1)
In [6]:
omega = 2.0/3.0
rnorm = []
monitor = True
x = np.random.rand(n)
for i in range(100):
    x[:] = x - omega * Dinv * A * x
    #x[:] = x - sla.spsolve(D-E, A*x)
    rnorm.append(np.linalg.norm(A * x))

    if monitor:
        plt.ion()
        plt.figure(1)
        plt.plot(x)
        plt.draw()
In [7]:
plt.plot(x)
Out[7]:
[<matplotlib.lines.Line2D at 0x109a797b8>]
In [8]:
np.linalg.norm(x - (x - omega * Dinv * A * x))
Out[8]:
0.0073584960707146051
In [ ]: