In [77]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [78]:
A = np.random.rand(6,6)
A = A.T.dot(A)
In [79]:
print("%g" % np.linalg.cond(A))
13695.1
In [80]:
D = np.diag(np.arange(1,A.shape[0]+1,dtype=float))
print(D)
[[ 1.  0.  0.  0.  0.  0.]
 [ 0.  2.  0.  0.  0.  0.]
 [ 0.  0.  3.  0.  0.  0.]
 [ 0.  0.  0.  4.  0.  0.]
 [ 0.  0.  0.  0.  5.  0.]
 [ 0.  0.  0.  0.  0.  6.]]
In [81]:
plt.spy(D)
Out[81]:
<matplotlib.image.AxesImage at 0x10b6f8940>
In [82]:
print(np.linalg.cond(D))
6.0
In [83]:
D[0,0] = 0.001
print(np.linalg.cond(D))
6000.0
In [87]:
x = np.ones((A.shape[0],))

B = A
for i in range(0,4):
    b = B.dot(x)
    xsolved = np.linalg.solve(B, b)
    maxdiff = np.abs(xsolved - x).max()
    print("cond: %20e     max error: %20e" % (np.linalg.cond(B), maxdiff))
    
    B = A.dot(B)
cond:         1.369511e+04     max error:         3.217426e-13
cond:         1.875560e+08     max error:         3.398965e-08
cond:         2.568664e+12     max error:         1.729675e-04
cond:         3.875178e+16     max error:         1.853222e+01
In [ ]: