This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 6 Fortran Procedures: Subprograms and Functions Numerical programming often requires repeating the same calculations with differ- ent data and variables. Examples include having to integrate different functions or the same function over different intervals repeatedly. Having to recode the same chunk of fortran statement is wasteful of time and error prone as typos and bugs can be introduced in the process, moreover, the time we have invested in validating a specific piece of code would have to be duplicated again and again. Most pro- gramming languages provide a mechanism to avoid these repetitions and pitfalls. In FORTRAN this is done simply and elegantly by turning algorithmically related code segments into subprograms or procedures . There are many kinds of sub- programs in FORTRAN: subroutines , functions , and BLOCK DATA subprograms. Moreover, subroutines and functions can come in two flavors, internal and exter- nal . The func function already encountered in the trapezoidal rule integration is one example of an internal procedure. Subprograms are essential to build complex software as it allows programmers to think of their code in terms of algorithmic units and permits some degree of code abstraction. The benefits is that the de- velopment and validation of code can be done flexibly and in manageable units. Validated software can also be reused without having to worry repeatedly about its correctness. 6.1 Modular Design The example of trapezoidal integration will be used again to illustrate the use of external procedures; the code for an adaptive trapezoidal integration is listed in 6.1. Clearly one can see a division of tasks among the different portions of the code. More specifically, one can distinguish the following phases in the program: • An initialization phase that primarily reads in the input data; lines 8-9. 43 44 CHAPTER 6. FORTRAN PROCEDURES: SUBPROGRAMS AND FUNCTIONS 1 program trap 2 implicit none 3 integer :: N ! number of intervals 4 real :: a,b ! interval extrema 5 integer :: maxiteration ! maximum number of iterations allowed 6 integer :: i, iteration ! counters 7 real :: dx, Ans, xi, error, Ansold, tolerance 8 print *,’Enter a, b,tolerance, initial N, maxiteration’ 9 read *, a,b, tolerance, N, maxiteration 10 do iteration = 1,maxiteration ! iteration count 11 dx = (b-a)/real(N) ! grid size 12 Ans = 0.5*( myfunc(a) + myfunc(b) ) ! Evaluate function at a and b 13 do i = 1,N-1 14 xi = i*dx + a 15 Ans = Ans + myfunc(xi) 16 enddo 17 Ans = Ans * dx ! Integral estimate with N points 18 if (iteration > 1) then ! one iteration before check 19 error = abs(Ans - Ansold) 20 if (error < tolerance) then ! absolute error 21 exit ! converged, exit loop 22 else ! if not converged do following 23 N = 2*N ! double number of intervals 24 Ansold = Ans ! store old answer 25 endif 26 else 27 error = 0.0 28 endif 29 print *, N, Ans, error 30 enddo 31 print *, N, Ans, error ! print final answer 32 stop 33 contains 34 real function myfunc(x)...
View Full Document
This note was uploaded on 01/08/2012 for the course MSC 321 taught by Professor Staff during the Fall '08 term at University of Miami.
- Fall '08