11 - 2/4/2000ame 150 l program control subroutines and functions
TRANSCRIPT
11 - 2/4/2000 AME 150 L
AME 150 L
Program Control
Subroutines and Functions
11 - 2/4/2000 AME 150 L
Program Control - Loops
• Powerful numerical tool - SERIES– Polynomials or Power Series
– Infinite Series of Generalized Functions
2 30 1 2 3
0
( ) ...
( )
NN
Ni
ii
f x c c x c x c x c x
f x c x
0
( ) ( )k kk
g x c x
11 - 2/4/2000 AME 150 L
Arrays of Variables• The use of indexed or subscripted variables
can be treated in one of two waysa) Create different variables
c0 C0, c1 C1, … , cN CN
b) Use a formal array structureREAL, dimension(0:12) :: Cthen c0 C(0), c1 C(1), … , cN C(N)
Size of an array is either a fixed number, a parameter, (or an argument or dynamic)
11 - 2/4/2000 AME 150 L
Declaring Arrays
• Two equivalent methodsa) REAL, dimension(-2:5) :: a,b,cthe three arrays a,b,s all have the same dimension (-2:5)
b) REAL :: a(-2:5), B(15), c(0:5)makes it possible to use one declaration for arrays of different sizes
11 - 2/4/2000 AME 150 L
Recursion• re·cur·sion (r-kûrzhn)
n. Mathematics – An expression, such as a polynomial, each term of which is
determined by application of a formula to preceding terms.
– A formula that generates the successive terms of a recursion.
• [Late Latin recursi, recursin- a running back, from Latin recursus, past participle of recurrere, to run back; see recur.]
• other definition 1, definition 2
11 - 2/4/2000 AME 150 L
Recursion (Examples)
• Calculate xn
x = {set value of x}
x_to_n = 1
…
x_to_n = x * x_to_n
1st time x_to_n=x,
2nd time x_to_n=x*x,
etc.
• Calculate (-1)n
m1n = 1
…
m1n = - m1n
every time statement is executed, m1n changes sign +1, -1, +1
11 - 2/4/2000 AME 150 L
Loops• To execute the same set of instructions
repeatedly
• Most common structure DO … END DO– Loop can be counted (original DO loop)– Loop can be conditional (DO WHILE)– Loop can be Infinite (but broken by a condition
or a test)
• DO may have a label
11 - 2/4/2000 AME 150 L
Counted DO Loop• Syntax:
[label:]DO ctr = init, fin [, incr]
…Fortran statements
END DO [label]
If label is used, it is followed by one colon :
ctr is a variable, and must be declared(It is preferred that ctr be an integer)
If incr is omitted, it is assumed to be +1
11 - 2/4/2000 AME 150 L
Counted DO (continued)• The DO loop is executed exactly
largest of [(fin-init)/incr, 0*] times
• DO loops can count backwards, but increment must be negative
• DO loops may have ctr as a REAL variable, but practice is discouraged
• DO index (ctr) can be used as an index of an array, or in normal expressions
11 - 2/4/2000 AME 150 L
Trivial example of a Loop
• Sum of Integers, Program fragmentINTEGER :: n, i, sum
n = {read in some value for n}
sum = 0 !Initialize variable
loop: DO I = 1, n
sum = sum + I
END DO loop
WRITE(*,*)n,sum
11 - 2/4/2000 AME 150 L
Parts of a Loop
• Initialization – Both Loop Index and calculations
• Incrementing– At completion of loop, increment (or 1) is
added to Loop Index
• Testing– Whenever Loop Index <= Final value, repeat
loop with new value of Loop Index
11 - 2/4/2000 AME 150 L
Parts of a Loop (continued)
• Body of Loop– The statements that are repeatedly executed for
different values of the Loop Index
• Special cases– CYCLE - go to increment & test immediately– EXIT - exit loop immediately
11 - 2/4/2000 AME 150 L
Power Series
INTEGER :: i, N, sum=0
REAL :: x, c(0:20)…read in x, N, and all c's (NOTE N<=20)
DO i = 0 , N
sum = sum + c(i) * x**i
END DO
0
( )N
ii
i
f x c x
11 - 2/4/2000 AME 150 L
Special Power Series - ex
• This series is a special case of the preceding example, where ci = 1/i!
• n! is notation for n factorial (or the factorial function)
• n! = 1*2*3*…*(n-1)*n
0 !
ix
i
xe
i
11 - 2/4/2000 AME 150 L
The Factorial Function !
• Normal definition -- repeated productProgram FragmentINTEGER :: I, n, nfact
n= {get some value for n}
nfact=1 !initialize factorial
DO I=1,n
nfact = nfact * I
END DO
11 - 2/4/2000 AME 150 L
More Factorial Function !• Recursive definition
1! = 1n! = n*(n-1)! [=n*(n-1)*(n-2)*…*2*1]
• Hence2!=2*1!=2*1=2; 3!=3*2!=3*2=6; 4!=4*3!=4*6=24; 5!=5*4!=5*24=1206!= 6*5!=6*120=720 7!=7*6!=7*720=5040 … factorial grows fast!
11 - 2/4/2000 AME 150 L
Factorial (Continued)
• Since n!=n*(n-1)!, and 1!=11! = 1* 0! 0! = 1
and
0! = 0 (-1)! =1 (-1)! is infinite (1/0)
and for every negative integer
(-n)! = (-n)*(-n+1)! (-n)! =and believe it or not,
11 - 2/4/2000 AME 150 L
The Gamma Function
Factorial is related to an integral called the Gamma Function
and hence, n! is defined for non-integer values of n [a real exclamation point]
0
! ( 1) n tn n t e dt