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.
Control Constructs
These change the sequential execution order. The main ones are:
Conditionals (IF etc.)
Loops (DO etc.)
Switches (SELECT/CASE etc.) etc.)
IF STATEMENT
Single Statement IF
IF (logical expression) simple statement
EX.
IF (X < A) X = A
Block IF Statement
IF (logical expression) THEN
block of statements
ELSE
block of statements
ENDIF
EX.
IF (X /= 0.0) THEN
X = 1.0/A
ϭ
Y = EXP(-A)
ELSE
X=A
Y = EXP(A)
END IF
Omitting the ELSE
The ELSE and its block can be omitted
IF (logical expression) THEN
Block of statement
END IF
EX.
IF (X > Maximum) THEN
X = Maximum
END IF
Including ELSEIF Blocks
IF (X < 0.0) THEN
! This is tried first
X=A
ELSEIF (X < 2.0) THEN
! This second
X = A + (B-A)*(X-1.0)
ELSEIF (X < 3.0) THEN
! And this third
X = B + (C-B)*(X-2.0)
ELSE
! This is used if none succeed
X=C
END IF
Nested if statements
If statements can be nested in several levels. To ensure readability, it is
important to use proper indentation. Here is an example:
if (x .GT. 0) then
if (x .GE. y) then
Ϯ
write(*,*) 'x is positive and x = y'
else
write(*,*) 'x is positive but x < y'
endif
elseif (x .LT. 0) then
write(*,*) 'x is negative'
else
write(*,*) 'x is zero'
endif
Example:
Write a Fortran program to calculate the value of function f(x); where
x= [-5, 0, 5]. Use suitable format statement.
܎ሺ‫ܠ‬ሻ ൌ ‫ ିܖ܉ܜ‬ሺ‫ܠ‬ሻ ൅ ‫ ܠ ܠ܍‬൐ Ͳ
܎ሺ‫ܠ‬ሻ ൌ ૙‫ ܠ‬ൌ ૙
܎ሺ‫ܠ‬ሻ ൌ ξ‫ ܠ‬૛ ൅ ૛‫ ܠ‬൏2
20
Program value_of_function
Implicit none
Real :: y,x
Read(*,*) x
if(x>0) then
y=Atan (x) +exp(x)
write(*,20) x, y
elseif (x<2) then
y=sqrt (x**2+2)
write(*,20) x, y
else
y=0
write(*,20) x, y
format (2x,'at x=',1x,f3.1,3x,'f(x)=',1x,f7.4)
endif
end
Example:
Write a program that, after accepting a pair of nonzero coordinates x and y,
prints the number of the quadrant in which the point (x,y) lies. Assume that the
ϯ
quadrants are numbered counterclockwise. Beginning with the upper-right
quadrant.
Program coordinates
Implicit none
Real :: x,y
Write(*,*) "Enter the first coordinate:"
Read(*,*) x
Write(*,*) "Enter the Second coordinate:"
Read(*,*) y
If(x*y>0)then
If(x>0) then
Quadr=1
Else
Quadr=3
Endif
Elseif(y>0)then
Quadr=2
Else
Quadr=4
Endif
Write(*,20) quadr
20 format (2x,'quadrant=',1x,i3)
End
Example:
Write a fortran 90 program to describe an internal flow of a fluid, which
have velocity (v), density (r) and dynamic viscosity (m), through a pipe with a
diameter (d).
Program flow
Implicit none
Real:: v, r, m, d, re
Read(*,*) v, r, m, d
Re=(r*v*d)/m
If (Re <=2200) write (*,*) 'flow is laminar'
If(Re>2200.and.Re<4000)then
Write(*,*) 'flow is transient'
Else
Write(*,*) 'flow is turbulent'
Endif
end
ϰ
Example: (HW)
Suppose a movie theater has 36 seats, laid out in a rectangular six rows by
six seats. Let the seats be priced as indicated
1
1
1
1
1
1
1
2
2
2
2
1
1
2
5
5
2
1
1
2
5
5
2
1
1
2
2
2
2
1
1
1
1
1
1
1
Write a program that when given the row number and the seat number,
prints the price of a ticket.
Example: (HW)
Given a quadratic equation as follows:
ܽ‫ ݔ‬ଶ ൅ ܾ‫ ݔ‬൅ ܿ ൌ Ͳ
if b*b-4*a*c is non-negative, the roots of the equation can be solved with the
following formulae:
”‘‘–ʹ ൌ
”‘‘–ͳ ൌ
ଵ
ଶୟ
൫െ„ െ ξ„ ଶ െ Ͷƒ…൯
ͳ
ቀെ„ ൅ ඥ„ ଶ െ Ͷƒ…ቁ
ʹƒ
Write a program to read in the coefficients a, b and c, and compute and display the
roots. If the discriminate b*b - 4*a*c is negative, the equation has complex root.
Thus, this program should solve the equation if the discriminate is non-negative
and show a message otherwise.
ϱ
Download