Lecture 5.3 - Introduction To Fortran 90-3
Lecture 5.3 - Introduction To Fortran 90-3
Fortran 90-3
Other Declarations
Define constants to be used in program:
PARAMETER (PI=3.1415927, NAME=‘UDSM’)
PARAMETER (PI4=PI/2, FLAG=.TRUE.)
these cannot be changed in assignments
can use parameters to define other parameters
Pass a function or subroutine name as an argument:
INTRINSIC SIN – the SIN function will be passed
as an argument to a subprogram (subroutine or
function)
EXTERNAL CP260 – the CP260 function defined
in a FUNCTION subprogram will be passed as an
argument to another subprogram
CP 260 Lectures - Fortran 2
Example 8
program projectile
implicit none ! To be followed by Variable declaration
! define constants
real, parameter :: g = 9.82 real :: a, t, u, x, y, pi, g
real, parameter :: pi = 3.1415927 real :: theta, v, vx, vy
real :: a, t, u, x, y parameter (g = 9.82)
real :: theta, v, vx, vy parameter (pi = 3.1415927)
! Read values for a, t, and u from terminal
write(*,*) ‘Enter values of a, t, u’
read(*,*) a, t, u
! convert angle to radians
a = a * pi / 180.0
x = u * cos(a) * t
y = u * sin(a) * t – 0.5 * g * t * t
vx = u * cos(a)
vy = u * sin(a) - g * t
v = sqrt(vx * vx + vy * vy)
theta = atan(vy / vx) * 180.0 / pi
write(*,*) ’x: ’, x, ’ y: ’, y
write(*,*) ’v: ’, v, ’ theta: ’, theta
end program projectile CP 260 Lectures - Fortran 3
Name Intrinsic FunctionsAction
Common
ABS(A) absolute value of any A
ACOS(X) inverse cosine in the range (0,p) in radians
AIMAG(Z) imaginary part of Z
AINT(X [,KIND]) truncates fractional part towards zero, returning real
ANINT(X [,KIND]) nearest integer, returning real
ASIN(X) inverse sine in the range (-p/2,p/2) in radians
ATAN(X) inverse tangent in the range (-p/2,p/2) in radians
ATAN2(Y,X) inverse tangent of Y/X in the range (-p,p) in radians
CMPLX(X [,Y][,KIND] converts to complex X+iY; if Y is absent, 0 is used
CONJG(Z) complex conjugate of Z
COS(W) cosine of argument in radians
COSH(X) hyperbolic cosine
EXP(W) exponential function
FLOOR(X) greatest integer less than X
INT(A [,KIND]) converts to integer, truncating (real part) towards zero
KIND(A) integer function, returns the KIND of the argument
LOG(W) natural logarithm: if W is real it must be positive
(a)
(b)
Alternative 1 Alternative 2
DO 35 i=1, 100, 2 DO 35 i=1, 100, 2
WRITE (*, 50) i 35 WRITE (*, 50) i
35 Continue 50 format('I = ',i4)
50 format('I = ',i4)
I = 1
I = 3
…
I = 99
I = 1
I = 3
…
I = 99
CP 260 Lectures - Fortran 17
DO Construct -3
Implied DO Loop
The implied DO loop is a shorthand notation
useful for the following:
specify iteration of part of an I/O list
initialize an array
transfer part of an array
transfer array items in a sequence different from
the order of subscript progression
Cuts back on the number of lines of code required