Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix...

14
Page | 1 Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to develop efficient solution schemes. Two of those such systems are banded and symmetric matrices. A banded matrix is a square matrix that has all elements equal to zero, with the exception of a band centered on the main diagonal. Banded systems are frequently encountered in the solution of differential equations. In addition, other numerical methods such as cubic splines involve the solution of banded systems. The dimensions of a banded system can be quantified by two parameters: the bandwidth BW and the half- bandwidth HBW (see figure below). These two values are related by BW = 2HBW + 1. In general, then, a banded system is one for which aij = 0 if |i − j | > HBW. For a triadiagonal matrix BW=3, HBW=1. For a pentadiagonal matrix BW=5, and HBW=2. Next efficient elimination methods are described for both. Heat Transfer Problem As an example to illustrate the solution mechanism for banded matrices, specifically for tridiagonal matrices, we are going to use a simple problem from the heat transfer field. Assume that the extremes of a cylindrical metal rod are maintained at different temperatures, 0°C (left-hand extreme) and 1°C (right-hand extreme) and we wish to determine the steady state temperature at interior nodes. To do this, we divide the rod into 11 small imaginary cylinders, the temperatures of each cylinder is represented by the geometric center, named interior nodes as shown in the figure below: 0 o C 1 o C 11 nodes x

Transcript of Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix...

Page 1: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 1

Thomas Algorithm for Tridiagonal Matrix

Special Matrices

Some matrices have a particular structure that can be exploited to develop efficient solution schemes. Two of

those such systems are banded and symmetric matrices.

A banded matrix is a square matrix that has all elements equal to zero, with the exception of a band centered on

the main diagonal. Banded systems are frequently encountered in the solution of differential equations. In

addition, other numerical methods such as cubic splines involve the solution of banded systems.

The dimensions of a banded system can be quantified by two parameters: the bandwidth BW and the half-

bandwidth HBW (see figure below). These two values are related by BW = 2HBW + 1. In general, then, a banded

system is one for which aij = 0 if |i − j | > HBW.

For a triadiagonal matrix BW=3, HBW=1. For a

pentadiagonal matrix BW=5, and HBW=2.

Next efficient elimination methods are described

for both.

Heat Transfer Problem

As an example to illustrate the solution mechanism

for banded matrices, specifically for tridiagonal

matrices, we are going to use a simple problem

from the heat transfer field. Assume that the

extremes of a cylindrical metal rod are maintained

at different temperatures, 0°C (left-hand extreme)

and 1°C (right-hand extreme) and we

wish to determine the steady state temperature at interior nodes. To do this, we divide the rod into 11 small

imaginary cylinders, the temperatures of each cylinder is represented by the geometric center, named interior

nodes as shown in the figure below:

0 oC 1

oC

11 nodes x

Page 2: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 2

At steady state, the heat transfer equation takes the form:

𝜕2𝑢

𝜕𝑥2= 0 0 ≤ 𝑥 ≤ 1

𝐵𝐶′𝑠: 𝑢 = 0 𝑥 = 0 𝑢 = 1 𝑥 = 1

where u is the temperature, and x is the spatial one dimensional coordinate.

Expand the second derivative with central difference representation of error order 𝜗(ℎ2):

𝑢(𝑖 + 1) − 2𝑢(𝑖) + 𝑢(𝑖 − 1)

∆𝑥2= 0 𝑖 = 2, 3, 4, … 𝑖𝑥 − 1

Rearrange and simplify

𝑢(𝑖 − 1) − 2𝑢(𝑖) + 𝑢(𝑖 + 1) = 0 𝑖 = 2, 3, 4, … 𝑖𝑥 − 1

Choose the number of points to be solved ix = 11 (i.e., the number of nodes) and expand the above equation

such as the resulting index for 𝑢 are i=1, 2, 3, … ix (within the range of your problem discretization).

𝑢(1) − 2𝑢(2) + 𝑢(3) = 0

𝑢(2) − 2𝑢(3) + 𝑢(4) = 0

𝑢(3) − 2𝑢(4) + 𝑢(5) = 0

𝑢(4) − 2𝑢(5) + 𝑢(6) = 0

𝑢(5) − 2𝑢(6) + 𝑢(7) = 0

