Gauss Jordan Method – Inverse of a Matrix | FORTRAN 95

Fortran Programming - Bottom Science
PROGRAM gauss_jordan_inverse

IMPLICIT NONE
REAL::A(20,20),t
INTEGER::i,j,n,k

PRINT *, "============================================"
PRINT *, "Program to find the inverse of a matrix using Gauss Jordon method - [BY - www.BottomScience.com]"
PRINT *, "============================================"

PRINT *,'GAUSS JORDAN'
PRINT *,'NO. OF ROWS'
READ(*,*)n

PRINT *,'ENTER ELEMENTS'
READ(*,*)((A(i,j),j=1,n),i=1,n)

PRINT *,'YOUR MATRIX - '
DO i=1,n
  write(*,*)(A(i,j),j=1,n)
END DO

!CREATING AN IDENTITY MATRIX
DO i=1,n
DO j=1,n
  IF(i==j) THEN
    A(i,j+n)=1
  ELSE
    A(i,j+n)=0
  END IF
END DO
END DO

!CONVERTING IN DIAGONAL
DO i=1,n !I->ROWS
  DO j=1,n !J-> COLUMNS
    IF(i .ne. j) THEN
        t=A(j,i)/A(i,i)
        DO k=1,2*n
          A(j,k)=A(j,k)-(A(i,k)*t)
        END DO
    END IF
   END DO
END DO


DO i=1,n
DO j=n+1,2*n
  A(i,j)=A(i,j)/A(i,i)
END DO
END DO

PRINT *,'FINAL MATRIX - '
DO i=1,n
  write(*,*)(A(i,j),j=1,n)
END DO

PRINT *,'INVERSE MATRIX - '
DO i=1,n
  write(*,*)(A(i,j),j=n+1,2*n)
END DO

END PROGRAM

Leave a Reply

Your email address will not be published.