Information Technology Reference
In-Depth Information
6.4
Heun's Scheme in Different Languages
The purpose of this section is to implement Heun's method from Algorithm 6.12
on page 212 in the following programming languages: Fortran 77, C, C CC ,Java,
Matlab, Maple, and Python. Section 6.3 gives a glimpse of these languages applied
to the trapezoidal integration technique. You should therefore be familiar with
Sect. 6.3 before proceeding with the implementation examples regarding Heun's
method. These new examples involve array and file handling in the various computer
languages.
6.4.1
Code Structure
Before diving into the details of various programming language, we should have
a clear view of the structure of the complete program. It would be convenient to
have t and the total simulation time t stop D Nt as input parameters to the main
program. Algorithm 6.12 must then be called and the solution should be displayed
graphically. The latter task can in some environments easily be done inside the pro-
gram, while in other situations we need to store the solution in a file and use a
separate plotting program to view the graph of u .t / . We have decided to always
write the solution to a file (to demonstrate file writing) and in addition display the
solution directly when this is feasible. The code must also implement a right-hand
side function f. u / . For testing purposes, we choose f. u / D u and u .0/ D 1 such
that the exact solution reads u .t / D e t .
6.4.2
Fortran 77
Algorithm 6.12 can be directly translated to Fortran 77:
subroutine heun (f, u0, dt, n, u)
integer n
real * 8 u0, u(0:n), dt, f
external f
integer i
real
v
C
initial condition:
u(0) = u0
C
advance n steps:
do i = 0, n-1
v = f(u(i))
u(i+1) = u(i) + 0.5 * dt * (v + f( u(i) + dt * v))
end do
return
end
Search WWH ::




Custom Search