𝑢(6) − 2𝑢(7) + 𝑢(8) = 0

𝑢(7) − 2𝑢(8) + 𝑢(9) = 0

𝑢(8) − 2𝑢(9) + 𝑢(10) = 0

𝑢(9) − 2𝑢(10) + 𝑢(11) = 0

The range of this discretization is 1 ≤ 𝑖 ≤ 𝑖𝑥 for later coding with matlab

Name the coefficients of these equations p, q, r and the right-hand side vector s:

𝑝(1)𝑢(1) + 𝑞(1)𝑢(2) + 𝑟(1)𝑢(3) = 𝑠(1)

𝑝(2)𝑢(2) + 𝑞(2)𝑢(3) + 𝑟(2)𝑢(4) = 𝑠(2)

𝑝(3)𝑢(3) + 𝑞(3)𝑢(4) + 𝑟(3)𝑢(5) = 𝑠(3)

𝑝(4)𝑢(4) + 𝑞(4)𝑢(5) + 𝑟(4)𝑢(6) = 𝑠(4)

𝑝(5)𝑢(5) + 𝑞(5)𝑢(6) + 𝑟(5)𝑢(7) = 𝑠(5)

𝑝(6)𝑢(6) + 𝑞(6)𝑢(7) + 𝑟(6)𝑢(8) = 𝑠(6)

𝑝(7)𝑢(7) + 𝑞(7)𝑢(8) + 𝑟(7)𝑢(9) = 𝑠(7)

𝑝(8)𝑢(8) + 𝑞(8)𝑢(9) + 𝑟(8)𝑢(10) = 𝑠(8)

𝑝(9)𝑢(9) + 𝑞(9)𝑢(10) + 𝑟(9)𝑢(11) = 𝑠(9)

Page 3: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 3

The matrix form yields a “quasi” tridiagonal matrix:

The empty cells within the matrix coefficients matrix are actually filled with zeros. Please notice that that the

above matrix is not square; we need to make it square. Thomas algorithm consists of two steps, direct sweep

and inverse sweep (Con el permiso de Thomas, this method is just Gaussian elimination applied to a tridiagonal

matrix avoiding the job on zeros entries).

Page 4: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 4

DIRECT SWEEP (forward pass on Gaussian Elimination)

First step

Eliminate u(1) and u(11) (i.e, first and last) from the matrix of coefficients in the first and last equations, by

passing them to the other side of the equal sign. This elimination is possible as u(1) and u(11) are known

temperatures.

For the first equation:

p(1)u(1) + q(1)u(2) + r(1)u(3) = s(1)

q(1)u(2) + r(1)u(3) = s(1) - p(1)u(1)

and for the last equation:

p(9)u(9) + q(9)u(10) + r(9)u(11) = s(9)

p(9)u(9) + q(9)u(10) = s(9) - r(9)u(11)

which square and simplifies the original matrix to a tridiagonal matrix. A tridiagonal system—that is, one with a

bandwidth of 3—can be expressed generally as

Rename: s(1) = s(1) - p(1)u(1)

s(9) = s(9) – r(9)u(11)

columns

rows 1 2 3 4 5 6 7 8 9

1 q(1) r(1) u(2) s(1) - p(1)u(1)

2 p(2) q(2) r(2) u(3) s(2)

3 p(3) q(3) r(3) u(4) s(3)

4 u(5) s(4)

5 ... = u(6) = s(5)

6 ... u(7) s(6)

7 ... u(8) s(7)

8 u(9) s(8)

9 p(9) q(9) u(10) s(9) - r(9)u(11)

1 2 3 4 5 6 7 8 9

Every time you solve a problem similar to

this one, you must develop a square

matrix as the above. This is the MATRIX

representation of your problem

Page 5: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 5

This is a clean matrix equation suitable for a solution representing your problem.

The following procedure is equivalent to the steps of Gauss elimination by using the banded matrix notation

which prevents from working with the ‘zero’ elements.

The first equation:

q(1)u(2) + r(1)u(3) = s(1) Eq-1

Normalize the first equation, dividing it by q1 (pivot element). In the following equations we have simplified the

notation, e.g. q(1) becomes q1, etc.

𝑢2 +𝑟1

𝑞1𝑢3 =

𝑠1

