You are on page 1of 3

Tridiagonal Matrices: Thomas Algorithm

W. T. Lee

MS6021, Scientic Computation, University of Limerick


The Thomas algorithm is an efcient way of solving tridiagonal matrix systems. It is based on LU decompo-
sition in which the matrix system Mx = r is rewritten as LUx = r where L is a lower triangular matrix and
U is an upper triangular matrix. The system can be efciently solved by setting Ux = and then solving rst
L = r for and then Ux = for x. The Thomas algorithm consists of two steps. In Step 1 decomposing the
matrix into M = LU and solving L = r are accomplished in a single downwards sweep, taking us straight
from Mx = r to Ux = . In step 2 the equation Ux = is solved for x in an upwards sweep.
I. STAGE 1
In the rst stage the matrix equation Mx = r is converted
to the form Ux = . Initially the matrix equation looks like:

b
1
c
1
0 0 0 0
a
2
b
2
c
2
0 0 0
0 a
3
b
3
c
3
0 0
0 0 a
4
b
4
c
4
0
0 0 0 a
5
b
5
c
5
0 0 0 0 a
6
b
6

x
1
x
2
x
3
x
4
x
5
x
6

r
1
r
2
r
3
r
4
r
5
r
6

Row 1
b
1
x
1
+ c
1
x
2
= r
1
Divide through by b
1
x
1
+
c
1
b
1
x
2
=
r
1
b
1
Rewrite:
x
1
+
1
x
2
=
1
,
1
=
c
1
b
1
,
1
=
r
1
b
1

1
1
0 0 0 0
a
2
b
2
c
2
0 0 0
0 a
3
b
3
c
3
0 0
0 0 a
4
b
4
c
4
0
0 0 0 a
5
b
5
c
5
0 0 0 0 a
6
b
6

x
1
x
2
x
3
x
4
x
5
x
6

1
r
2
r
3
r
4
r
5
r
6

Row 2.
a
2
x
1
+ b
2
x
2
+ c
2
x
3
= r
2
Use a
2
times row 1 of the matrix to eliminate the rst term
a
2
(x
1
+
1
x
2
=
1
)

http://www3.ul.ie/wlee; william.lee@ul.ie
Row 2 a
2
x
1
+ b
2
x
2
+c
2
x
3
= r
2
a
2
Row 1 a
2
x
1
+ a
2

1
x
2
= a
2

1
New Row 2 (b
2
a
2

1
) x
2
+c
2
x
3
= r
2
a
2

1
Divide through by (b
2
a
2

1
) to get
x
1
+
c
2
b
2
a
2

1
x
2
=
r
2
a
2

1
b
2
a
2

1
We can rewrite this as
x
2
+
2
x
3
=
2
,
2
=
c
2
b
2
a
2

1
,
2
=
r
2
a
2

1
b
2
a
2

1
.

1
1
0 0 0 0
0 1
2
0 0 0
0 a
3
b
3
c
3
0 0
0 0 a
4
b
4
c
4
0
0 0 0 a
5
b
5
c
5
0 0 0 0 a
6
b
6

x
1
x
2
x
3
x
4
x
5
x
6

2
r
3
r
4
r
5
r
6

Row 3.
a
3
x
2
+ b
3
x
3
+ c
3
x
4
= r
3
Use a
3
times row 2 of the matrix to eliminate the rst term
a
3
(x
2
+
2
x
3
=
2
)
Row 3 a
3
x
2
+ b
3
x
3
+c
3
x
4
= r
3
a
3
Row 2 a
3
x
2
+ a
3

2
x
3
= a
3

2
New Row 3 (b
3
a
3

2
) x
3
+c
3
x
4
= r
3
a
3

2
Divide through by (b
3
a
3

2
) to get
x
2
+
c
3
b
3
a
3

2
x
3
=
r
3
a
3

2
b
3
a
3

2
We can rewrite this as
x
3
+
3
x
4
=
3
,
3
=
c
3
b
3
a
3

2
,
3
=
r
3
a
3

2
b
3
a
3

2
.
2

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 a
4
b
4
c
4
0
0 0 0 a
5
b
5
c
5
0 0 0 0 a
6
b
6

x
1
x
2
x
3
x
4
x
5
x
6

3
r
4
r
5
r
6

Row 4.
a
4
x
3
+ b
4
x
4
+ c
4
x
5
= r
4
Use a
4
times row 3 of the matrix to eliminate the rst term
a
4
(x
3
+
3
x
4
=
3
)
Row 4 a
4
x
3
+ b
4
x
4
+c
4
x
5
= r
4
a
4
Row 3 a
4
x
3
+ a
4

3
x
4
= a
4

