-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexercise.f90
49 lines (35 loc) · 1.32 KB
/
exercise.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
program exercise
! Tri-diagonal matrix problem via Thomas' algorithm
! See https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
! The following values are used as a test:
! a = [1.0, 1.0, 1.0] - lower diagonal
! b = [4.0, 4.0, 4.0, 4.0] - diagonal
! c = [2.0, 2.0, 2.0] - upper diagonal
! d = [1.0, 4.0, 5.0, 6.0] - rhs
! which should give a solution approx. x = [-0.195, 0.890, 0.317, 1.42]
use iso_fortran_env
implicit none
integer, parameter :: mykind = real32
integer, parameter :: nmax = 4
real (mykind), dimension(nmax) :: b = [4.0, 4.0, 4.0, 4.0]
real (mykind), dimension(2:nmax) :: a = [1.0, 1.0, 1.0]
real (mykind), dimension(1:nmax-1) :: c = [2.0, 2.0, 2.0]
real (mykind), dimension(nmax) :: d = [1.0, 4.0, 5.0, 6.0]
real (mykind), dimension(nmax) :: x
real (mykind) :: w
integer :: i
! Set up the matrix here: all elements; diagonal elements, then
! off-diagonal elements
! Solve via Thomas' algorithm
! Note b(:) and d(:) are destroyed
do i = 2, nmax
w = a(i) / b(i-1)
b(i) = b(i) - w*c(i-1)
d(i) = d(i) - w*d(i-1)
end do
x(:) = d(:)/b(:)
do i = nmax-1, 1, -1
x(i) = (d(i) - c(i)*x(i+1))/b(i)
end do
print *, "Solution ", x(:)
end program exercise