𝑞1 rename: 𝑢2 + ��1𝑢3 = ��1 Eq-2, where ��1 =

𝑟1

𝑞1 and ��1 =

𝑠1

𝑞1 Eq-3

Multiplied the first equation by p2 and subtract it from the second equation:

p2u2 + q2u3 + r2u4 = s2 Eq-4

−(p2u2 + p2 r1u3 = p2s1) Multiply Eq-2 by p2 Eq-5

(𝑞2 − 𝑝2��1)𝑢3 + 𝑟2𝑢4 = 𝑠2 − 𝑝2��1 Eq-6

Notice that the above procedure works for eliminating u2.

Normalize E-6 dividing by 𝑞2 − 𝑝2��1 (i.e., the coefficient of u3

𝑢3 +𝑟2

𝑞2−𝑝2��1𝑢4 =

𝑠2−𝑝2��1

𝑞2−𝑝2��1 Eq-7

Rename Eq-7:

𝑢3 + ��2𝑢4 = ��2 Eq-8 where ��2 = 𝑟2

𝑞2−𝑝2��1 and ��2 =

𝑠2−𝑝2��1

𝑞2−𝑝2��1 Eq-9

We repeat the procedure with the 3rd equation and eliminate u3 using the previous equation (Eq-8):

columns

rows 1 2 3 4 5 6 7 8 9

1 q(1) r(1) u(2) s(1)

2 p(2) q(2) r(2) u(3) s(2)

3 p(3) q(3) r(3) u(4) s(3)

4 u(5) s(4)

5 ... u(6) = s(5)

6 ... u(7) s(6)

7 ... u(8) s(7)

8 u(9) s(8)

9 p(9) q(9) u(10) s(9)

1 2 3 4 5 6 7 8 9

Page 6: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 6

𝑝3𝑢3 + 𝑞3𝑢4 + 𝑟3𝑢5 = 𝑠3 Eq-10

−(𝑝3𝑢3 + 𝑝3��2𝑢4 = 𝑝3��2) Multiply Eq-8 by p3 Eq-11

(𝑞3 − 𝑝3��2)𝑢4 + 𝑟3𝑢5 = 𝑠3 − 𝑝3��2 Eq-12

Normalize Eq-12 dividing by 𝑞3 − 𝑝3��2

𝑢4 +𝑟3

𝑞3−𝑝3��2𝑢5 =

𝑠3−𝑝3��2

𝑞3−𝑝3��2 Eq-13

Rename:

𝑢4 + ��3𝑢5 = ��3 Eq-14 where ��3 = 𝑟3

𝑞3−𝑝3��2 and ��3 =

𝑠3−𝑝3��2

𝑞3−𝑝3��2 Eqs-15

By looking at Eqs-9 and Eqs-15 we derived the recurrence formulas:

𝑟�� = 𝑟𝑖

𝑞𝑖−𝑝𝑖𝑟𝑖−1 Eq-16 and 𝑠�� =

𝑠𝑖−𝑝𝑖𝑠𝑖−1

𝑞𝑖−𝑝𝑖𝑟𝑖−1 Eq-17

For the first equation in the coefficient Matrix where p1 is not used in the left hand side, it is enough to set p1=0

for both (Eq-16 and Eq-17) recurrence formulas to yield an expression for the r1 and s1:

𝑟1 = 𝑟1

𝑞1 Eq-3a and 𝑠1 =

𝑠1

𝑞1 Eq-3b

NOTE: This doesn’t mean that p(1)=0 for the right-hand side vector

INVERSE SWEEP

Here we compute from the last equation backwards to the first. Start with the last equation:

p9u9 + q9u10 = s9 – r9u11 Eq-18

Since ix =11, the above equation can be expressed also as:

p(ix-2)u(ix-2) + q(ix-2)u(ix-1) = s(ix-2) Eq-19

Write the second to last equation by looking the pattern of either Eq.-2 or Eq. 8:

𝑢(𝑖𝑥 − 2) + ��(𝑖𝑥 − 3)𝑢(𝑖𝑥 − 1) = �� (𝑖𝑥 − 3) Eq-20

Multiply Eq-20 by p(ix-2) and subtract it from Eq-19:

Page 7: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 7

Last equation 𝑝(𝑖𝑥 − 2)𝑢(𝑖𝑥 − 2) + 𝑞(𝑖𝑥 − 2)𝑢(𝑖𝑥 − 1) = 𝑠(𝑖𝑥 − 2)

Second last −(𝑝(𝑖𝑥 − 2)𝑢(𝑖𝑥 − 2) + 𝑝(𝑖𝑥 − 2)��(𝑖𝑥 − 3)𝑢(𝑖𝑥 − 1) = 𝑝(𝑖𝑥 − 2)��(𝑖𝑥 − 3))

[𝑞(𝑖𝑥 − 2) − 𝑝(𝑖𝑥 − 2)��(𝑖𝑥 − 3)]𝑢(𝑖𝑥 − 1) = 𝑠(𝑖𝑥 − 2) − 𝑝(𝑖𝑥 − 2)��(𝑖𝑥 − 3) Eq-20

Normalize Eq-20

𝑢(𝑖𝑥 − 1) = 𝑠(𝑖𝑥−2)−𝑝(𝑖𝑥−2)��(𝑖𝑥−3)

𝑞(𝑖𝑥−2)−𝑝(𝑖𝑥−2)��(𝑖𝑥−3) Eq-21

Rename the right-hand side of Eq-21:

��(𝑖𝑥 − 2) = 𝑠(𝑖𝑥−2)−𝑝(𝑖𝑥−2)��(𝑖𝑥−3)

𝑞(𝑖𝑥−2)−𝑝(𝑖𝑥−2)��(𝑖𝑥−3) Eq-22

Then Eq-21 becomes

𝑢(𝑖𝑥 − 1) = ��(𝑖𝑥 − 2) Eq-23

As expected, the definition of �� is the same as in the previous direct sweep step.

For the current example ix=11, therefore ux(ix-1) = u(10), and Eq-21 can be easily computed:

𝑢(10) = 𝑠(9)−𝑝(9)��(8)

𝑞(9)−𝑝(9)��(8) Eq-24

Observe that all constants in the RHS of Eq-24 are known values. Now the following equation from bottom up

(second to last equation):

𝑢(𝑖𝑥 − 2) + ��(𝑖𝑥 − 3)𝑢(𝑖𝑥 − 1) = ��(𝑖𝑥 − 3) Eq-20

Solve by u(ix-2):

𝑢(𝑖𝑥 − 2) = ��(𝑖𝑥 − 3) − ��(𝑖𝑥 − 3)𝑢(𝑖𝑥 − 1) Eq-25

Again observe that you already know every variable and parameter on the RHS.

Generalize

𝑢(𝑖) = ��(𝑖 − 1) − ��(𝑖 − 1)𝑢(𝑖 + 1) Eq-26

Please become aware of that the last computed u(i) will be, e.g., 𝑢(2) = ��(1) − ��(1)𝑢(3)

The formulas to apply then

For the last equation

Page 8: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 8

𝑢(𝑖𝑥 − 1) = ��(𝑖𝑥 − 2) Eq-23

For the next ones, from bottom up:

𝑢(𝑖) = ��(𝑖 − 1) − ��(𝑖 − 1)𝑢(𝑖 + 1) Eq-26

And so on. For the last equation to be solved we have:

𝑢(2) = ��(1) − ��(1)𝑢(3)

Summarizing the equations:

DIRECT SWEEP

𝑟�� = 𝑟𝑖

𝑞𝑖−𝑝𝑖𝑟𝑖−1 Eq-16 and 𝑠�� =

𝑠𝑖−𝑝𝑖𝑠𝑖−1

𝑞𝑖−𝑝𝑖𝑟𝑖−1 Eq-17

In Eq-16, indices run as i=2, 3, 4, …, ix-3 (note that 𝑟(9) = 0 is in the other side of the equation and 𝑟(9)=0 does

not exist).

Eq-17 applies for i=2, 3, …, ix-2

For the first equation where i=1 and p(1) is in the other side of the equation, the next two equations apply:

𝑟1 = 𝑟1

𝑞1 Eq-3a and 𝑠1 =

𝑠1

𝑞1 Eq-3b

INVERSE SWEEP

The formulas to apply then

For the last equation

𝑢(𝑖𝑥 − 1) = ��(𝑖𝑥 − 2) Eq-23

For the next ones up

𝑢(𝑖) = ��(𝑖 − 1) − ��(𝑖 − 1)𝑢(𝑖 + 1) Eq-26

the last unknown to be computed, i.e., i=2:

𝑢(2) = ��(1) − ��(1)𝑢(3)

Therefore, for this step you

compute the unknowns in the

order u(ix-1), u(9), u(8), …, u(2)

Page 9: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 9

For the heat transfer problem-example and for developing MATLAB code

𝑢(𝑖 − 1) − 2𝑢(𝑖) + 𝑢(𝑖 + 1) = 0 𝑖 = 2, 3, 4, … 𝑖𝑥 − 1

For values of the variables and coefficients:

u(1), u(2), u(3), …, u(11) u(1),… u(ix)

p(1), p(2), p(3),…, p(9) p(1),…p(ix-2)

q(1), q(2), q(3),…, q(9) q(1),…q(ix-2)

r(1), r(2), r(3),….,r(9) r(1),….r(ix-2)

s(1), s(2), s(3),…., s(9) s(1),… s(ix-2)

Actual values:

u(1)=0.0 BC

u(ix)=u(11)=1 BC

p(i)=1.0 i=1,2,.. ix-2

q(i)=-2.0 i=1,2,…ix-2

r(i)=1.0 i=1,2,…ix-2

The values of s(1) and s(9) were renamed, here we go back to the original definition:

s(1)=s(1)-p(1)u(1)=0-(1)(0)=0 % Note that p(1) is not zero for the right-hand side vector

…..

s(9)=s(9)-r(9)u(11)=0-(1)(1)=-1 % Note that r(9) is not zero for the right-hand side vector

In other words, for the current problem:

s(i) =0 i=2,…ix-3 (i.e., ix-3=8); all are zeros except for s(9)

s(9)=s(ix-2)=-1

After identifying p(i), q(i), r(i), s(i) and u(i) we start computing ��(𝑖), and ��(𝑖)

Page 10: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 10

��(1) =𝑟(1)

𝑞(1) i=1

��(𝑖) =𝑟(𝑖)

[𝑞(𝑖)−𝑝(𝑖)��(𝑖−1) i=2,3,…ix-3

��(1) =𝑠(1)

𝑞(1)=

0

−2= 0 i=1

��(𝑖) =(𝑠(𝑖)−𝑝(𝑖)��(𝑖−1))

(𝑞(𝑖)−𝑝(𝑖)��(𝑖−1)) i=2,3,..,ix-2

Now keep going with the INVERSE SWEEP to find u(i)’s

At the end of your computations you should be able to summarize your results in a Table:

in which not all the cells are filled with values. Please as an assignment fill the table above for the heat transfer

problem, this practice is critical for your success in a test.

EXACT SOLUTION

We have chosen a problem that has an exact (and simple) solution, just to show the solution mechanism for the

numerical method. The analytical (exact) solution is then

𝜕2𝑢

𝜕𝑥2 = 0 0 ≤ 𝑥 ≤ 1

𝜕

𝜕𝑥(

𝜕𝑢

𝜕𝑥⏟𝐾1

) = 0

𝜕𝑢

𝜕𝑥= 𝐾1 ==> 𝑢 = 𝐾1𝑥 + 𝐾2

Apply boundary conditions to find the values of K1 and K2:

i p(i) q(i) r(i) s(i) u(i)

1

2

3

4

5

6

7

8

9

Page 11: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 11

u=0 x=0 K2=0

u=1 x=1 K1=1

Solution

u = x

In an actual exam, the evaluation of the Thomas Algorithm may involve.

(1) Write down the system of equations in standarized form

(2) Write down the system in Matrix form (square matrix)

(3) Identify the p(i), q(i), r(i), and s(i)

(4) Compute the ��(𝑖), and ��(𝑖) parameters

(5) Compute the 𝑢(𝑖)′𝑠

(6) Fill a table like

i p(i) q(i) r(i) s(i) u(i)

1

2

3

4

...

...

...

...

ix-2

Page 12: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 12

Excel computation

MATLAB , to the RESCUE Following are several ways of creating tridiagonal matrices. You need to

create a tridiagonal matrix to use standard methods of solving a matrix

system with matlab, such as x=A\b

% Tridiagonal Maker % file: tridiaMaker.m % TM [tridiagonal matrix] clc, clear, close

% Option 1: n = 5;

p = -1;

File: Tridiagonal Algorithm

TRIDIAGONAL ALGORITHMDIRECT SWEEP (Computation start with r^(1), s^(1) up to r^(9) and s^(9))

i p q r s ř ŝ u

1 1 -2 1 0 -0.5 0 0 BC's

2 1 -2 1 0 -0.66667 0

3 1 -2 1 0 -0.75 0

4 1 -2 1 0 -0.8 0

5 1 -2 1 0 -0.83333 0

6 1 -2 1 0 -0.85714 0

7 1 -2 1 0 -0.875 0

8 1 -2 1 0 -0.88889 0

9 1 -2 1 -1 -0.9 0.9

10

11 1 BC's first step

second step

third step

INVERSE SWEEP (Computation goes from u(10) up to u(2)

i p q r s ř ŝ u

1 1 -2 1 0 -0.5 0 0 BC's

2 1 -2 1 0 -0.66667 0 0.1

3 1 -2 1 0 -0.75 0 0.2

4 1 -2 1 0 -0.8 0 0.3

5 1 -2 1 0 -0.83333 0 0.4

6 1 -2 1 0 -0.85714 0 0.5

7 1 -2 1 0 -0.875 0 0.6

8 1 -2 1 0 -0.88889 0 0.7

9 1 -2 1 -1 -0.9 0.9 0.8

10 0.9

11 1 BC's

You start computing hereand go up

You start computing here and go down

Boundary conditions are known values

The values of s(1) and s(9) could be tricky as they were both renamed to find recurrence formulas:

s(1)=s(1)-p(1)u(1)=0-(1)(0)=0s(9)=s(9)-r(9)u(11)=0-(1)(1)=-1

s(1) and s(9) in the RHS both are zero, but not the new s(1) and s(9)

Page 13: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 13

q = 2; r = -1;

TM=full(gallery('tridiag',n,p,q,r))

fprintf('\n\n');

% Option 2 n = 5 ; nOnes = ones(n, 1) ; TM = diag(2 * nOnes, 0) - diag(nOnes(1:n-1), -1) - diag(nOnes(1:n-1), 1)

fprintf('\n\n');

% Option 3 n=5; TM=toeplitz([2 -1 zeros(1, n-2)], [2 -1 zeros(1, n-2)])

OUTPUT

TM =

2 -1 0 0 0

-1 2 -1 0 0

0 -1 2 -1 0

0 0 -1 2 -1

0 0 0 -1 2

Tridiagonal solver routine for MATLAB

If you want to be loyal to Thomas algorithm, here you have a shared code:

function u = tridiag(a,b,c,r,N) ;

% Function tridiag:

% Inverts a tridiagonal system whose lower, main and upper diagonals

% are respectively given by the vectors a, b and c. r is the right­hand

% side, and N is the size of the system. The result is placed in u.

% (Adapted from Numerical Recipes, Press et al. 1992)

if (b(1)==0)

fprintf(1,'Reorder the equations for the tridiagonal solver...')

pause

Page 14: Thomas Algorithm for Tridiagonal Matrix - MATLAB€¦ · Thomas Algorithm for Tridiagonal Matrix Special Matrices Some matrices have a particular structure that can be exploited to

P a g e | 14

end

beta = b(1) ; u(1) = r(1)/beta ;

% Start the decomposition and forward substitution

for j = 2:N

gamma(j) = c(j­1)/beta ;

beta = b(j)­a(j)*gamma(j) ;

if (beta==0)

fprintf(1,'The tridiagonal solver failed...')

pause

end

u(j) = (r(j)­a(j)*u(j­1))/beta ;

end

% Perform the back substitution

for j = 1:(N­1) k = N­j ;

u(k) = u(k) ­ gamma(k+1)*u(k+1) ;

end

REFERENCES

https://www.mathworks.com/help/matlab/math/operating-on-diagonal-matrices.html

http://math.stackexchange.com/questions/763909/creating-a-tridiagonal-matrix-in-

matlab

https://www.princeton.edu/~lam/tridiagonal-solver.pdf