Chapter - 9 : Gauss Eliminations

Ex:9.1 Pg: 242

In [1]:
from __future__ import division
%matplotlib inline
from matplotlib.pyplot import plot,title,xlabel,ylabel,show
#the equations are:
#3*x1 + 2*x2=18 and -x1 + 2*x2=2

#equation 1 becomes,
#x2=-(3/2)*x1 + 9
#equation 2 becomes,
#x2=-(1/2)*x1 + 1

#plotting equation 1
x2=[0]
for x1 in range(1,7):
    x2.append(-(3/2)*x1 + 9)

x1=[1, 2, 3, 4, 5, 6]
#plotting equation 2
x4=[0]
for x3 in range(1,7):
    x4.append((1/2)*x3 + 1)

x3=[1, 2, 3, 4, 5, 6]
plot(x1,x2[1:])
plot(x3,x4[1:])
title("x2 vs x1")
xlabel("x1")
ylabel("x2")
#the lines meet at x1=4 amd x2=3
print "The lines meet at=x2=",3,"and x1=",4
The lines meet at=x2= 3 and x1= 4

Ex:9.2 Pg: 244

In [2]:
from numpy.linalg import det
from numpy import mat
#For fig9.1
a=mat([[3, 2],[-1, 2]])
print "The value of determinant for system repesented in fig 9.1 =",det(a)
#For fig9.2 (a)
a=mat([[-0.5, 1],[-0.5, 1]])
print "The value of determinant for system repesented in fig 9.2 (a) =",det(a)
#For fig9.2 (b)
a=mat([[-0.5, 1],[-1, 2]])
print "The value of determinant for system repesented in fig 9.2 (b) =",det(a)
#For fig9.2 (c)
a=mat([[-0.5, 1],[-2.3/5, 1]])
print "The value of determinant for system repesented in fig 9.2 (c) =",det(a)
The value of determinant for system repesented in fig 9.1 = 8.0
The value of determinant for system repesented in fig 9.2 (a) = 0.0
The value of determinant for system repesented in fig 9.2 (b) = 0.0
The value of determinant for system repesented in fig 9.2 (c) = -0.04

Ex:9.3 Pg: 245

In [3]:
from numpy import mat
from numpy.linalg import det
#the matrix or the system
b1=-0.01#
b2=0.67#
b3=-0.44#
a=mat([[0.3, 0.52, 1],[0.5, 1, 1.9],[0.1, 0.3, 0.5]])
a1=mat([[a[1,1], a[1,2]],[a[2,1], a[2,2]]])
A1=det(a1)
a2=mat([[a[1,0], a[1,2]],[a[2,0], a[2,2]]])
A2=det(a2)
a3=mat([[a[1,0], a[1,1]],[a[2,0], a[2,1]]])
A3=det(a3)
D=a[0,0]*A1-a[0,1]*A2+a[0,2]*A3
p=mat([[b1, 0.52, 1],[b2, 1, 1.9],[b3, 0.3, 0.5]])
q=mat([[0.3, b1, 1],[0.5, b2, 1.9],[0.1, b3, 0.5]])
r=mat([[0.3, 0.52, b1],[0.5, 1, b2],[0.1 ,0.3, b3]])
x1=det(p)/D#
x2=det(q)/D#
x3=det(r)/D#
print "The values are:"
print "x1=",x1
print "x2=",x2
print "x3=",x3
The values are:
x1= -14.9
x2= -29.5
x3= 19.8

Ex:9.4 Pg: 246

In [4]:
#the equations are:
#3*x1+2*x2=18
#-x1+2*x2=2
a11=3#
a12=2#
b1=18#
a21=-1#
a22=2#
b2=2#
x1=(b1*a22-a12*b2)/(a11*a22-a12*a21)#
x2=(b2*a11-a21*b1)/(a11*a22-a12*a21)#
print "x1=",x1
print "x2=",x2
x1= 4.0
x2= 3.0

Ex:9.5 Pg: 251

In [5]:
from numpy import arange,mat

n=3#
b=[7.85,-19.3,71.4] # ################################
a=mat([[3, -0.1, -0.2],[0.1, 7, -0.3],[0.3, -0.2, 10]])
for k in range(1,n):
    for i in range(k+1,n+1):
        fact=a[i-1,k-1]/a[k-1,k-1]
        for j in range(k+1,n+1):
            a[i-1,j-1]=a[i-1,j-1]-fact*(a[k-1,j-1])
        
        b[(i-1)]=b[(i-1)]-fact*b[(k-1)]
    
x=[0,0,b[(n-1)]/a[n-1,n-1]]
for i in arange(n-1,0,-1):
    s=b[i-1]#
    for j in range(i+1,n+1):
        s=s-a[i-1,j-1]*x[j-1]
    
    x[i-1]=b[i-1]/a[i-1,i-1]

print "x1=",x[0]
print "x2=",x[1]
print "x3=",x[2]
x1= 2.61666666667
x2= -2.79319371728
x3= 7.0

Ex:9.6 Pg:255

