Chapter 1 - Linear Algebraic Equations

Exa1.1 Page 3

In [1]:
from numpy import zeros
from __future__ import division
a=[0];b=[];c=[]
for i in range(1,7):
    a.append(1)            #sub diagonal assignment

for j in range(0,7):
    b.append(-2)            #main diagonal assignment

for k in range(0,6):
    c.append(1)            #super diagonal assignment

d=[-240]                     #given values assignment
for l in range(1,6):
    d.append(-40) 

d.append(-60)
i=1#
n=7#
beta1=[b[i-1]]#                #initial b is equal to beta since a1=0
gamma1=[d[i-1]/beta1[i-1]]#      #since c7=0
m=i+1
for j in range(m,n+1):
    beta1.append(b[j-1]-a[j-1]*c[j-1-1]/beta1[j-1-1])
    gamma1.append((d[j-1]-a[j-1]*gamma1[j-1-1])/beta1[j-1])

#x(n)=gamma1(n)#               #since c7=0
x=zeros(n-1)
#x[n-1]=gamma1[n-1]
x=list(x)
x.append(gamma1[-1])
n1=n-i# 

for k in range(0,n1):
    j=n-k-1
    x[j-1]=gamma1[j-1]-c[j-1]*x[j+1-1]/beta1[j-1]


print "the solution of ex 1.1 by TDMA method is"
for i in range(0,7):
    print x[i]
the solution of ex 1.1 by TDMA method is
317.5
395.0
432.5
430.0
387.5
305.0
182.5

Exa1.2 Page 6

In [2]:
from __future__ import division
a1=10; a2=1; a3=2;              #1st row
b1=2; b2=10; b3=1;              #2nd row
c1=1; c2=2; c3=10;              #3rd row   
d1=44; d2=51; d3=61;            #given values

b3=b3-(b1/a1)*a3                # for making b1=0
b2=b2-(b1/a1)*a2
d2=d2-(b1/a1)*d1
b1=b1-(b1/a1)*a1

c3=c3-(c1/a1)*a3               # for making c1=0
c2=c2-(c1/a1)*a2
d3=d3-(c1/a1)*d1
c1=c1-(c1/a1)*a1

c3=c3-(c2/b2)*b3              # for making c2=0
d3=d3-(c2/b2)*d2
c2=c2-(c2/b2)*b2

x3=d3/c3#                      # final values of x
x2=(d2-(b3*x3))/b2#
x1=(d1-(x3*a3)-(x2*a2))/a1#
print "the solution using gauss elimination method is %d, %d and %d"%(x1,x2,x3)
the solution using gauss elimination method is 3, 4 and 5

Exa1.3 Page 7

In [3]:
from __future__ import division
a1=3; a2=1; a3=-2;              #1st row
b1=-1; b2=4; b3=-3;              #2nd row
c1=1; c2=-1; c3=4;             #3rd row   
d1=9; d2=-8; d3=1;            #given values

b3=b3-(b1/a1)*a3                # for making b1=0
b2=b2-(b1/a1)*a2
d2=d2-(b1/a1)*d1
b1=b1-(b1/a1)*a1

c3=c3-(c1/a1)*a3               # for making c1=0
c2=c2-(c1/a1)*a2
d3=d3-(c1/a1)*d1
c1=c1-(c1/a1)*a1

c3=c3-(c2/b2)*b3              # for making c2=0
d3=d3-(c2/b2)*d2
c2=c2-(c2/b2)*b2

x3=d3/c3#                      # final values of x
x2=(d2-(b3*x3))/b2#
x1=(d1-(x3*a3)-(x2*a2))/a1#
print "the solution using gauss elimination method is %d, %d and %d"%(x1,x2,x3)
the solution using gauss elimination method is 3, -2 and 0

Exa1.4 Page 8

In [4]:
from __future__ import division

a1=.35; a2=.16; a3=.21; a4=.01              #1st row 
b1=.54; b2=.42; b3=.54; b4=.1              #2nd row
c1=.04; c2=.24; c3=.1; c4=.65              #3rd row
d1=.07; d2=.18; d3=.15; d4=.24             #4th row   
r1=14; r2=28; r3=17.5; r4=10.5            #given values

b4=b4-(b1/a1)*a4                          # for making b1=0
b3=b3-(b1/a1)*a3
b2=b2-(b1/a1)*a2
r2=r2-(b1/a1)*r1
b1=b1-(b1/a1)*a1

c4=c4-(c1/a1)*a4                         # for making c1=0
c3=c3-(c1/a1)*a3
c2=c2-(c1/a1)*a2
r3=r3-(c1/a1)*r1
c1=c1-(c1/a1)*a1

d4=d4-(d1/a1)*a4                         # for making d1=0
d3=d3-(d1/a1)*a3
d2=d2-(d1/a1)*a2
r4=r4-(d1/a1)*r1
d1=d1-(d1/a1)*a1

c4=c4-(c2/b2)*b4                        # for making c2=0
c3=c3-(c2/b2)*b3
r3=r3-(c2/b2)*r2
c2=c2-(c2/b2)*b2

d4=d4-(d2/b2)*b4                       # for making d2=0
d3=d3-(d2/b2)*b3
r4=r4-(d2/b2)*r2
d2=d2-(d2/b2)*b2

d4=d4-(d3/c3)*c4                      #for making d3=0
r4=r4-(d3/c3)*r3
d3=d3-(d3/c3)*c3

B2=r4/d4#
D2=(r3-(c4*B2))/c3#
B1=(r2-(D2*b3)-(B2*b4))/b2#
D1=(r1-(B2*a4)-(D2*a3)-(B1*a2))/a1#
print "the values of MOLAR FLOW RATES of D1, B1, D2, B2 respectively are : %.f, %.f, %.f and %.f"%(D1,B1,D2,B2)

B=D2+B2#
x1B=(.21*D2 + .01*B2)/B#
x2B=(.54*D2 + .1*B2)/B#
x3B=(.1*D2 + .65*B2)/B#
x4B=(.15*D2 + .24*B2)/B#
print "the composition of stream B is %.3f, %.3f, %.3f & %.3f"%(x1B,x2B,x3B,x4B)

D=D1+B1#
x1D=(.35*D1 + .16*B1)/D#
x2D=(.54*D1 + .42*B1)/D#
x3D=(.04*D1 + .24*B1)/D#
x4D=(.07*D1 + .18*B1)/D#
print "the composition of stream D is",x1D,x2D,x3D,x4D
the values of MOLAR FLOW RATES of D1, B1, D2, B2 respectively are : 26, 18, 9 and 17
the composition of stream B is 0.077, 0.247, 0.467 & 0.210
the composition of stream D is 0.274 0.492 0.12 0.114

Exa1.5 Page 11

In [5]:
from __future__ import division
xnew=[];e=[]
for i in range(0,3):
    xnew.append(2)
    e.append(1)

x=1e-6
while e[0]>x and e[1]>x and e[2]>x:
    xold=[]
    for i in range(0,3):
        xold.append(xnew[i])
        
    xnew[0]=(44-xold[1]-2*xold[2])/10
    xnew[1]=(-2*xnew[0]+51-xold[2])/10
    xnew[2]=(-2*xnew[1]-xnew[0]+61)/10
    e=[]
    for i in range(0,3):
        e.append(abs(xnew[i]-xold[i]))
    
print "the values of x1,x2,x3 respectively are"
for i in range(0,3):
    print '%.f'%xnew[i]
the values of x1,x2,x3 respectively are
3
4
5

Exa1.6 Page 11

In [6]:
from __future__ import division
xnew=[];e=[];
for i in range(0,3):
    xnew.append(2)
    e.append(1)

x=1e-6
while e[0]>x and e[1]>x and e[2]>x:
    xold=[]
    for i in range(0,3):
        xold.append(xnew[i])
        
    xnew[0]=(9-xold[1]+2*xold[2])/3
    xnew[1]=(xnew[0]-8+3*xold[2])/4
    xnew[2]=(xnew[1]-xnew[0]+1)/4
    e=[]
    for i in range(0,3):
        e.append(abs(xnew[i]-xold[i]))
    
print "the values of x1,x2,x3 respectively : "
for i in range(0,3):
    print '%.f'%xnew[i]
the values of x1,x2,x3 respectively : 
3
-2
-1