# Chapter - 11 : Special Matrices and Gauss-Seidel¶

## Ex:11.1 Pg: 297¶

In [1]:
from numpy import mat
A = mat([[2.04,-1,0,0],[-1,2.04,-1,0],[0,-1,2.04,-1],[0,0,-1,2.04]])
B = mat([[40.8],[0.8],[0.8],[200.8]])
g = A[0,1]
f1 =A[0,0]
e2 = A[1,0]
f2 = A[0,0] - e2 * A[1,0]
e3 = A[1,0]/f2
f3 = A[0,0] - e3 * A[1,0]
e4 = A[1,0]/f3#
f4 = A[0,0] - e4 * A[1,0]
M = mat([[f1,g,0,0],[e2,f2,g,0],[0,e3,f3,g],[0,0,e4,f4]])
L = mat([[1,0,0,0],[M[1,0],1,0,0],[0,M[2,1],1,0],[0,0,M[3,2],1]])
U = mat([[M[0,0],g,0,0],[0,M[1,1],g,0],[0,0,M[2,2],g],[0,0,0,M[3,3]]])
r1 = B[0,0]
r2 = B[1,0] - e2*B[0,0]
r3 = B[2,0] - e3*r2#
r4= B[3,0] - e4*r3#
N = mat([[r1],[r2],[r3],[r4]])
T4 = r4/U[3,3]
T3 = (r3 - g*T4)/U[2,2]
T2 = (r2 - g*T3)/U[1,1]
T1 = (r1 - g*T2)/U[0,0]
print "T1 = ",T1
print "T2 = ",T2
print "T3 = ",T3
print "T4 = ",T4

T1 =  151.165722019
T2 =  267.578072919
T3 =  236.681195836
T4 =  214.451566586


## Ex:11.2 Pg: 299¶

In [2]:
from numpy import mat,sqrt
A = mat([[6,15,55],[15,55,225],[55,225,979]])
sl = 0
l11 = sqrt(A[0,0])
#for second row
l21 = (A[1,0])/l11
l22 = (A[1,1] - l21**2)**(0.5)
#for third row
l31 = (A[2,0])/l11#
l32 = (A[2,1] - l21*l31)/l22#
l33 = (A[2,2] - l31**2 - l32**2)**(0.5)#
L = mat([[l11,0,0],[l21,l22,0],[l31,l32,l33]])
print "\nL = \n",L

L =
[[  2.44948974   0.           0.        ]
[  6.12372436   4.18330013   0.        ]
[ 22.45365598  20.91650066   6.11010093]]


## Ex:11.3 Pg: 301¶

In [11]:
#3x - 0.1y - 0.2z = 7.85
#0.1x + 7y - 0.3z = -19.3
#0.3x - 0.2y + 10z = 71.4
Y = 0#
Z = 0#
x=range(1,3)
y=range(1,3)
z=range(1,3)
for i in range(1,3):
x[i-1]= (7.85 +0.1*Y+0.2*Z)/3#
X = x[i-1]
y[i-1]= (-19.3 - 0.1*X +0.3*Z)/7#
Y = y[i-1]#
z[i-1]= (71.4 - 0.3*X+0.2*Y)/10#
Z = z[i-1]
if i==2:
ex = (x[i-1] - x[(i-2)])*100/x[i-1]
ey = (y[i-1] - y[i-2])*100/y[i-1]
ez = (z[i-1] - z[i-2])*100/z[i-1]

print "x through two iterations =",x[0:1]
print "y through two iterations =",y[0:1]
print "z through two iterations =",z[0:1]
print "error of x = ",ex,"%"
print "error of y = ",ey,"%"
print "error of z = ",ez,"%"

x through two iterations = [2.6166666666666667]
y through two iterations = [-2.7945238095238096]
z through two iterations = [7.005609523809525]
error of x =  12.50234999 %
error of y =  -11.7977361365 %
error of z =  -0.075978454143 %


## Ex:11.4 Pg: 307¶

In [15]:
from __future__ import division

from numpy import mat
A = mat([[1,0.5,1/3],[1,2/3,1/2],[1,3/4,3/5]])
B = mat([[1.833333],[2.166667],[2.35]])
U = A**-1
X = U*B#
x = X[0,0]
y = X[1,0]
z = X[2,0]
print "x = ",x
print "y = ",y
print "z = ",z

x =  0.999991
y =  1.000044
z =  0.99996


## Ex:11.5 Pg: 309¶

In [16]:
from __future__ import division
from numpy import mat
A = mat([[1,0.5,1/3],[1,2/3,1/2],[1,3/4,3/5]])
B = mat([[1.833333],[2.166667],[2.35]])
U = A**-1
X = U*B#
x = X[0,0]
y = X[1,0]
z = X[2,0]
print "x = ",x
print "y = ",y
print "z = ",z

x =  0.999991
y =  1.000044
z =  0.99996


## Ex:11.6 Pg: 310¶

In [18]:
from numpy import mat
A = mat([[1,0.5,1/3],[1/2,1/3,1/4],[1/3,1/4,1/5]])
B = mat([[1.833333],[1.083333],[0.783333]])
U = A**-1
X = U*B#
x = X[0,0]
y = X[1,0]
z = X[2,0]
print "x = ",x
print "y = ",y
print "z = ",z

x =  0.999999
y =  1.000008
z =  0.99999