Information Technology Reference
In-Depth Information
Graphics
Plotting the solutions is performed as in the Fortran case; i.e., an external plotting
program is used to plot the data stored in the file sol.dat .
There exist plotting programs written in C or C CC that can be used as libraries
for the present program such that the u array can be sent directly to a plotting routine,
without using a file for intermediate data storage. However, there are no “standard”
plotting libraries for C CC .
6.4.5
Java
The Java program for solving an ODE by Heun's method is quite similar to the
corresponding C CC program, except that the stand-alone functions heun and dump
must be methods of a class in Java.
The implementation of the heun function, as described in Algorithm 6.12 ,is
performed via the advance method in class Heun :
class Heun {
public static double[] advance (Func f, double u0,
double dt, int n)
{
double u [] = new double[n+1];
u[0] = u0; // initial condition
// advance n steps:
for (int i = 0; i < n; i++) {
double v = f.f(u[i]);
u[i+1] = u[i] + 0.5 * dt * (v + f.f( u[i] + dt * v ));
}
return u;
}
}
The right-hand side of the ODE is represented as function objects in a Func hier-
archy, as explained in Sect. 6.3.4 . Arrays in Java are created in the same manner as
in C CC . However, there is no need to delete the array, since Java will do this when
the array is no longer in use. This makes it possible to create our array wherever it
is convenient, e.g., in the Heun.advance function, and return it from the function
to the calling code, if desired. (In C CC this is also possible, but it is usually a good
habit, from a programming safety point of view, to perform both the new and delete
statements in the calling code. Declaring a variable u of type std::valarray inside
the C CC heun function can of course be performed, but C CC requires us to copy
this array if we want to return it, whereas with plain C/C CC and Java arrays we
avoid this copy, and only references/pointers are returned.)
File writing in Java is typically illustrated by the following statements:
PrintStream f = new PrintStream(new FileOutputStream("sol.dat"));
f.println(time + " " + u[i]);
f.close()
Search WWH ::




Custom Search