L e c t

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineeriin
ng
Year : Second B.Sc.
Mechanical Engineering Dep.
Inverse Matrices
Iff A is a square n×n matrix, sometimes there exists A-1 (“A inverse”)
”) such that
A A-1= I and A-1 A = I
Some properties of inverse A:
1. A -1 exists only if A is nonsingular - that is | A | ≠ 0
Note: | A | is defined as the determinant of A.
2. If A -1 exists then it is unique.
To find the inverse of matrix A, using Gauss-Jordan
Jordan elimination, we must
find a sequence of elementary row operations that reduces A to the identity and
then perform the same operations on In to obtain A-1.
Example: Find the inverse of the matrix A by using Gauss-Jordan meethod
ϭ
Solution:
Begin with the 3× 6 matrix whose left half is A and whose right half is the identity
matrix.







 Thus,
hus, the inverse of the given matrix is
Ϯ
A fortran 90 program to find the inverse of matrix by Gauss-Jordan method
program inverse_matrix
implicit none
integer,parameter::n=3
real,dimension(n,n)::a,a1
integer::i,j,l,irow,k
real::z,max
open(unit=5,file="rr.dat")
data a/2,1,1,1,2,1,1,1,2/
data a1/1,3*0,1,3*0,1/
do i=1,n
write(5,60)(a(i,j),j=1,n) , (a1(i,j),j=1,n)
60 format(2x,3(2x,f9.5),4x,3(2x,f9.5))
enddo
write(5,*)"***************"
do i=1,n !this is the big loop over all the columns of a(n,n)
!in case the entry a(i,i)is zero, we need to find good pivot
max=a(i,i)
do j=i,n
if(a(j,i).gt.max) then
max=a(j,i)
irow=j
;
endif
;
enddo
!interchange lines i with irow for both a & a1
if(max.gt.a(i,i)) then
do k=1,n
z=a(i,k)
a(i,k)=a(irow,k)
a(irow,k)=z
z=a1(i,k)
a1(i,k)=a1(irow,k)
a1(irow,k)=z
enddo
endif
ϯ
!divided all elements of a & a1 by a(i,i)
z=a(i,i)
do j=1,n
a(i,j)=a(i,j)/z
a1(i,j)=a1(i,j)/z
enddo
!make zero all entries in column a(j,i) & a1(j,i)
do j=i+1,n
z=a(j,i)
do k=1,n
a(j,k)=a(j,k)-z*a(i,k)
a1(j,k)=a1(j,k)-z*a1(i,k)
enddo
enddo
enddo
!subtract appropiate multiple of row j from j-1
do i=1,n-1
do j=i+1,n
z=a(i,j)
do l=1,n
a(i,l)=a(i,l)-z*a(j,l)
a1(i,l)=a1(i,l)-z*a1(j,l)
enddo
;
enddo
;
enddo
do i=1,n
write(5,60)(a(i,j),j=1,n) , (a1(i,j),j=1,n)
enddo
;
end
OUTPUT
2.00000 1.00000
1.00000 2.00000
1.00000 1.00000
***************
1.00000 .00000
.00000 1.00000
.00000 .00000
1.00000
1.00000
2.00000
.00000
.00000
1.00000
ϰ
1.00000 .00000 .00000
.00000 1.00000 .00000
.00000 .00000 1.00000
.75000 -.25000 -.25000
-.25000 .75000 -.25000
-.25000 -.25000 .75000
Download