Download presentation
Presentation is loading. Please wait.
Published byAnnabella Robbins Modified over 9 years ago
1
11 - 2/4/2000AME 150 L Program Control Subroutines and Functions
2
11 - 2/4/2000AME 150 L Program Control - Loops Powerful numerical tool - SERIES –Polynomials or Power Series –Infinite Series of Generalized Functions
3
11 - 2/4/2000AME 150 L Arrays of Variables The use of indexed or subscripted variables can be treated in one of two ways a) Create different variables c 0 C0, c 1 C1, …, c N CN b) Use a formal array structure REAL, dimension(0:12) :: C then c 0 C(0), c 1 C(1), …, c N C(N) Size of an array is either a fixed number, a parameter, (or an argument or dynamic)
4
11 - 2/4/2000AME 150 L Declaring Arrays Two equivalent methods a) REAL, dimension(-2:5) :: a,b,c the 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
5
11 - 2/4/2000AME 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 2definition 1definition 2
6
11 - 2/4/2000AME 150 L Recursion (Examples) Calculate x n 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
7
11 - 2/4/2000AME 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
8
11 - 2/4/2000AME 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
9
11 - 2/4/2000AME 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
10
11 - 2/4/2000AME 150 L Trivial example of a Loop Sum of Integers, Program fragment INTEGER :: 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
11 - 2/4/2000AME 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
12
11 - 2/4/2000AME 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
13
11 - 2/4/2000AME 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
14
11 - 2/4/2000AME 150 L Special Power Series - e x This series is a special case of the preceding example, where c i = 1/i! n! is notation for n factorial (or the factorial function) n! = 1*2*3*…*(n-1)*n
15
11 - 2/4/2000AME 150 L The Factorial Function ! Normal definition -- repeated product Program Fragment INTEGER :: I, n, nfact n= {get some value for n} nfact=1!initialize factorial DO I=1,n nfact = nfact * I END DO
16
11 - 2/4/2000AME 150 L More Factorial Function ! Recursive definition 1! = 1 n! = n*(n-1)! [=n*(n-1)*(n-2)*…*2*1] Hence 2!=2*1!=2*1=2; 3!=3*2!=3*2=6; 4!=4*3!=4*6=24; 5!=5*4!=5*24=120 6!= 6*5!=6*120=720 7!=7*6!=7*720=5040 … factorial grows fast!
17
11 - 2/4/2000AME 150 L Factorial (Continued) Since n!=n*(n-1)!, and 1!=1 1! = 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,
18
11 - 2/4/2000AME 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]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.