Information Technology Reference
In-Depth Information
Heun's method for du/dt = -u
1
u(t)
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0
0.5
1
1.5
2
2.5
3
3.5
4
Fig. 6.2
Plot made by Matlab
title('Heun method for du/dt = -u');
legend('u(t)'); % attach curve label
print('-deps', 'tmp.eps'); % make Encapsulated PostScript plot
Suppose these commands are stored in a file
plotmatlab.m
. Inside Matlab we can
then write
plotmatlab
to execute the commands in
plotmatlab.m
and thereby
create the plot. Alternatively,
unix> matlab -nodesktop -r plotmatlab
creates the plot and invokes Matlab in a terminal window. Type
exit
to terminate
the session. The PostScript version of the plot, as created by Matlab, is displayed in
Fig.
6.2
.
6.4.4
C
CC
The Computational Algorithm
The implementation of Algorithm
6.12
in the C
CC
programming language can take
the following form:
void heun (fptr f, double u0, double dt, int n, double u[])
{
u[0] = u0; // initial condition
// advance n steps:
for(inti=0;i<=n-1; i++) {
double v = f(u[i]);
u[i+1] = u[i] + 0.5
*
dt
*
(v+f(u[i]+dt
*
v ));
}
}