FORTRAN 90 L e c

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc
Mechanical Engineering Dep.
AAPPPPLLIICCAATTIIOONNSS AABBOOUUTT FFUUNNCCTTIIOONN SSUUBBPPRROOGGRRAAMM
EXAMPLE
Bessel function of the first kind of order zero is defined to be the sum of the
following in finite series:
୭ ሺšሻ ൌ ͳ െ
šଶ
šସ
š଺
൅
െ
൅‫ڮ‬
ʹଶ ሺͳǨሻଶ ʹସ ሺʹǨሻଶ ʹ଺ ሺ͵Ǩሻଶ
Write a fortran 90 program to compute Jo(x) for input data of x. Using the series
until the absolute value of a term is less than tolerance value say 1e-8. Calculate
the factorial by using function subprogram. Print your result in output file.
IMPLICIT NONE
INTEGER::I,F
REAL::SUM=1.0,TERM,ERROR=1E-4,X
READ(*,*)X
OPEN (UNIT=4,FILE="RT.DAT")
I=1
DO
TERM=((-1)**(I)*X**(2.0*I))/(2.0**(2.0*I)*F(I)**2.0)
IF(ABS(TERM).LT.ERROR)EXIT
SUM=SUM+TERM
I=I+1
ENDDO
;
WRITE(4,*)"BESSEL FUNCTION=",SUM
END
INTEGER FUNCTION F(I)
IMPLICIT NONE
INTEGER::J,FACT,I
ϭ
FACT=1
IF(I==0)THEN
ELSE ;
;
F=1
DO J=1,I
FACT=FACT*J
;
ENDDO
F=FACT
ENDIF
;
END
EXAMPLE
ሺଵାଶ଴௫ሻగ
Write a fortran 90 program to form a table for ݂ሺ‫ݔ‬ሻ ൌ ‫ ‹• ݔ‬ቀ
ቁ
ଶ
over a closed interval [-1 , 1] using increments in x of 0.2. Compute f(x) by
using external function. Use format statement to print your results.
PROGRAM ER
IMPLICIT NONE
REAL::F,i
WRITE(*,4) "X","F(X)"
4 FORMAT(8X,A,9X,A)
write(*,70)"________","___________"
70 format(4x,a,3x,a)
DO I=-1,1,.2
WRITE(*,7)
I,F(I)
7 FORMAT( 2X,F9.4,3X,F10.6)
ENDDO
END
REAL FUNCTION F(I)
REAL,PARAMETER::PI=3.14159
real::I
F=i*SIN(((1.0+(20.0*i*pi/180))*PI)/2.0)
END
Ϯ
EXAMPLE
Write a fortran 90 program to read the launch angle a, the time since
launch t, and the launch velocity u, and compute the position, the velocity and
the angle with the ground by using function subprograms.
If you know that
The horizontal and vertical displacements, x and y, are computed as follows:
x = u × cos(a) × t
y = u × sin(a) × t – g.t2/2
The horizontal and vertical components of the velocity vector are computed as:
Vx = u × cos(a) x
Vy = u × sin(a) − g×t
The magnitude of the velocity vector is
V=(V2x+V2y).5
The angle between the ground and the velocity vector is
tan(è ) = Vy / Vx
PROGRAM PROJECTILE
IMPLICIT NONE
REAL, PARAMETER :: PI = 3.1415926
REAL :: a,t,u
REAL :: k,j,hor,ver,vx,vy,v,angle
degree
! direction at time in
READ(*,*) a,t,u
a =a * PI / 180.0
! convert to radian
k=vx(u,a)
j=vy(u,a,t)
WRITE(*,30) hor(u,a,t),ver(u,a,t),v(k,j),angle(k,j)
30 format(1x, 'Horizontal displacement :',2x,F9.3,/,1x, 'Vertical&
&displacement : ',2x,F9.3,/,1x, 'Resultant velocity ',2x,F9.4,/,1x,'Direction(in&
&degree) : ',2x,F8.3)
END
ϯ
real function hor(u,a,t)
implicit none
real::a,t,u
hor= u * COS(a) * t
end
real function ver(u,a,t)
implicit none
REAL, PARAMETER :: g = 9.81
real::a,t,u
ver = U * SIN(A)*t-(g*t**2)/ 2.0
end
real function vx(u,a)
implicit none
real::a,u
Vx = U * COS(A)
end
real function vy(u,a,t)
implicit none
REAL, PARAMETER :: g = 9.81
real::a,t,u
Vy = U * SIN(A) - g * T
end
real function v(k,j)
ϰ
implicit none
real::k,j
V=sqrt(k**2+j**2)
end
real function angle(k,j)
implicit none
real::k,j
angle=atan(j/k)
angle=angle*180/3.14159
end
EXAMPLE
Write a fortran 90 to locate the centroid of the shaded area shown below.
You can obtain the centroid of all these shapes from table below. If you know
σ ஺௑ത
σ ஺௒ത
that ܺത ൌ
, ܻത ൌ
compute ܺത ǡ ܻത by function subprogram.
σ஺
σ஺
part
A (in2)
1
2
3
4
120
30
-14.14
-8
X
(in)
6
14
6
12
Y (in)
5
10/3
1.273
4
implicit none
integer,parameter::n=4
real,dimension(n)::a,xb,yb
integer::i
real::xbar,ybar ,fx,fy
read(*,*)(a(i),i=1,n)
read(*,*)(xb(i),i=1,n)
read(*,*)(yb(i),i=1,n)
xbar=fx(a,xb)
ϱ
ybar=fy(a,yb)
write(*,6)xbar,ybar
6
format(3x,"the
centroid
of
&"xbar=",1x,f8.4,"in",3x,"ybar=",1x,f8.4,"in")
end
real function fx(a,xb)
implicit none
integer,parameter::n=4
real,dimension(n)::a,xb
integer::i
real::sumax,suma
do i=1,n
sumax=sumax+a(i)*xb(i)
suma=suma+a(i)
enddo
fx=sumax/suma
end
real function fy(a,yb)
implicit none
integer,parameter::n=4
real,dimension(n)::a,yb
integer::i
real::sumay,suma
do i=1,n
sumay=sumay+a(i)*yb(i)
suma=suma+a(i)
enddo
fy=sumay/suma
end
ϲ
the
shaded
are",2x,&
ϳ
Download