Created
October 27, 2019 07:52
-
-
Save md-abdul-halim-rafi/437f369d487bf84dec13f10305f7790f to your computer and use it in GitHub Desktop.
Jacobi and Gauss-Seidal System solving using Fortran
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0. 0. 0. 0. 0. | |
4. 1. 1. 0. 1. 6. | |
-1. -3. 1. 1. 0. 6. | |
2. 1. 5. -1. -1. 6. | |
-1. -1. -1. 4. 0. 6. | |
0. 2. -1. 1. 4. 6. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program jacobi-seidal | |
implicit none | |
integer, parameter :: n = 5 | |
integer :: i, j, k | |
real :: a(n,n), x(n), b(n), dom, tb1(0:50,50), tb2(0:50,50), s(n), tol | |
logical :: stp | |
open(unit = 2, file= 'input.txt') | |
tol = 0.00001 | |
read(2,*)x(:) | |
do i = 1, n | |
read(2,*)(a(i,j), j = 1, n), b(i) | |
tb1(0,i)=x(i) | |
end do | |
do i = 1, n | |
dom = 0 | |
do j = 1, n | |
if(i /= j) then | |
dom = dom + abs(a(i,j)) | |
end if | |
end do | |
if(dom > abs(a(i,i))) then | |
write(*,*)"Matrix A is not Dominant" | |
stop | |
end if | |
end do | |
write(*,*)"Matrix A is Dominant" | |
k = 0 | |
do | |
k = k + 1 | |
do i = 1, n | |
s(i) = 0 | |
do j = 1,n | |
if(i /= j) then | |
s(i) = s(i) + x(j)*a(i,j) | |
end if | |
end do | |
end do | |
do i = 1, n | |
x(i) = (b(i)-s(i))/a(i,i) | |
tb1(k,i) = x(i) | |
end do | |
stp=.true. | |
do i = 1, n | |
if(abs(tb1(k,i)-tb1(k-1,i)) > tol) then | |
stp=.false.; exit | |
end if | |
end do | |
if(stp)exit | |
end do | |
write(*,*)"Jacobi Iterative process stop after ",k,"th iteration." | |
write(*,*)" Inter. No. "," X1 "," X2 "," X3 "," X4 "," X5 " | |
write(*,*)" ----------"," ----------- "," ----------- "," -----------"," -----------"," -----------" | |
do i=0,k | |
write(*,*)i,(tb1(i,j),j=1,n) | |
end do | |
do i=1,n !! for initialization | |
x(i)=tb1(0,i) | |
tb2(0,i)=tb1(0,i) | |
end do | |
k = 0 | |
do | |
k = k + 1 | |
do i = 1, n | |
s(i) = 0 | |
do j = 1,n | |
if(i /= j) then | |
s(i) = s(i) + x(j)*a(i,j) | |
x(i) = (b(i)-s(i))/a(i,i) | |
tb2(k,i) = x(i) | |
end if | |
end do | |
end do | |
stp=.true. | |
do i = 1, n | |
if(abs(tb2(k,i)-tb2(k-1,i)) > tol) then | |
stp=.false.; exit | |
end if | |
end do | |
if(stp)exit | |
end do | |
write(*,*)"Jacobi Iterative process stop after ",k,"th iteration." | |
write(*,*)" Inter. No. "," X1 "," X2 "," X3 "," X4 "," X5 " | |
write(*,*)" ----------"," ----------- "," ----------- "," -----------"," -----------"," -----------" | |
do i=0,k | |
write(*,*)i,(tb2(i,j),j=1,n) | |
end do | |
end program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment