Skip to content

Instantly share code, notes, and snippets.

@md-abdul-halim-rafi
Last active October 20, 2019 13:36
Show Gist options
  • Save md-abdul-halim-rafi/652012651675e0a6bcdd0279d885e363 to your computer and use it in GitHub Desktop.
Save md-abdul-halim-rafi/652012651675e0a6bcdd0279d885e363 to your computer and use it in GitHub Desktop.
Gaussian Elimination using Fortran
program ques_05
implicit none
integer, parameter :: n = 4 !No of variables
integer :: i, j
real :: a(n,n+1), x(n), s
!TODO: input all coefficient in 'a6q5_in.txt'
open(unit = 2, file = 'input.txt')
do i = 1, n
read(2,*)(a(i,j), j = 1, n+1)
end do
write(*,'(/, "Augmented Matrix is : ", /)')
do i = 1, n
write(*,'(4(5(f10.2,3x)))')(a(i,j), j = 1, n+1)
end do
do j = 1, n
do i = j+1, n
a(i,:) = a(i,:) - a(j,:) * a(i,j) / a(j,j)
end do
end do
write(*,'(/, "After Gaussian Elimination : ", /)')
do i = 1, n
write(*,'(4(5(f10.2,3x)))')(a(i,j), j = 1, n+1)
end do
do i = n, 1, -1
s = a(i,n+1)
do j = i+1, n
s = s - a(i,j) * x(j)
end do
x(i) = s / a(i,i)
end do
write(*,'(/, "X : ",2/, 4(F10.2, /))')(x(i), i = 1, n)
end program
2 1 -1 4 19
-1 -2 1 2 -3
2 4 2 1 25
-1 1 -1 -2 -5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment