Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

MATRIX OPERATION USING GAUSS

ELIMINATION

1 Theory
Let us conider a following set of equation:

a11 x1 + a12 x2 + a13 x3 = b1


a21 x1 + a22 x2 + a23 x3 = b2
a31 x1 + a32 x2 + a33 x3 = b3
For the above set of equation let us take a matrix:
    
a11 a12 a13 x1 b1
a21 a22 a23   x 2 = b2 
 
a31 a32 a33 x3 b3
First round of Elimination:
R2 → R2 − a21 × R1 /a11
R3 → R3 − a31 × R1 /a11
Second Round of Elimination:
(1)
R3 → R3 − a32 × R2 /a22
Where (1) refers to the element from matrix after first round of elimination
For Back substution we have:
(2) (2) (2) (2)
a11 X1 + a12 X2 + a13 X3 = b1
(2) (2) (2)
a22 X2 + a23 X3 = b1
(2) (2)
a33 X3 = b1

2 Program
a. Problem: Solve the following set of linear equation

3x1 + 2x2 + 4x3 = 7


2x1 + x2 + x3 = 4
x1 + 3x2 + 5x3 = 2

15
b. Source Code:

from numpy import mat


A = mat ( [ [ 3 . , 2 . , 4 . , 7 . ] , [ 2 . , 1 . , 1 . , 4 . ] , [ 1 . , 3 . , 5 . , 2 . ] ] )
# Gauss E l i m i n a t i o n : F i r s t round
A[ 1 , : ] =A[ 1 , : ] − A[ 0 , : ] ∗ A[ 1 , 0 ] /A[ 0 , 0 ]
A[ 2 , : ] =A[ 2 , : ] − A[ 0 , : ] ∗ A[ 2 , 0 ] /A[ 0 , 0 ]
# Gauss E l i m i n a t i o n : Second round
A[ 2 , : ] = A[ 2 , : ] − A[ 2 , 1 ] ∗A[ 1 , : ] / A[ 1 , 1 ]
#Back s u b s t i t u t i o n
print (A)
x3 = A[ 2 , 3 ] /A[ 2 , 2 ]
x2 = (A[ 1 , 3 ] − A[ 1 , 2 ] ∗ x3 ) /A[ 1 , 1 ]
x l = (A[ 0 , 3 ] − A[ 0 , 1 ] ∗ x2 − A[ 0 , 2 ] ∗ x3 ) /A[ 0 , 0 ]
print ( ’ x1 = ’ , xl , ’ , x2 = ’ , x2 , ’ , x3 = ’ , x3 )

c. Output:

[[ 3. 2. 4. 7. ]
[ 0. -0.33333333 -1.66666667 -0.66666667]
[ 0. 0. -8. -5. ]]
x1 = 2.25 ,x2 = -1.1249999999999996 ,x3 = 0.6250000000000001

16

You might also like