In [6]:
a11=1#
a12=2#
b1=10#
a21=1.1#
a22=2#
b2=10.4#
x1=(b1*a22-a12*b2)/(a11*a22-a12*a21)#
x2=(b2*a11-a21*b1)/(a11*a22-a12*a21)#
print "For the original system:"
print "x1=",x1
print "x2=",x2
a21=1.05#
x1=(b1*a22-a12*b2)/(a11*a22-a12*a21)#
x2=(b2*a11-a21*b1)/(a11*a22-a12*a21)#
print "For the new system:"
print "x1=",x1
print "x2=",x2
For the original system:
x1= 4.0
x2= 3.0
For the new system:
x1= 8.0
x2= 1.0

Ex:9.7 Pg: 257

In [7]:
from numpy.linalg import det
from numpy import mat
#part a
a=mat([[3, 2],[-1, 2]])
b1=18#
b2=2#
print "The determinant for part(a)=",det(a)
#part b
a=mat([[1, 2],[1.1, 2]])
b1=10
b2=10.4#
print "The determinant for part(b)=",det(a)
#part c
a1=a*10#
b1=100#
b2=104#
print "The determinant for part(c)=",det(a1)
The determinant for part(a)= 8.0
The determinant for part(b)= -0.2
The determinant for part(c)= -20.0

Ex:9.8 Pg: 258

In [8]:
from numpy.linalg import det
from numpy import mat
#part a
a=mat([[1, 0.667],[-0.5, 1]])
b1=6#
b2=1#
print "The determinant for part(a)=",det(a)
#part b
a=mat([[0.5, 1],[0.55, 1]])
b1=5
b2=5.2
print "The determinant for part(b)=",det(a)
#part c
b1=5#
b2=5.2#
print "The determinant for part(c)=",det(a)
The determinant for part(a)= 1.3335
The determinant for part(b)= -0.05
The determinant for part(c)= -0.05

Ex:9.9 Pg: 260

In [9]:
#0.0003*x1 + 3*x2 = 2.0001
#1*x1 + 1*x2 = 1
a11 = 0.000#
#multiplying first equation by 1/0.0003, we get, x1 + 10000*x2 = 6667
x2 = (6667-1)/(10000-1)#
x1 = 6667 - 10000*x2#
print "x2 = ",x2
print "x1 = ",x1
print "The error varies depending on the no. of significant figures used"
x2 =  0.666666666667
x1 =  0.333333333334
The error varies depending on the no. of significant figures used

Ex:9.10 Pg: 262

In [10]:
from __future__ import division
#2*x1 + 10000*x2 = 10000
#x1 + x2 = 2
x1 = 1#
x2 = 1#
print "without scaling, applying forward elimination"
#x1 is too small and can be neglected
x21 = 10000/10000#
x11 = 0#
e1 = (x1 - x11)*100/x1#
print "x2 = ",x21
print "x1 = ",x11
print "error for x1 = ",e1
print "with scaling"
#0.00002*x1 + x2 = 1
#now x1 is neglected because of the co efficient
x22 = 1#
x12 = 2 - x1#
print "x1 = ",x12
print "x2 = ",x22
#using original co efficient
#x1 can be neglected
print "pivot and retaining original coefficients"
x22 = 10000/10000#
x12 = 2 - x1#
print "x1 = ",x12
print "x2 = ",x22
without scaling, applying forward elimination
x2 =  1.0
x1 =  0
error for x1 =  100.0
with scaling
x1 =  1
x2 =  1
pivot and retaining original coefficients
x1 =  1
x2 =  1.0

Ex:9.11 Pg: 265

In [11]:
from __future__ import division
from numpy import mat
from numpy.linalg import solve
a=mat([[70, 1, 0],[60, -1, 1],[40, 0, -1]])
b=mat([[636],[518],[307]])
x=abs(solve(a,b))
print "a=",x[0,0],"m/s**2"
print "T=",x[1,0],"N"
print "R=",x[2,0],"N"
a= 8.59411764706 m/s**2
T= 34.4117647059 N
R= 36.7647058824 N

Ex:9.12 Pg: 269

In [12]:
from __future__ import division
from numpy import mat,vstack
from numpy.linalg import det
#3*x1 - 0.1*x2 - 0.2*x3 = 7.85
#0.1*x1 + 7*x2 - 0.3*x3 = -19.3
#0.3*x1 - 0.2*x2 + 10*x3 = 71.4
# this can be written in matrix form as
A = mat([[3,-0.1,-0.2,7.85],[0.1,7,-0.3,-19.3],[0.3,-0.2,10,71.4]])
print "Equation in matrix form can be written as : \n",A
X = A[0:1] / (A[0,0])#
Y = A[1:2] - 0.1*X#
Z = A[2:3] - 0.3*X#

Y = Y/(Y[0,1])
X = X - Y * (X[0,1])
Z = Z - Y * (Z[0,1])#
Z = Z/(Z[0,2])#
X = X - Z*(X[0,2])#
Y = Y - Z*(Y[0,2])#
A = vstack((X,Y,Z))
print "final matrix = \n",A
print "\nx1 = ",A[0,3]
print "x2 = ",A[1,3]
print "x3 = ",A[2,3]
Equation in matrix form can be written as : 
[[  3.    -0.1   -0.2    7.85]
 [  0.1    7.    -0.3  -19.3 ]
 [  0.3   -0.2   10.    71.4 ]]
final matrix = 
[[ 1.   0.   0.   3. ]
 [ 0.   1.   0.  -2.5]
 [ 0.   0.   1.   7. ]]

x1 =  3.0
x2 =  -2.5
x3 =  7.0