FORTRAN 90 L e c

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Mechanical Engineering Dep.
Year : Second B.Sc.
QGB
Write afortran 90 program to find the inverse of matrix A by using subroutine program,
then check if the inverse matrix that you obtained is true or not in another subroutine program.
Print your result in out put file.
ͳ
ͳ െ͵
A=൥െͳ ʹ
Ͳ൩
ͳ െͳ ͳ
program inverse_and_check_the _inverse_matrix
implicit none
integer,parameter::n=3
real,dimension(n,n)::a,a1,c,f,e
integer::i,j
data a/1,-1,1,1,2,-1,-3,0,1/
data a1/1,3*0,1,3*0,1/
open (unit=2,file="sr2.dat")
do i=1,n
do j=1,n
e(i,j)=a(i,j)
enddo
;
enddo
CALL INVER (a,a1,c)
write(2,40)((c(i,j),j=1,n),i=1,n)
40 format(3(f10.6))
!check if matrix c is true inverse of matrix a
call multi(c,e,f)
write(2,40)((f(i,j),j=1,n),i=1,n)
end
1
subroutine inver(a,a1,c)
implicit none
integer,parameter::n=3
real,dimension(n,n)::a,a1,c
integer::i,j,l,k
real::z
!divided all elements of a & a1 by a(i,i)
do i=1,n
!this is the big loop over all the columns of a(n,n)
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
2
do j=1,n
c(i,j)=a1(i,j) ; enddo ; enddo ; end
subroutine multi(c,e,f)
integer,parameter::n=3
real,dimension(n,n)::e,c,f
integer::i,j,k
do i=1,n
do j=1,n
f(i,j)=0
do k=1,n
f(i,j)=f(i,j)+c(i,k)*e(k,j)
enddo ; enddo ; enddo ; end
QGA
Write a fortran 90 program to find the temperature distribution at point 1 , 2, and 3 in rod
AB as shown below by using inverse matrix method if you know that
Ti+1 - 2Ti + Ti-1 = 0
50 oC
35 oC
1
2
3
AT POINT 1
T2- 2T1 + 50 = 0
T2- 2T1 = -50
AT POINT 2
T3- 2T2 + T1 = 0
AT POINT3
35- 2T3 + T2 = 0
-2T3+T2 = -35
െʹ ͳ
Ͳ ܶଵ
െͷͲ
൥ ͳ െʹ ͳ ൩ ൥ܶଶ ൩ =൥ Ͳ ൩
Ͳ
ͳ െʹ ܶଷ
െ͵ͷ
3
implicit none
integer,parameter::n=3
real,dimension (n)::b,T
real,dimension(n,n)::a,a1
integer::i,j,k,l
real::z
data a/-2,1,0,1,-2,1,0,1,-2/
data a1/1,3*0,1,3*0,1/
data b/-50,0,-35/
!divided all elements of a & a1 by a(i,i)
do i=1,n
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)
4
a1(i,l)=a1(i,l)-z*a1(j,l)
enddo
enddo
enddo
do i=1,n
write(*,60)(a(i,j),j=1,n) , (a1(i,j),j=1,n)
60 format(2x,3(f10.6),10x,3(f10.6))
enddo
print*,"********************"
CALL MULTI (a1,b,T)
do i=1,n
print 10,"T",i,"=",T(i)
enddo
10 format(2x,a,i1,a,f10.5)
end
subroutine multi(a1,b,T)
implicit none
integer,parameter::n=3
real,dimension (n)::b,T
real,dimension(n,n)::a1
integer::i,j,k
do i=1,n
do j=1,n
T(i)=0
do k=1,n
T(i)=T(i)+a1(i,k)*b(k)
Enddo ; enddo ; enddo
end
5
Download