3
New Row 4 (b
4
a
4

3
) x
4
+c
4
x
5
= r
4
a
4

3
Divide through by (b
4
a
4

3
) to get
x
3
+
c
4
b
4
a
4

3
x
4
=
r
4
a
4

3
b
4
a
4

3
We can rewrite this as
x
4
+
4
x
5
=
4
,
4
=
c
4
b
4
a
4

3
,
4
=
r
4
a
4

3
b
4
a
4

3
.

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 a
5
b
5
c
5
0 0 0 0 a
6
b
6

x
1
x
2
x
3
x
4
x
5
x
6

4
r
5
r
6

Row 5.
a
5
x
4
+ b
5
x
5
+ c
5
x
6
= r
5
Use a
5
times row 4 of the matrix to eliminate the rst term
a
5
(x
4
+
4
x
5
=
4
)
Row 5 a
5
x
4
+ b
5
x
5
+c
5
x
6
= r
5
a
5
Row 4 a
5
x
4
+ a
5

4
x
5
= a
5

4
New Row 5 (b
5
a
5

4
) x
5
+c
5
x
6
= r
5
a
5

4
Divide through by (b
5
a
5

4
) to get
x
4
+
c
5
b
5
a
5

4
x
5
=
r
5
a
5

4
b
5
a
5

4
We can rewrite this as
x
5
+
5
x
6
=
5
,
5
=
c
5
b
5
a
5

4
,
5
=
r
5
a
5

4
b
5
a
5

4
.

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 0 1
5
0 0 0 0 a
6
b
6

x
1
x
2
x
3
x
4
x
5
x
6

5
r
6

Row 6
a
6
x
5
+ b
6
x
6
= r
6
Use a
6
times row 5 to eliminate the rst term.
a
6
x
5
+ a
6

5
x
6
= a
6

5
Resulting in
(b
6
a
6

5
) x
6
= r
6
a
6

5
Divide through by b
6
a
6

5
to get
x
6
=
6
,
6
=
r
6
a
6

5
b
6
a
6

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 0 1
5
0 0 0 0 0 1

x
1
x
2
x
3
x
4
x
5
x
6

At this point the matrix has been reduced to upper diagonal


form, so our equations are in the form Ux = .
II. STAGE 2
The matrix equation is now in a form which is trivial to
solve for x. We start with the last row and work our way up.
The nal equation is already solved
x
6
=
6
.

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 0 1
5
0 0 0 0 0 1

x
1
x
2
x
3
x
4
x
5
x
6

3
Row 5: x
5
+
5
x
6
=
5
.
Rearrange to get: x
5
=
5

5
x
6
.

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 0 1
5
0 0 0 0 0 1

x
1
x
2
x
3
x
4
x
5
x
6

Row 4: x
4
+
4
x
5
=
4
.
Rearrange to get: x
4
=
4

4
x
5
.

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 0 1
5
0 0 0 0 0 1

x
1
x
2
x
3
x
4
x
5
x
6

Row 3: x
3
+
3
x
4
=
3
.
Rearrange to get: x
3
=
3

3
x
4
.

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 0 1
5
0 0 0 0 0 1

x
1
x
2
x
3
x
4
x
5
x
6

Row 2: x
2
+
2
x
3
=
2
.
Rearrange to get: x
2
=
2

2
x
3
.

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 0 1
5
0 0 0 0 0 1

x
1
x
2
x
3
x
4
x
5
x
6

Row 1: x
1
+
1
x
2
=
1
.
Rearrange to get: x
1
=
1

1
x
2
.

1
1
0 0 0 0
0 1
2
0 0 0
0 0 1
3
0 0
0 0 0 1
4
0
0 0 0 0 1
5
0 0 0 0 0 1

x
1
x
2
x
3
x
4
x
5
x
6

At this point x, the solution to the matrix equation, is fully


determined.
III. IN PRACTICE
The Thomas algorithm is used because it is fast and be-
cause tridiagonal matrices often occur in practice. (This ar-
gument is slightly circular because people often manipulate
the problems they are working on to reduce them to solving a
tridiagonal matrix problem.) Although it is rare, the algorithm
can be unstable if b
i
a
i

i1
is zero or numerically zero for
any i. This will occur if the tridiagonal matrix is singular, but
in rare cases can occur if it is non-singular. The condition for
the algorithm to be stable is
b
i
> a
i
+c
i

for all i. The matrix problems which result from the discreti-
sation of partial differential equations nearly all satisfy this
criterion.
If the algorithm is numerically unstable then you must re-
arrange the equations: known as pivoting. Standard LU de-
composition algorithms for full or banded matrices include
pivoting. (But rst you should check to make sure you have
not made a mistake in formulating the problem.)

You might also like