Information Technology Reference
In-Depth Information
There are two new issues (compared with the trapezoidal function explained in
Sect. 6.3.2 ):
1. We now have a subroutine, i.e., a subprogram that does not return a value.
2. One of the arguments is an array. The indices in this array range from 0 up to (and
including) n , specified by the declaration real * 8 u(0:n) . The array is indexed 12
using the syntax u(i) .
All arguments in Fortran 77 subroutines and functions are both input and output
variables, in the sense that any changes in these variables are visible outside the
function. That is, if we change u , which we intend to do, since u is supposed to hold
the computed function values, the final u is immediately available in the calling
program:
C
calling code:
integer n
real * 8 v(0:n), v0, some_f, dt
external some_f
dt = 0.01
v0 = 1.0
call heun (some_f, v0, dt, n, v)
Note that subroutines are called using the call statement (in most other languages
it is sufficient to write the name of the subprogram). After the return from the call
heun statement, the v array will contain the computed discrete u .t / values.
If we accidentally change u0 inside the heun subroutine, the new value will be
reflected in the v0 variable in the calling program. This is not a feature we want.
Most other programming languages have mechanisms for taking a copy of the argu-
ments (usually referred to as call-by-value). Using copies of pure input variables
reduces the danger of unintended changes to the variables in the calling code.
Having called the subroutine heun to compute discrete function values in an
array, we may want to dump this array to file for later plotting. A suitable file format
is to have two numbers on each line, the time point and the corresponding value of
u ,e.g.,
0. 1.
0.004 0.996008
0.008 0.992031936
0.012 0.988071745
0.016 0.984127362
0.02 0.980198726
0.024 0.976285772
0.028 0.97238844
Most plotting programs can read such data pairs: t i ; u .t i / , and draw straight line seg-
ments between the points to visualize the curve. In our code we include a subroutine
dump that creates such a file. Some key statements for illustrating writing in Fortran
77 are given next.
12 Some compilers have flags for checking the validity of the index (with an associated efficiency
penalty).
Search WWH ::




Custom Search