Numerical Integration of Ordinary Differential Equations For Initial Value Problems
Numerical Integration of Ordinary Differential Equations For Initial Value Problems
The latest version of this PDF file, along with other supplemental material
Gerald Recktenwald for the book, can be found at www.prenhall.com/recktenwald or
Portland State University web.cecs.pdx.edu/~gerry/nmm/.
Department of Mechanical Engineering
[email protected] Version 0.92 August 22, 2006
page 1
Q = hA(Ts − T∞) dT
mc = −Q = −hA(Ts − T∞)
dt
m, c
where Q is the heat transfer rate, h is the
Assume material is highly conductive ⇒ Ts = T
heat transfer coefficient, A is the surface
area, Ts is the surface temperature, and dT
T∞ is the temperature of the fluid. mc = −hA(T − T∞)
dt
When the cooling rate is primarily
controlled by the convection from the or
dT hA
surface, the variation of the object’s =− (T − T∞)
dt mc
temperature with is described by an
ODE.
The ODE
dy
= −y y(0) = y0 The generic form of a first order ODE is
dt
can be integrated directly: dy
= f (t, y); y(0) = y0
dy dt
= −dt
y where the right hand side f (t, y) is any single-valued function of t and y .
ln y = −t + C The approximate numerical solution is obtained at discrete values of t
ln y − ln C2 = −t
tj = t0 + jh
y
ln = −t
C2 where h is the “stepsize”
−t
y = C2 e
−t
y = y0 e
Graphical Interpretation
f (t0,y0) = slope at (t0,y0)
numerical solution at t3 y(t) = exact solution
y(tj ) = exact solution evaluated at tj
yj = approximate solution at tj
f (tj , yj ) = approximate r.h.s. at tj
y0 exact solution y(t)
h h h
t0 t1 t2 t3
Consider a Taylor series expansion in the neighborhood of t0 Given h = t1 − t0 and initial condition, y = y(t0), compute
˛ ˛
dy ˛˛ (t − t0)2 d2y ˛˛ y1 = y0 + h f (t0, y0)
y(t) = y(t0) + (t − t0) + ˛ + ...
dt ˛t0 2 dt2 ˛ y2 = y1 + h f (t1, y1)
t0
... ...
Retain only first derivative term and define
˛ yj+1 = yj + h f (tj , yj )
dy ˛˛
f (t0, y0) ≡
dt ˛t0 or
yj = yj−1 + h f (tj−1, yj−1)
to get
y(t) ≈ y(t0) + (t − t0)f (t0, y0)
y
For a given h, the largest error in
0.5
Euler Exact Error the numerical solution is the Global
j tj f (tj−1 , yj−1 ) yj = yj−1 + h f (tj−1 , yj−1 ) y(tj ) yj − y(tj ) Discretization Error or GDE. 0.4
Rewrite the discrete form of Euler’s method as Introduce a family of functions zj (t), which are the exact solutions to the ODE given the
approximiate solution produced by Euler’s method at step j .
yj − yj−1
= f (tj−1, yj−1) (discrete)
h dzj
= f (t, y); zj (tj−1) = yj−1
Compare with original ODE dt
dy
= f (t, y) (continuous) Due to truncation error, Euler’s method produces a value of yj+1 that is different from
dt zj (tj+1) even though by design, yj = z(tj ).
Substitute the exact solution into the discrete approximation to the ODE to get
In other words
y(tj ) − y(tj−1)
− f (tj−1, y(tj−1)) = 0 yj+1 − z(tj+1) = 0
h
because yj+1 contains truncation error.
1
The local discretization error (LDE) is the residual obtained when the exact zj (t) is
Example: Solve 0.9
0.8
substituted into the discrete approximation to the ODE
dy
= t − 2y; y(0) = 1 0.7
y(t) z(tj ) − z(tj−1)
dt 0.6 exact τ (t, h) = − f (tj−1, zj (tj−1))
h
y and z
0.5
The plot shows the numerical solution
0.4
(•) obtained with h = 0.5. The z(t) Note:
0.3
curve starting at each of the numerical z 3 (t) • User chooses h, and this affects LDE
0.2
solution points is shown as a solid line.
0.1
z 1 (t)
z 2 (t) • LDE also depends on t, the position in the interval
0
0 0.5 1 1.5 2
t
Using a Taylor series expansion for zj (x) we find that Assume that zj(ξ) is bounded by M in the interval t0 ≤ t ≤ tN . Then
z(tj ) − z(tj−1) h hM
− f (tj−1, zj (tj−1)) = zj (ξ) τ (t, h) ≤ LDE for Euler’s method
h 2 2
where tj−1 ≤ ξ ≤ tj and zj ≡ d2zj /dt2. Although M is unknown we can still compute the effect of reducing the stepsize by taking
the ratio of τ (t, h) for two different choices of h
Thus, for Euler’s method the local discretization error is
h τ (t, h2) h2
τ (t, h) = z (ξ) =
2 j τ (t, h1) h1
• The implementation separates the logic of the ODE integration scheme from the
evaluation of the right hand side, f (t, y). A general purpose ODE solver requires the
user to supply a small m-file for evaluating f (t, y).
We now commence a survey of one-step methods that are more accurate than Euler’s
Increase accuracy by evaluating slope twice in each step of size h
method.
• Not all methods are represented here k1 = f (tj , yj )
• Objective is a logical progression leading to RK-4
Compute a tentative value of y at the midpoint
• Sequence is in order of increasing accuracy and increasing computational efficiency
h
yj+1/2 = yj + f (tj , yj )
Methods with increasing accuracy, lower GDE 2
Method GDE re-evaluate the slope
h h
Euler O(h) k2 = f (tj +
, y j + k1 )
2
2 2
Midpoint O(h ) Compute final value of y at the end of the full interval
Heun O(h2)
yj+1 = yj + hk2
RK-4 O(h4)
Note that since h < 1, a GDE of O(h4) is much smaller than a GDE of O(h). LDE = GDE = O(h2)
Solve
dy
= −y; y(0) = 1; 0≤t≤1
Midpoint method requires twice as much work per time step. Does the extra effort pay dt
off? The exact solution is y = e−t.
Consider integration with Euler’s method and h = 0.1. Formal accuracy is O(0.1). >> compEM
Compute a tentative value of y at the endpoint estimate of slope at tj yj from Heun’s method
∗
yj = yj + hf (tj , yj )
Generalize the idea embodied in Heun’s method. Use a weighted average of the slope
➣ Euler’s method evaluates slope at beginning of the step
evaluated at multiple in the step
➣ Midpoint method evaluates slope at beginning and at midpoint of the step
X
➣ Heun’s method evaluates slope at beginning and at end of step yj+1 = yj + h γmkm
Can we continue to get more accurate schemes by evaluating the slope at more points in where γm are weighting coefficients and km are slopes evaluated at points in the interval
the interval? Yes, but there is a limit beyond which additional evaluations of the slope tj ≤ t ≤ tj+1
increase in cost (increased flops) faster than the improve the accuracy.
In general, X
γm = 1
Comparison of Euler, Midpoint and RK4 (2) Summary: Accuracy of ODE Integration Schemes
RHS
Error step size evaluations
• GDE decreases as h decreases
Euler 1.2 × 10−3 0.00625 161
−3
• Need an upper limit on h to achieve a desired accuracy
Midpoint 2.9 × 10 0.2 12
• Example: Euler’s Method
The goal is to obtain a solution that does not depend (in a significant way) on h.
Possible outcomes
• User supplies error tolerance, not stepsize
• If Δ is smaller than tolerance, accept the yj+1 solution. • Simultaneously compute 4th and 5th order Runge-Kutta solution
• If Δ is much smaller than tolerance, accept the yj+1 solution, and try increasing the • Compare two solutions to determine accuracy
stepsize. • Adjust step-size so that error tolerance is maintained
• If Δ is larger than tolerance, reduce h and try again.
Function Description
The ode45 function attempts to obtain the solution to within the user-specified error
ode113 Variable order solution to nonstiff systems of ODEs. ode113 uses an
tolerances. In some situations the solution can be obtained within the tolerance by taking
explicit predictor-corrector method with variable order from 1 to 13.
so few time steps that the solution appears to be unsmooth. To compensate for this,
ode15s Variable order, multistep method for solution to stiff systems of ode45 automatically interpolates the solution between points that are obtained from the
ODEs. ode15s uses an implicit multistep method with variable solver.
order from 1 to 5.
Consider
ode23 Lower order adaptive stepsize routine for non-stiff systems of ODEs. dy
= cos(t), y(0) = 0
ode23 uses Runge-Kutta schemes of order 2 and 3. dt
The following statements obtain the solution with the default parameters.
ode23s Lower order adaptive stepsize routine for moderately stiff systems of
ODEs. ode23 uses Runge-Kutta schemes of order 2 and 3. >> rhs = inline(’cos(t)’,’t’,’y’);
>> [t,Y] = ode45(rhs,[0 2*pi],0);
ode45 Higher order adaptive stepsize routine for non-stiff systems of ODEs. >> plot(t,Y,’o’)
ode45 uses Runge-Kutta schemes of order 4 and 5.
(See plot on next slide)
1 1
>> rhs = inline(’cos(t)’,’t’,’y’); Repeat without interpolation:
0.8 0.8
>> [t,Y] = ode45(rhs,[0 2*pi],0); >> options = odeset(’Refine’,1)
>> plot(t,Y,’o’) 0.6 >> [t2,Y2] = ode45(rhs,[0 2*pi],0,options);
0.6
0.4
>> hold on 0.4
>> plot(t2,Y2,’rs-’)
0.2 0.2
0
-0.8 -0.8
-1 -1
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
5000
Prey population
function dpdt = rhsPop2(t,p,flag,alpha,delta)
4500
% rhsPop2 Right hand sides of coupled ODEs for 2 species predator-prey system
%
4000
% Synopis: dpdt = rhsPop2(t,p,flag,alpha,delta)
% 3500
% Input: t = time. Not used in this m-file, but needed by ode45
% p = vector (length 2) of populations of species 1 and 2 3000
0 5 10 15 20 25 30
% flag = (not used) placeholder for compatibility with ode45
% alpha = vector (length 2) of growth coefficients
alpha(2) = 0.000200 delta(2) = 0.800000
% delta = vector (length 2) of mortality coefficients 120
%
115
% Output: dpdt = vector of dp/dt values
Predator population
dpdt = [ alpha(1)*p(1) - delta(1)*p(1)*p(2); 110
alpha(2)*p(1)*p(2) - delta(2)*p(2); ]; 105
100
95
90
85
0 5 10 15 20 25 30
time (arbitrary units)
F(t)
Governing equation is a second order ODE
m
2 F
x ẍ + 2ζωnẋ + ωnx =
k c m
c
X ζ≡ √
2 km
F = ma
q
Forces acting on the mass are
ωn ≡ k/m
Fspring = −kx
ζ and ωn are the only (dimensionless) parameters
Fdamper = −cẋ
F (t) − kx − cẋ = mẍ
F 2 subplot(2,1,1);
= − 2ζωny2 − ωny1 plot(t,y(:,1)); ylabel(’Displacement’); grid;
m title(sprintf(’zeta = %5.3f omegan = %5.1f’,zeta,omegan));
subplot(2,1,2);
plot(t,y(:,2)); xlabel(’Time (s)’); ylabel(’Velocity’); grid;
Solve Second Order System with ODE45 Response of Second Order System to a Step Input
zeta = 0.100 omegan = 35.0
0.015
function dydt = rhsSmd(t,y,flag,zeta,omegan,a0)
Displacement
% rhsSmd Right-hand sides of coupled ODEs for a spring-mass-damper system
0.01
%
% Synopis: dydt = rhsSmd(t,y,flag,zeta,omegan,a0)
% 0.005
% Input: t = time, the independent variable
% y = vector (length 2) of dependent variables
% y(1) = displacement and y(2) = velocity 0
% flag = dummy argument for compatibility with ode45 0 0.5 1 1.5
% zeta = damping ratio (dimensionless)
% omegan = natural frequency (rad/s) 0.6
% a0 = input force per unit mass
% 0.4
% Output: dydt = column vector of dy(i)/dt values
Velocity
0.2
if t<=0, fonm = 0.0;
else, fonm = a0; % Force/mass (acceleration)
end 0
dn−1u dyn
yn = = f (t, u)
dtn−1 dt