-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgelm.f
65 lines (65 loc) · 1.76 KB
/
gelm.f
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
c Gauss Elimination
c Comment out the lines b/w "Pivoting" & "End of Pivoting" to turn off pivoting
program gelm
implicit none
integer :: i, j, k, m, n, p, q
double precision :: u, s, tol, maxv, tmp
double precision, allocatable :: a(:, :), x(:)
tol = 1e-5
print *, "Enter the number of variables :"
read *, n
allocate (a(n, n + 1), x(n))
print *, "Enter the elements of the matrix :"
read *, ((a(i, j), j = 1, n + 1), i = 1, n)
print *, "The original matrix is :"
do i = 1, n
print *, (a(i, j), j = 1, n + 1)
end do
do k = 1, n - 1
! Pivoting
maxv = abs(a(k, k))
p = k
do m = k + 1, n
if (abs(a(m, k)) .gt. maxv) then
maxv = abs(a(m, k))
p = m
end if
end do
if (maxv .lt. tol) then
print '(A)', "Ill conditioned equations!"
stop
end if
if (p .ne. k) then
do q = k, n + 1
tmp = a(k, q)
a(k, q) = a(p, q)
a(p, q) = tmp
end do
end if
! End of Pivoting
do i = k + 1, n
u = a(i, k) / a(k, k)
do j = k, n + 1
a(i, j) = a(i, j) - u * a(k, j)
end do
end do
end do
print *, "The upper triangular matrix is :"
do i = 1, n
print *, (a(i, j), j = 1, n + 1)
end do
! The last variable
x(n) = a(n, n + 1) / a(n, n)
! The other variables
do i = n - 1, 1, -1
s = 0
do j = i + 1, n
s = s + a(i, j) * x(j)
end do
x(i) = (a(i, n + 1) - s) / a(i, i)
end do
print *, "The solution is :"
do i = 1, n
print *, x(i)
end do
end