Professional Documents
Culture Documents
Differential Equations
for Initial Value Problems
Gerald Recktenwald
Portland State University
Department of Mechanical Engineering
gerry@me.pdx.edu
The latest version of this PDF file, along with other supplemental material
for the book, can be found at www.prenhall.com/recktenwald.
F = ma
Acceleration is the time derivative of velocity, so
dv
=a
dt
and
dv F
=
dt m
T∞
m, c
Q = hA(Ts − T∞)
The variation of temperature with time for a submerged object is
described by an ODE.
dT
mc = −Q = −hA(Ts − T∞)
dt
Assume material is highly conductive ⇒ Ts = T
dT
mc = −hA(T − T∞)
dt
or
dT hA
=− (T − T∞)
dt mc
Example:
dy
= −y y(0) = y0
dt
This equation can be integrated directly
dy
= −dt
y
ln y = −t + C
ln y − ln C2 = −t
y
ln = −t
C2
−t
y = C2 e
−t
y = y0 e
Given
dy
= f (t, y); y(0) = y0
dt
Find approximate solution at discrete values of t
tj = t0 + jh
Graphical Interpretation
f (t0,y0) = slope at (t0,y0)
numerical solution at t3
h h h
t0 t1 t2 t3
to get
y(t) ≈ y(t0) + (t − t0)f (t0, y0)
y1 = y0 + h f (t0, y0)
y2 = y1 + h f (t1, y1)
... ...
yj+1 = yj + h f (tj , yj )
or
yj = yj−1 + h f (tj−1, yj−1)
dy
= t − 2y y(0) = 1
dt
The exact solution is
1 −2t
y= 2t − 1 + 5e
4
dy
= t − 2y y(0) = 1
dt
for a sequence of smaller h (see demoEuler)
1
Exact
h = 0.2
0.9 h = 0.1
h = 0.05
0.8
0.7
0.6
y
0.5
0.4
0.3
0.2
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
t
ej = yj − y(tj )
GDE = max(ej ), j = 1, . . .
Here are results for the sample problem plotted on previous slide:
dy/dt = t − 2y , y(0) = 1
h max(ej )
0.200 0.1117
0.100 0.0502
0.050 0.0240
0.025 0.0117
dy
= f (t, y) continuous
dt
If the exact solution is substituted back into the discrete
approximation to the ODE we get
y(tj ) − y(tj−1)
− f (tj−1, y(tj−1)) = 0
h
dzj
= f (t, y) zj (tj−1) = yj−1
dt
Due to truncation error, Euler’s method produces a value of
yj+1 that is different from zj (tj+1) even though by design,
yj = z(tj ). In other words
Example Solve
dy
= t − 2y y(0) = 1
dt
0.9
0.8
0.7
y(t)
0.6 exact
y and z
0.5
0.4
0.3
z 3 (t)
0.2
z 1 (t)
0.1 z 2 (t)
0
0 0.5 1 1.5 2
t
z(tj ) − z(tj−1)
τ (t, h) = − f (tj−1, zj (tj−1))
h
z(tj ) − z(tj−1) h
− f (tj−1, zj (tj−1)) = zj (ξ)
h 2
h
τ (t, h) = zj (ξ)
2
hM
τ (t, h) ≤ LDE for Euler’s method
2
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
τ (t, h2) h2
=
τ (t, h1) h1
GDE = O(h)
Thus
GDE(h1) h1
=
GDE(h2) h2
Method GDE
Euler O(h)
Midpoint O(h2)
Heun’s O(h2)
RK-4 O(h4)
k1 = f (tj , yj )
h
yj+1/2 = yj + f (tj , yj )
2
re-evaluate the slope
h h
k2 = f (tj + , y j + k1 )
2 2
Compute final value of y at the end of the full interval
yj+1 = yj + hk2
estimate of slope
at tj–1 + 0.5h true solution for the given yj–1
yj–1 + 0.5 h k 1
tj–1 tj
Implementation in odeMidpt.m
function [t,y] = odeMidpt(diffeq,tn,h,y0)
% odeMidpt Midpoint method for integration of a single, first order ODE
%
% Synopsis: [t,y] = odeMidpt(diffeq,tn,h,y0)
%
% Input: diffeq = (string) name of the m-file that evaluates the right
% hand side of the ODE written in standard form
% tn = stopping value of the independent variable
% h = stepsize for advancing the independent variable
% y0 = initial condition for the dependent variable
%
% Output: t = vector of independent variable values: t(j) = (j-1)*h
% y = vector of numerical solution values at the t(j)
Solve
dy
= −y y(0) = 1 0≤t≤1
dt
The exact solution is y = e−t.
>> compEM
k1 = f (tj , yj )
k1 + k2
yj+1 = yj + h
2
tj–1 tj
In general,
γm = 1
k1 = f (tj , yj )
h h
k2 = f (tj + , y j + k1 )
2 2
h h
k3 = f (tj + , yj + k2)
2 2
k4 = f (tj + h, yj + hk3)
2
4
3
1
yj –1 true solution for the given yj –1
0.5h 0.5h
tj –1 tj
Solve
dy
= −y y(0) = 1 0≤t≤1
dt
>> compEMRK4
Conclusion:
Possible outcomes
Syntax:
[t,Y] = ode45(diffeq,tn,y0)
[t,Y] = ode45(diffeq,[t0 tn],y0)
[t,Y] = ode45(diffeq,[t0 tn],y0,options)
[t,Y] = ode45(diffeq,[t0 tn],y0,options,arg1,arg2,...)
User writes the diffeq m-file to evaluate the right hand side of
the ODE.
options = odeset(’parameterName’,value,...)
[y,t] = ode45(’rhsFun’,[t0 tN],y0,options,...)
Function Description
ode113 Variable order solution to nonstiff systems of
ODEs. ode113 uses an explicit predictor-
corrector method with variable order from 1
to 13.
ode15s Variable order, multistep method for solution to
stiff systems of ODEs. ode15s uses an implicit
multistep method with variable order from 1 to
5.
ode23 Lower order adaptive stepsize routine for non-
stiff systems of ODEs. ode23 uses Runge-Kutta
schemes of order 2 and 3.
ode23s Lower order adaptive stepsize routine for
moderately stiff systems of ODEs. ode23 uses
Runge-Kutta schemes of order 2 and 3.
ode45 Higher order adaptive stepsize routine for non-
stiff systems of ODEs. ode45 uses Runge-Kutta
schemes of order 4 and 5.
Consider
dy
= cos(t), y(0) = 0
dt
The following statements obtain the solution with the default
parameters.
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
The two solutions are identical at the points obtained from the
Runge-Kutta algorithm
Consider
dy1
= f1(t, y1, y2)
dt
dy2
= f2(t, y1, y2)
dt
dp1
= α1p1 − δ1p1p2 prey
dt
dp2
= α2p1p2 − δ2p2 preditor
dt
5000
Prey population
4500
4000
3500
3000
0 5 10 15 20 25 30
115
Predator population
110
105
100
95
90
85
0 5 10 15 20 25 30
time (arbitrary units)
F(t)
x
k c
F = ma
Forces acting on the mass are
Fspring = −kx
Fdamper = −cẋ
F (t) − kx − cẋ = mẍ
2 F
ẍ + 2ζωnẋ + ωnx =
m
c
ζ≡ √
2 km
ωn ≡ k/m
Define
y1 ≡ x y2 ≡ ẋ
then
dy1
= ẋ = y2
dt
dy2
= ẍ
dt
F 2
= − 2ζωnẋ − ωnx
m
F 2
= − 2ζωny2 − ωny1
m
function demoSmd(zeta,omegan,tstop)
% demoSmd Second order system of ODEs for a spring-mass-damper system
%
% Synopsis: smdsys(zeta,omegan,tstop)
%
% Input: zeta = (optional) damping ratio; Default: zeta = 0.1
% omegan = (optional) natural frequency; Default: omegan = 35
% tstop = (optional) stopping time; Default: tstop = 1.5
%
% Output: plot of displacement and velocity versus time
subplot(2,1,1);
plot(t,y(:,1)); ylabel(’Displacement’); grid;
title(sprintf(’zeta = %5.3f omegan = %5.1f’,zeta,omegan));
subplot(2,1,2);
plot(t,y(:,2)); xlabel(’Time (s)’); ylabel(’Velocity’); grid;
0.01
0.005
0
0 0.5 1 1.5
0.6
0.4
Velocity
0.2
-0.2
0 0.5 1 1.5
time (seconds)
Given
dnu
= f (t, u)
dtn
the transformation is
dy1
y1 = u = y2
dt
du dy2
y2 = = y3
dt dt
d2u dy3
y3 = = y4
dt2 dt
... ...
dn−1u dyn
yn = = f (t, u)
dtn−1 dt