Information Technology Reference
In-Depth Information
Arrays in Fortran 77 must be allocated at compile time. That is, we cannot ask
the user for
t
and
t
stop
, compute
N D t
stop
=t
, and create an array
u
of length
N
. Instead, we must allocate a sufficiently long
u
array in the main program. All
subroutines or functions can, however, receive arrays with lengths specified by vari-
ables. In our case, subroutine
heun
works with the first
n+1
elements of the
u
array
allocated at compile time in the main program. The
parameter
statement enables
setting the value of constants at compile time.
To get user information about
t
and
t
stop
,weuse
write
and
read
statements
and communicate with the user in terms of questions and answers. In other program-
ming languages we will get input from the command line instead. This is normally
possible in Fortran by calling up functionality in a C library, but the feature is not
a part of the Fortran standard. We also mention that comparisons of numbers in if-
statements apply the operators
.lt.
,
.le.
,
.eq.
,
.ge.
,and
.gt.
rather than
<
,
<=
,
==
or
=
,
>=
,and
>
, respectively.
Checking that
n
is not greater than
nmax
is very important. Without this test,
subroutine
heun
will index the
u
array outside the allocated chunk of numbers.
This may or may not produce strange error messages (usually just
segmentation
fault
) that are hard to track down. Some compilers have flags for detecting if the
array index is outside valid values. However, in subroutine
heun u
is declared as
u(0:n)
, and the compiler-generated check is just that the index
i
does not exceed
the value
n
, not whether
i
is outside the physical bounds of the array.
The Fortran 77 code is compiled, linked, and run as explained in Sect.
6.3.2
.
6.4.3
Graphics
Here we explain how we can plot curve data stored in files. The data consists of
points
.x
i
;y
i
/
,
i D 0;:::;N
, and the goal is to draw line segments between these
points in an
xy
coordinate system. We assume that the
.x
i
;y
i
/
points are stored
in two columns in a file, the
x
i
values in column 1 and the
y
i
values in column 2.
This file is given the name
sol.dat
in the forthcoming demonstrations of plotting
programs. In the present example with the numerical solution of ODEs, the
x
i
values
correspond to points in time, and the
y
i
values are the associated values of the
unknown function
u
in the differential equation.
Gnuplot
A widely available plotting program is Gnuplot. Assuming that the name of the file
containing the data points in two columns is
sol.dat
, the Gnuplot command
plot 'sol.dat' with lines
displays the graph. To try it out: Start Gnuplot by typing
gnuplot
. This enters a
command mode where you can issue the command above.