Gauss 2
Gauss 2
Tridiagonal Systems
Tuesday, 14 January 20
solving ordinary and partial differential equations. It is advantageous to develop computer
Tridiagonal systems
codes specifically designed for such linear systems, since they reduce the amount of storage
used.
In many
Of problems
practical the linear system
importance to be
is the solved is tridiagonal.
tridiagonal system. Here, all
aij the
= 0,nonzero
if | i elements
j | 2 in
the coefficient matrix must be on the main diagonal or on the two diagonals just above and
below the main diagonal (usually called superdiagonal and subdiagonal, respectively):
⎡ ⎤⎡ ⎤ ⎡ ⎤
d1 c1 x1 b1
⎢ a1 d2 c2 ⎥ ⎢ x2 ⎥ ⎢ b2 ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ a2 d3 c3 ⎥ ⎢ x3 ⎥ ⎢ b3 ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ .. .. .. ⎥ ⎢ .. ⎥ ⎢ .. ⎥
⎢ . . . ⎥⎢. ⎥ ⎢. ⎥
⎢ ⎥⎢ ⎥=⎢ ⎥ (1)
⎢ ai−1 di ci ⎥ ⎢ xi ⎥ ⎢ bi ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ .. .. .. ⎥⎢. ⎥ ⎢. ⎥
⎢ . . . ⎥ ⎢ .. ⎥ ⎢ .. ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ an−2 dn−1 cn−1 ⎦ ⎣ xn−1 ⎦ ⎣ bn−1 ⎦
an−1 dn xn bn
(All elements not in the displayed diagonals are 0’s.) A tridiagonal matrix is characterized
by the condition ai j = 0 if |i − j| ! 2. In general, a matrix is said to have a banded structure
if there is an integer k (less than n) such that ai j = 0 whenever |i − j| ! k.
The storage requirements for a banded matrix are less than those for a general matrix
of the same size. Thus, an n × n diagonal matrix requires only n memory locations in the
computer, and a tridiagonal matrix requires only 3n − 2. This fact is important if banded
matrices
Tuesday, 14 January 20of very large order are being used.
phase and the back substitution phase are incorporated in the procedure, and no pivoting is
Tridiagonal systems
used; that is, the pivot equations are those given by the natural ordering {1, 2, . . . , n}. Thus,
naive Gaussian elimination is used.
Steps in Elimination
In step 1, we subtract a1 /d1 times row 1 from row 2, thus creating a 0 in the a1 position.
Only the entries d2 and b2 are altered. Observe that c2 is not altered. In step 2, the process
is repeated, using the new row 2 as the pivot row. Here is how the di ’s and bi ’s are altered
Initial steps: a1
in each step: 0 ⇣ ⌘
a1 ⎧ % &
d2 d2 c 1⎪ a1
⇣ ⌘ ⎨ d2 ← d2 − d c1
d 1 ⎪
a1 1
b2 b2 b 1⎪
% &
d1 ⎪ a 1
⎩ b2 ← b2 − b1
c2 c2 d1
In general, we obtain
⇣⎧
⎪
⌘ %
a
&
General di di ⎪
⎪
⎨
aid 1← d −
i c i
i 1
i−1
ci−1
⇣ di 1⌘ di−1
a % (2 & i n)
bi bi ⎪
⎪
⎪
i 1
b i 1 ai−1
⎩i bi1 ← bi −
d bi−1 (2 ! i ! n)
di−1
At the end of the forward elimination phase, the form of the system is as follows:
⎡ ⎤⎡ ⎤ ⎡ ⎤
Resulting system: d 1 c 1 x 1 b1
⎢ d2 c2 ⎥ ⎢ x 2 ⎥ ⎢ b2 ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ d3 c3 ⎥ ⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ x 3 ⎥ ⎢ b3 ⎥
⎢ .. .. ⎥ ⎢ .. ⎥ ⎢ .. ⎥
⎢ . . ⎥ ⎢ ⎥ ⎢ ⎥
⎢ ⎥⎢. ⎥ = ⎢. ⎥
⎢ di ci ⎥ ⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ xi ⎥ ⎢ bi ⎥
⎢ .. .. ⎥⎢. ⎥ ⎢. ⎥
⎢ . . ⎥ ⎢ .. ⎥ ⎢ .. ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ dn−1 cn−1 ⎦ ⎣ xn−1 ⎦ ⎣ bn−1 ⎦
dn xn bn
Of course, the bi ’s and di ’s are not as they were at the beginning of this process, but the ci ’s
are. The back substitution phase solves for xn , xn−1 , . . . , x1 as follows:
bn
xn ←
Tuesday, 14 January 20 dn
⎪
⎪ ai−1
⎨ di ← di −
⎪ ci−1
%
di−1
& Tridiagonal systems
⎪ a
(2 Back
i n) Substitution
⎪
⎪ i−1
⎩ bi ← bi − bi−1 ! !
di−1
the end of the forward elimination phase, the form of the system is as follows:
⎡ ⎤⎡ ⎤ ⎡ ⎤ bn
d 1 c1 x1 b1 xn dn
1
⎢
⎢ d2 c2 ⎥ ⎢ x 2 ⎥ ⎢ b2 ⎥
⎥⎢ ⎥ ⎢ ⎥ xn 1 dn 1
(bn 1 cn 1 xn )
⎢ d3 c3 ⎥ ⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ x 3 ⎥ ⎢ b3 ⎥
⎢ .. .. ⎥ ⎢ .. ⎥ ⎢ .. ⎥
⎢ . . ⎥⎢. ⎥ ⎢. ⎥
⎢ ⎥⎢ ⎥=⎢ ⎥
⎢ di ci ⎥ ⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ xi ⎥ ⎢ bi ⎥
⎢ .. .. ⎥⎢. ⎥ ⎢. ⎥
⎢ . . ⎥ ⎢ . ⎥ ⎢ . ⎥
⎢ ⎥⎢. ⎥ ⎢. ⎥
⎣ dn−1 cn−1 ⎦ ⎣ xn−1 ⎦ ⎣ bn−1 ⎦
dn xn bn
se, the bi ’s and di ’s are not as they were at the beginning of this process, but the ci ’s
e back substitution phase solves for xn , xn−1 , . . . , x1 as follows:
General scheme bn
xn ←
dn
11
xxn−1
i ← (bi (bn−1
ci−
xi+1
cn−1),
xn ) (i = n1 , n 2, . . . , 1)
ddi n−1
we obtain
1
xi ← (bi − ci xi+1 ) (i = n − 1, n − 2, . . . , 1)
di
procedure Tri for a tridiagonal system, we use single-dimensioned arrays (ai ), (di ),
for the diagonals in the coefficient matrix and array (bi ) for the right-hand side,
e the solution in array (xi ).
Tuesday, 14 January 20
Tridiagonal systems
Sample Code
program main
subroutine tri(n,a,d,c,b,x)
real, dimension (10):: a,d,c,b,x
integer :: n
integer ::n = 10
real, dimension(n) :: a,d,c,b
real, dimension(n) :: x
do i=1,n
integer ::i
d(i) = 2.0
real :: xmult
a(i) = 0.5
do i = 2,n
c(i) = 0.5
xmult = a(i-1)/d(i-1)
b(i) = 3.0
d(i) = d(i) - xmult*c(i-1)
end do
b(i) = b(i) - xmult*b(i-1)
b(1) = 2.5
end do
b(n) = 2.5
x(n) = b(n)/d(n)
call tri(n,a,d,c,b,x)
do i = n-1,1,-1
print *, "subroutine tri"
x(i) = (b(i) - c(i)*x(i+1))/d(i)
print *,(x(i),i=1,n)
end do
end subroutine tri
end program main
Tuesday, 14 January 20
Example:
One-Dimensional Poisson Equation
d2 ⇢(x)
dx2 = ✏0
x0 = 0, xn+1 = 1
Using Finite difference scheme, divide the xi = ih
domain into n+1 sections: 1
h = n+1
u0 = un+1 = 0
ui+1 + ui 1 2ui
= fi , for i = 1, . . . , n, fi = f (xi )
h2
Tuesday, 14 January 20
= 0 to xn+1 = 1. The step length or spacing is defined as h = 1/(n + 1). We have th
ndary conditions v0 = vn+1 = 0. We approximate
Example: the second derivative of u with
One-Dimensional
vi+1 + vi−1 − 2vi Poisson Equation
− = fi for i = 1, . . . , n,
h2
Rewriting in matrix form:
i = f (xi ). Show that you can rewrite this equation as a linear set of equations of t
Av = b
Assume: f (x) = 3(x + x2 )ex Av = b̃,
A is an n × n tridiagonal matrix which we rewrite as
Question: What is the form of matrix?
⎛ ⎞
2 −1 0 . . . . . . 0
⎜ −1 2 −1 0 . . . . . . ⎟
⎜ ⎟
⎜ 0 −1 2 −1 0 . . . ⎟
A=⎜
⎜
⎟
⎟
⎜ . . . . . . . . . . . . . . . ⎟
⎝ 0 ... −1 2 −1 ⎠
0 ... 0 −1 2
= h2Assignment:
fi . Compute the potential o each point between 0 and 1, and compare with
aseanalytical
we will solution.
assume Takethat
two different values
f (x) = (3x + xfor n=
2 )e x , 10,
and 20 keep
etc. the same interval and bounda
ons. Then the above differential equation has an analytic solution given by u(x)
ex (convince yourself that this solution:
Analytical is correctu(x) x(1 x)exthe solution in the Poiss
by =inserting
n). We will compare our numerical solution with this analytic result in the ne
Tuesday, 14 January 20