Академический Документы
Профессиональный Документы
Культура Документы
Robert Piché
Tampere University of Technology
y˙1 = f1 (t, y1 , . . . , yn )
y˙2 = f2 (t, y1 , . . . , yn )
.. .
. = ..
y˙n = fn (t, y1 , . . . , yn )
1
for some interval t0 ≤ t ≤ T , where f1 , . . . , fn are given functions, and satisfying
the n initial conditions
y1 (t0 ) = a1
y2 (t0 ) = a2
.. .
. = ..
yn (t0 ) = an
where the n values a1 , . . . , an are given. Here the expression “given” means
that they have been defined as a result of modeling the physical system, either
explicitly by you the modeler or automatically by your modeling software.
The matrix J of partial derivatives given by
∂fi
Jij =
∂yj
is called the jacobian matrix. If the jacobian matrix is constant, then the ordi-
nary differential equation is said to be linear shift-invariant.
Introducing the vectors y = [y1 , . . . , yn ]T , f = [f1 , . . . , fn ]T , and a =
[a1 , . . . , an ]T , the system of first-order ordinary differential equations can be
written in the form
ẏ = f (t, y) (1)
y(t0 ) = a (2)
y˙1 = y2
sin(t) − ky1 − cy2
y˙2 =
m
1.2.1 Example
The above example can be submitted to the DYNAST simulation software be-
low, as a set of two first-order equations. The reader is encouraged to submit
the example to DYNAST and try a few modifications to see how the results
change.
2
*:forced SHM with damping -second-order d.e.
*SYSTEM;
: Parameters
: Equations of motion
2 Algorithms
The simplest solution algorithm for ordinary differential equation initial value
problems is Euler’s method, given by
Y0 = a
Yj+1 = Yj + hj f (tj , Yj ), j = 0, 1, . . . , p − 1 (3)
where hj = tj+1 − tj is the time step. Euler’s method has the basic features
common to all solution algorithms. The algorithm starts with the given initial
value Y0 = y(t0 ) = a, and then marches forward in time, computing the sequence
of approximate solution values Y0 = y(t0 ), Y1 ≈ y(t1 ), . . . , Yp ≈ y(tp ) in order.
Euler’s method is a good pedagogical example, but is not recommended for
most practical use. There are many algorithms to solve ordinary differential
equation initial value problems that are much more efficient. Simulation soft-
ware and mathematical software libraries typically offer a selection of several
different algorithms. As a user, you are left with the task of choosing the algo-
rithm that is suitable for your problem.
The software documentation will try to guide your choice by telling some-
thing about the algorithms. You will typically be informed that a particular
algorithm is
• one-step or multistep,
3
• explicit or implicit,
• low-order or high-order
Thus, to make an informed choice of algorithm, you need to understand what
these terms mean. They are explained in this section, which describes the gen-
eral features of various algorithms for numerical solution of ordinary differential
equation initial value problems.
4
constant time step algorithms will plow right through and compute completely
erroneous results. Also, a varying time step algorithm ensures that numerical
instability does not occur, as discussed later in this lesson.
2.1.1 Example
The purpose of this example is to show a case where a variable time step method
is better than a constant time step method. We consider the following initial
value problem (IVP)
ẏ1 −0.1 −199.9 y1 y1 (0) 2
= , = (4)
ẏ2 0 −200 y2 y2 (0) 1
The response of y1 is the sum of two parts, one of which is much faster than
the other. In other words this IVP is stiff.
Exercise 1. Verify that equation (5) is a solution of (4) by differentiation.
Equation (4) can be solved using a constant time step or variable time step
methods with the DYNAST software as follows.
The Dynast program for a constant time step is shown below.
The penultimate line in this input, RUN MIN=10, MAX=10 forces DYNAST
to use a constant time step method rather than a varying time step method.
MIN specifies the minimum step length a variable time step method should use.
The minimum step length is given by the time range interval divided by the
value of MIN . Likewise MAX specifies the maximum step length a variable time
step method should use. By making MIN and MAX equal the method is clearly
retricted to be a constant time step method.
5
Figure 1: Plot of the numerical solutions of equation (4) using DYNAST with
a constant time step method.
When you click Submit to DYNAST you get a new browser window with
text output. To see the plot, click on graphical form in the output window.
Only the first output variable, y1 , is shown in the plot. To display y2 as well
click on y2 under Dependent variables and then on the box Redraw. To obtain
a common y-axis click on Common Y. It is also instructive to choose the Show
point marks option to show the solution points computed by the solver. The
continuous graph is a linear interpolation between these points. Click on Redraw
after choosing these options. You should obtain a plot of y1 and y2 shown in
Figure 1. Choose Discrete X instead of Show point marks and y1 only to obtain
Figure 2. This shows only the function points calculated by this numerical
method. Notice how they are evenly spaced (except at the end : a minor bug?).
We can compare the numerical solution with the exact analytical solution
by adding some lines to the DYNAST input code, as follows.
6
Figure 3: Plot of exact and numerical solution of equation (4) using DYNAST
with a constant time step method.
7
Table 1: Output of system (4) solved using DYNAST with a constant time step
method.
Number of nodes: 2
Number of equations: 2
X ... TIME
1 ... y1
2 ... yexact1
3 ... error1
X 1 2 3
0.000000E+000 2.000000E+000 2.000000E+000 0.000000E+000
1.000000E-002 1.332334E+000 1.134336E+000 1.979985E-001
2.000000E-002 1.109114E+000 1.016318E+000 9.279647E-002
3.000000E-002 1.034043E+000 9.994832E-001 3.455978E-002
4.000000E-002 1.008356E+000 9.963435E-001 1.201221E-002
5.000000E-002 9.991302E-001 9.950579E-001 4.072312E-003
6.000000E-002 9.953927E-001 9.940241E-001 1.368578E-003
7.000000E-002 9.934852E-001 9.930253E-001 4.598891E-004
8.000000E-002 9.921883E-001 9.920320E-001 1.562687E-004
9.000000E-002 9.910956E-001 9.910404E-001 5.524675E-005
9.500000E-002 9.905750E-001 9.905450E-001 2.997530E-005
1.000000E-001 9.900672E-001 9.900498E-001 1.739895E-005
Errors: 0, Warnings: 0
Total seconds used up by DYNAST: 0.00
Program DYNAST exited on Friday, 3 March 2000, at 15:40:18
8
Figure 4: Absolute error in numerical solution y1 of equation (4) using DYNAST
with a constant time step method.
Figure 5: Discrete plot of the numerical solution y1 of equation (4) using DY-
NAST with a variable time step method.
You can now try the same problem using the DYNAST program with a
variable time step method. You should change the DYNAST input code from
RUN MIN=10, MAX=10;
to
RUN;
and run this.
Again you can plot the results graphically as discrete time steps by clicking
on the graphical form and then on the box next to Discrete X and then on the
box Redraw. The plot for y1 is given in Figure 5 as a discrete plot. Figure 5
should be compared with Figure 2. The variable time step differs from the
constant time step solution: it uses small steps at the beginning where the
function is changing rapidly, and large steps where the function is not changing
much.
9
Figure 6: Plot of the exact and numerical solution, y1 , of equation (4) using
DYNAST with variable step method.
Figure 7: Absolute and relative error for numerical solution, y1 , of equation (4)
using DYNAST with variable step method.
10
Table 2: Output of system (4) solved using DYNAST with a variable time step
method.
X ... TIME
1 ... y1
2 ... yexact1
3 ... error1
Errors: 0, Warnings: 0
Total seconds used up by DYNAST: 0.01
Program DYNAST exited on Friday, 3 March 2000, at 16:37:19
method used in the example above. How much more time does it take to compute
using this method compared with the variable time step method?
HINT: This involves running DYNAST with different step lengths and ex-
amining the maximum error in the output. This requires some trial and error.
An examination of the spacing of the few steps in the variable time step
method in the output file should give a hint of how closely the steps need to be
spaced in the constant time step method. However, it turns out that this gives
an overestimate so try something between the order of 1000 and 10000.
11
Yj +1
Yj
t
tj tj + h /2 tj + h
Figure 8: In the classic Runge-Kutta method, the solution point Yj+1 is com-
puted using the previous solution value Yj and a weighted average of the slopes
at four points.
most well known variant, the classic Runge-Kutta method, has the form
k0 = f (tj , Yj )
h h
k1 = f (tj + , Yj + k0 )
2 2
h h
k2 = f (tj + , Yj + k1 )
2 2
k3 = f (tj + h, Yj + hk2 )
h
Yj+1 = Yj + (k0 + 2k1 + 2k2 + k3 ) (6)
6
This method requires four evaluations of the function f per time step, compared
to one evaluation required by the Euler method. The additional computation
is more than compensated by the larger time steps that the algorithm can use
because of its higher accuracy.
The values k0 , k1 , k2 , k3 in the Runge-Kutta method are the derivatives at
various points close to the initial solution value Yj (Figure 8). In equation (6),
the next solution point Yj+1 is computed using a weighted average of these
derivatives.
The following are some of the one-step algorithms that are widely available:
• Runge-Kutta methods with varying time step such as the Merson, Fehlberg,
England, and Prince-Dormand methods;
• Extrapolation methods such as the Bulirsch-Stoer method;
• Implicit Runge-Kutta methods and Rosenbrock methods, which are used
for stiff problems (discussed later).
Multistep algorithms, in contrast to one-step algorithms, have “memory”, in
the sense that they use k+1 previously computed solution values Yj , Yj−1 , . . . Yj−k
to compute the next solution value Yj+1 . The Adams methods is a widely used
class of multistep algorithm, of which an example with k = 2 is
h
Yj+1 = Yj + (23f (tj , Yj ) − 16f (tj−1 , Yj−1 ) + 5f (tj−2 , Yj−2 )) (7)
12
12
Yj +1
Yj
Yj
-1
Yj -2
t
tj-2 tj -1 tj tj +1
Figure 9: In a multistep method, the solution point Yj+1 is computed using the
solution value Yj and a weighted average of the slopes at several time points.
This formula uses values at times tj , tj−1 , tj−2 to compute the solution at time
tj+1 (Figure 9).
A modern implementation of a multistep method varies both the time step
size hj and the number of memory levels k in the course of the computation. At
the initial point t0 , there are no solution values available other than the initial
value, so the algorithm starts with k = 0 and a small step size.
In general, multistep algorithms need fewer evaluations of the differential
equation function f per time step than one-step algorithms. For example, the
Adams method (7) only needs to evaluate f once per time step, because values
can be stored and reused from previous time steps. By contrast, the classic
Runge-Kutta method (6) requires four evaluations of f per time step.
Nevertheless, one-step algorithms are often faster than multistep algorithms,
because of differences in accuracy and differences in the computational complex-
ity. Older textbooks recommend multistep algorithms for smooth problems that
require high accuracy and whose differential equation functions f are computa-
tionally expensive, and one-step algorithms for the rest. Nowadays there is not
such a clear a-priori distinction between newer one-step and multistep codes,
and it is worthwhile to try out both types of algorithm for your particular
problem.
13
0
with the initial value of the iteration Yj+1 computed using the explicit Euler
method. This idea of using an explicit method and then iterating an implicit
method is called a predictor-corrector approach, and is usually associated with
multistep algorithms.
Another way of solving implicit formulas is to use Newton iteration. In the
backward Euler formula the Newton iteration is
m+1
Yj+1 m
= Yj+1 − (I − hj J(tj+1 , Yj+1
m
))−1 [Yj+1
m
− Yj − hj f (tj+1 , Yj+1
m
)]
Each Newton iteration step requires the solution of a system of linear alge-
braic equations that involves the jacobian matrix. The jacobian matrix can
be computed using approximate formulas based on finite differences with small
perturbations of the arguments of f .
The formation of the jacobian matrix and the linear algebra of the Newton
iterations are computationally expensive operations. However, implicit algo-
rithms with Newton iteration are the most efficient alternatives when dealing
with stiff problems, as explained in the next paragraph.
ẏ = −20y, y(0) = 1
y(t) = e−20t
The Euler method (3) with constant time step h = 0.01 gives a reasonably
good approximation, but with a constant step h = 0.11 the solution is unstable
(Figure 10). In fact, the Euler method gives the solution
Yj = (1 − 20h)j
14
6
0
y
-6
0 1
Figure 10: The Euler method applied to ẏ = −20y is stable for time step
h = 0.01 (dots) but is unstable for time step h = 0.11 (circles).
2.4.1 Example
The example can be submitted to the DYNAST program.
15
2.5 Low-order vs. high-order
All algorithms compute their approximation of the solution of the differential
equation using a finite number of values of the function f . The error that
arises in this approximation is called truncation error. For constant time step
algorithms, the maximum truncation error when solving a differential equation
over a fixed time interval t0 ≤ t ≤ T is proportional to the time step h raised
to some exponent p. The value of p is called the order of the method.
For example, it can be shown that the truncation error of one time step of
the Euler method is, for small time step h, proportional to h2 , and that the
maximum error over an interval t0 ≤ t ≤ T is proportional to h. Thus, Euler’s
method has order 1. The backward Euler method also has order 1, while the
classic Runge-Kutta method has order 4, and the Adams method with k memory
levels has order k.
The order of a method is determined using Taylor series analysis. For exam-
ple, the trucation error of one time step of the Euler method applied to a single
differential equation is found as follows. The Taylor series of the differential
equation is
h2
y(tj+1 ) = y(tj + h) = y(tj ) + hẏ(tj ) + ÿ(ξ), tj ≤ ξ ≤ tj + h
2
Assuming Yj = y(tj ) and invoking (3), the truncation error is found to be
h2
y(tj+1 ) − Yj+1 = ÿ(ξ)
2
which is proportional to h2 .
A high-order method is more efficient than a low-order method in most prac-
tical problems. For example, the Euler method applied to a particular problem
has error Ce h while the classic Runge-Kutta method has error Crk h4 , where Ce
and Crk are constants. Then, to achieve an error smaller than some specified
level tol, the Euler method has to use a time step size no larger than tol/Ce . The
classic Runge-Kutta method may use a time step of (tol/Ce )1/4 , which is much
larger than the Euler method’s time step size, when tol is sufficiently small.
For less stringent accuracy requirements, however, the advantage of high-order
methods is not so great.
Also, if the solution is not sufficiently smooth, for example because of discon-
tinuous forcing functions, a high-order algorithm will not be as accurate as the
theoretical analysis promises. This is because the Taylor series analysis assumes
that high-order derivatives of the solution are continuous. Some algorithms vary
the number of memory stages k during the course of the computation, adap-
tively varying the order of the method as well as the time step. Software may
allow you to set an upper limit to the order to be used by the algorithm. This
is useful for problems that are not sufficiently smooth to justify the additional
computational expense of high-order methods.
3 Accuracy tolerance
The accuracy tolerance that you specify for the numerical solution of an ordinary
differential equation initial value problem affects the speed and accuracy of the
16
computation. This section presents some guidelines on how to select appropriate
values for the tolerance.
As was explained earlier, modern algorithms adaptively adjust the time step
(and sometimes the order of the method) to maintain the estimated truncation
error below a level that you specify. The specified tolerance is usually in terms
of relative error,
Yp − y(tp )
y(tp ) ≤ rtol
For example, if some components of the solution are zero (at the initial value,
say), then the relative error is not defined. Absolute error tolerance is also useful
to avoid situations where the algorithm works too hard to achieve high relative
accuracy on solution components that are of little interest or influence when
they are small.
You should avoid choosing error tolerances that are too large or too small.
If your error tolerance is too large, the algorithm’s error estimation formulas,
which are only valid for sufficiently small time step size, will give incorrect error
estimates. As a result, the adaptive time step size control will not work correctly
and the computed solution will not have the accuracy that you specified. You
should specify a relative accuracy of at least 1%.
If your error tolerance is too small, the computation will take an excessively
long time. The error tolerance may even be impossible, in the sense that the
requested truncation error is smaller than the roundoff error of the computer.
For example, if you specify atol=10−3 and one of the solution components is
somewhere around 106 , then the corresponding relative error is 10−9 , which is
smaller than the unit roundoff error of 32-bit floating point computers. When
you ask for impossible accuracy, the computation takes a long time and the
accuracy of the results is worse than when less stringent error tolerances are
specified.
Your software may ask for a single tolerance for all solution components.
This is easier for you than specifying a tolerance for each component, but may
give poor results if the solution components are not scaled the same, because the
error tolerance may be too large or too small for some of the components. For
example, the scales may differ by many orders of magnitude when the solution
components include variables with different physical units.
When the problem is stiff or has highly oscillatory components, the algo-
rithm’s error estimate may be significantly inaccurate. Repeating the com-
putation with different error tolerances can be helpful in detecting erroneous
solutions.
4 Answers to exercises
These are the answers to the exercises set in the course units.
17
Answer to Exercise 1. The following steps show the differentiation steps to
get the system of odes
ẏ1 −0.1 −199.9 y1 y1 (0) 2
= , =
ẏ2 0 −200 y2 y2 (0) 1
Furthermore
d d
y2 (t) = exp(−200 t)
dt dt
which gives
as required.
18
Figure 11: Plot of the numerical solutions of equation (4) using DYNAST with
a variable time step method.
19
The output is as follows
X ... TIME
1 ... y1
2 ... yexact1
3 ... error1
X 1 2 3
MAX 2.000000E+000 2.000000E+000 4.711374E-004
MIN 9.900498E-001 9.900498E-001 0.000000E+000
NP 7801
Errors: 0, Warnings: 0
Total seconds used up by DYNAST: 1.58
Program DYNAST exited on Tuesday, 7 March 2000, at 10:22:43
Notice that the value of the maximum of error1 is comparable to the variable
step method but the total number of steps is very large (7800) and the compu-
tational time is larger (1.58 seconds) than the time for the variable step method
of similar accuracy (0.01 seconds).
End of answers.
20