You are on page 1of 4

Department of Chemical Engineering

CHL768
Assignment 1 (Solving of Matrices)
How to solve a Matrix equation:
N coupled linear algebraic equations, for N unknowns, xi, i=1,2,.,N, have the form:
a
11
x
1
+ a
12
x
2
+ .+a
1N
x
N
= b
1
a
21
x
1
+ a
22
x
2
+ .+a
2N
x
N
= b
2

.
a
N1
x1 + a
N2
x
2
+ .+a
NN
x
N
= b
N
In vector matrix notation this can be compactly written as :
Ax = b
Where,
A =
(
(
(
(
(
(

NN N N N
N
N
N
a a a a
a a
a a a a
a a a a
) 1 (
3
) 1 (
2
) 1 (
1
) 1 (
3
) 1 (
31
) 1 (
2
) 1 (
23
) 1 (
22
) 1 (
21
) 1 (
1
) 1 (
13
) 1 (
12
) 1 (
11
) 1 (
...
... ... ... ... ...
... ... ...
...
...
, x
T
=
(

N
x x x ...
2 1
, b
T
=
(

N
b b b ...
2 1

GAUSS ELIMINATION:

A
(1)
x =
(
(
(
(
(
(

(
(
(
(
(
(

N NN N N N
N
N
N
x
x
x
x
a a a a
a a
a a a a
a a a a
...
...
... ... ... ... ...
... ... ...
...
...
3
2
1
) 1 (
3
) 1 (
2
) 1 (
1
) 1 (
3
) 1 (
31
) 1 (
2
) 1 (
23
) 1 (
22
) 1 (
21
) 1 (
1
) 1 (
13
) 1 (
12
) 1 (
11
) 1 (
=
(
(
(
(
(
(

N
b
b
b
b
...
3
2
1
= b
(1)
The first row of A
(1)
and b
(1)
(corresponding to the first equation of the set) is multiplied by -a
(1)
21
/ a
(1)
11
and added to the second row, to eliminate the (2,1) term. Similarly, the first row is multiplied by
-a
(1)
31
/ a
(1)
11
and added to the third row, to eliminate the (3,1) term, etc.

A
(2)
x =
(
(
(
(
(
(
(
(
(
(

... * * 0
... ... ... 0
... * * 0
... * * 0
...
13
) 1 (
11
) 1 (
1
) 1 (
3
) 1 (
12
) 1 (
11
) 1 (
1
) 1 (
2
) 1 (
13
) 1 (
11
) 1 (
31
) 1 (
33
) 1 (
12
) 1 (
11
) 1 (
31
) 1 (
32
) 1 (
13
) 1 (
11
) 1 (
21
) 1 (
23
) 1 (
12
) 1 (
11
) 1 (
21
) 1 (
22
) 1 (
13
) 1 (
12
) 1 (
11
) 1 (
a
a
a
a a
a
a
a
a
a
a
a a
a
a
a
a
a
a
a a
a
a
a
a a a
N
N
N
N
(
(
(
(
(
(
(

N
x
x
x
x
....
3
2
1



=
(
(
(
(
(
(
(
(
(
(
(

) 1 (
1
11
) 1 (
1
) 1 (
) 1 (
) 1 (
1
11
) 1 (
31
) 1 (
) 1 (
3
) 1 (
1
11
) 1 (
21
) 1 (
) 1 (
2
) 1 (
1
...
b
a
a
b
b
a
a
b
b
a
a
b
b
N
N


A
(2)
x =
(
(
(
(
(
(

NN N N
N
N
N
a a a
a
a a a
a a a a
) 2 (
3
) 2 (
2
) 2 (
3
) 2 (
2
) 2 (
23
) 2 (
22
) 2 (
1
) 1 (
13
) 1 (
12
) 1 (
11
) 1 (
... 0
... ... ... ... ...
... ... ... 0
... 0
...

(
(
(
(
(
(

N
x
x
x
....
2
1
=
(
(
(
(
(
(
(

) 2 (
) 2 (
3
) 2 (
2
) 1 (
1
...
N
b
b
b
b
= b
(2)




The next iteration attempts to eliminate (3,2),(4,2),.,(N,2) terms from A
(2)
. This is done by
successively multiplying the second row with
22
) 2 (
32
) 2 (
a
a
,
22
) 2 (
42
) 2 (
a
a
, .. and
22
) 2 (
2
) 2 (
a
a N
and added to
3
rd
,4
th
,.,Nth rows. This leads to :
A
(3)
x =
| | | |
(
(
(
(
(
(
(
(
(
(
(

... * 0 0
... * 0 0
... * 0 0
... 0
...
23
) 2 (
22
) 2 (
2
) 2 (
3
) 2 (
23
) 2 (
22
) 2 (
42
) 2 (
43
) 2 (
23
) 2 (
22
) 2 (
32
) 2 (
33
) 2 (
23
) 2 (
22
) 2 (
13
) 1 (
12
) 1 (
11
) 1 (
a
a
a
a
a
a
a
a
a
a
a
a
a a
a a a
N
N
(
(
(
(
(
(
(

N
x
x
x
x
....
3
2
1


=
(
(
(
(
(
(
(
(
(
(

) 2 (
2
22
) 2 (
2
) 2 (
) 2 (
) 2 (
2
22
) 2 (
32
) 2 (
) 2 (
3
) 2 (
2
) 1 (
1
...
b
a
a
b
b
a
a
b
b
b
N
N
= b
(3)

A
(N)
x = Ux =
(
(
(
(
(
(

(
(
(
(
(
(

N NN
N
N
N
x
x
x
x
a
a a
a a a
a a a a
...
0 0 0 0
... ... ... 0 0
... 0 0
... 0
...
3
2
1
) 1 (
3
) 3 (
33
) 3 (
2
) 2 (
23
) 2 (
22
) 2 (
1
) 1 (
13
) 1 (
12
) 1 (
11
) 1 (
=
(
(
(
(
(
(
(

) (
) 3 (
3
) 2 (
2
) 1 (
1
...
N
N
b
b
b
b
= b
(N)
U is the upper triangular matrix for A
(N)
. The unknown vector [x] can now be solved by a backward
sweep.


Q1. Write an algorithm for Gauss elimination and backward sweep.
Q2. Write a Fortran or C Program to solve a matrix N*N via Gauss elimination.
Q3. Solve with your program the following matrices by Gauss elimination.
A:
(
(
(

=
(
(
(

(
(
(

4
2
1
1 1 0
1 2 1
0 1 2
3
2
1
x
x
x


B:
(
(
(

=
(
(
(

(
(
(

2
20
8
1 1 1
3 2 2
2 1 1
3
2
1
x
x
x

C:
(
(
(

=
(
(
(

(
(
(

2
1
1 1
1 0001 . 0
2
1
x
x

LU Decomposition:
In many situations it is needed to solve Ax=b for several different values of b (but with the same A). In
such cases decomposition of A is done once and is stored in a lower and upper matrix. The upper matrix
U is defined earlier. The lower matrix is composed of the negative of the multiplying factors.
L =
(
(
(
(
(
(

1
0
...
....
/
...
/
...
0 ... / /
0 ... 1 /
0 0 0 1
22
) 2 (
2
) 2 (
11
) 1 (
1
) 1 (
22
) 2 (
32
) 2 (
11
) 1 (
31
) 1 (
11
) 1 (
21
) 1 (
a a a a
a a a a
a a
N N
; LU = A ; Ly = b ; Ux =y

Q4. Write a FORTRAN or C program to store the matrix A in lower and upper part.
Q5.Solve with your program the following matrix by LU decomposition and check LU=A.

(
(
(

=
(
(
(

(
(
(

20
2
8
3 2 2
1 1 1
2 1 1
3
2
1
x
x
x

You might also like