5.1. Initial Value Problem - IVP
5.1. Initial Value Problem - IVP
5.1. Initial Value Problem - IVP
Differential Equations
5. Ordinary Differential Equations
3
The differential equations will be replaced with algeb-
raic equations whose solutions approximate those of the
given differential equations
More differential examples:
d 2 g
0 Harmonic Oscillator
dt 2 l
dT
k (T Tm ) Newtons Law of Cooling
dt
d2y
dx 2
k 1
dy 2
dx
Shape of a hanging string
yn y0 f ( x0 , y0 )( xact x0 ) y0
(x0,y0)
yn y0 f ( x0 , y0 )( xact x0 )
x0 xact
The insight that Euler had was to see how this estimate could be improved on. The
strategy he used was to divide the interval [x0,xact] (or [xact,x0] in the case xact<x0) into
equal subintervals and recompute the tangent line as you go. This would not allow the
tangent line to drift far from the function itself. This would hopefully produce a more
accurate estimate for yact at the end.
9
The first derivative provides a direct estimate of the
slope at xi
f ( xi , yi )
where f(xi,yi) is the differential equation evaluated at
xi and yi. This estimate can be substituted into the
equation:
yi 1 yi f ( xi , yi )h
A new value of y is predicted using the slope to
extrapolate linearly over the step size h.
10
To Apply Eulers Method (x4,y4)
1. Divide the interval n equal subintervals.
(x3,y3)
(xact,yact)
2. Compute the width of each subinterval which (x1,y1)
is x=h=(xn-x0)/n.
y0 (x2,y2)
3. Compute the sequence of points as follows: (x0,y0)
x
x1 x0 x
x0 x1 x2 x3 x4
( x1 , y1 )
y1 y0 f ( x0 , y0 ) x
x2 x1 x In general the coordinates of the
( x2 , y2 ) point (xn+1,yn+1) can be computed
y2 y1 f ( x1 , y1 ) x from the coordinates of the point
(xn,yn) as follows:
x3 x2 x
( x3 , y3 )
y3 y2 f ( x2 , y2 ) x xn 1 xn x
( xn1 , yn 1 )
x4 x3 x yn 1 yn f ( xn , yn ) x
( x4 , y4 )
y4 y3 f ( x3 , y3 ) x
Error calculation
True error (Et)
True error (Et) or Exact value of error
= true value approximated value
True error
True percent relative error t 100 (%)
True value
true value approximated value
100 (%)
true value
xT x A
100%
xT
Approximate Error
x A (i) xA (i 1)
x A (i ) x A (i 1)
x A (i )
x A (i) x A (i 1)
100%
x A (i)
Example:
Given the differential equation to the right with its boundary
dy
dx x y 2
1
y 1 0 2
1 12 1
8
2 2 2 1
y(2) e 14.3919
1 1
x 3
4.
1 2 1
y2 1 2 1 2 1 8 1.125
1
3
x 1 2 1.5
1
5.
3
y 1 .125
12
1.
125 2 1.125 .5625 1.6875
1
4
x 1. 5 2 2
1
6.
4
y 1 .6875 1.5 2
1.6875 1
2 1.6875 1.89844 3.58594
Algorithm for Eulers Method
Given
f(x,y) (* expression for x and y *)
x0 (* initial value for x *)
y0 (* initial value for y *)
xn (* terminal value for x *)
n (* number of partitions of the interval *)
deltax = (xn-x0)/n
xi = x0
xprev = xi
yi = y0
for(i=1, in, i++,
xi = xi + deltax
yi = yi + f(xprev,yi)*deltax)
xprev=xi
Return yi
Example 2: Eulers Method (1 of 3)
For the initial value problem
y 9.8 0.2 y, y(0) 0
y1 y0 f 0 h 0 9.8(0.1) .98
y2 y1 f1 h .98 9.8 0.2.98(0.1) 1.94
y3 y2 f 2 h 1.94 9.8 0.21.94(0.1) 2.88
y4 y3 f 3 h 2.88 9.8 0.22.88(0.1) 3.80
Example 2: Exact Solution (2 of 3)
From table below, we see that the errors are small. This is
most likely due to round-off error and the fact that the exact
solution is approximately linear on [0, 0.4]. Note:
yexact yapprox
Percent Relative Error 100
yexact
Predictor : y0
i 1 yi f ( xi , yi )h
f ( xi , yi ) f ( xi 1 , yi01 )
Corrector : yi 1 yi h
2
0
Where y 23
n 1 is calculated using the Eulers method.
Algorithm for the improved Euler
method
1. Given a solution at point (xi,yi), calculate the
next value of the independent variable: f ( xi , y i )
2. Calculate, f(x xi h
i,yi).
xi 1
3. Estimate yi+1 using Eulers method:
Predictor : yi01 yi f ( xi , yi )h
4. Calculate f ( xi 1 , y 0i 1 )
5. Calculate the numerical solution at x xi 1 :
f ( xi , yi ) f ( xi 1 , yi01 )
Corrector : yi 1 yi h
2
24
25
Example 4:
Consider the previous initial value problem.
y y x , y(0) 0.5
Find the value of y(0.8).
1
*
y1 y0 h f x0 , y0 f x1 , y1
2
1
*
y1 y0 h f x0 , y0 f x1 , y1
2
1
y1 0.5 0.1 0.5 0.45 0.5475
2
f ( x1 , y1 ) y1 x1 0.5475 0.1
0.4475
0.59225
f ( x2 , y ) y x2 0.59225 0.2
*
2
*
2
0.39225
1
*
y2 y1 h f x1 , y1 f x2 , y2
2
1
y2 0.5475 0.1 0.4475 0.39225
2
0.589487
5.1.2.2. The Midpoint (or Improved Polygon) Method/
Uses Eulers method t predict a value of y at the
midpoint of the interval:
yi 1 yi f ( xi 1/ 2 , yi 1/ 2 )h
31
5.1.3. Runge-Kutta Methods (RK)
yi 1 yi (a1k1 a2 k2 )h
with
k1 f ( xi , yi )
k2 f ( xi p1h, yi q11k1h )
33
Values of a1, a2, p1, and q11 are evaluated by setting
the second order equation to Taylor series expansion
to the second order term. Three equations to
evaluate four unknowns constants are derived.
We have : yi 1 yi ( a1k1 a2k2 )h
f ' ( xi , yi ) 2
However yi 1 yi f ( xi , yi )h h
2!
f ( xi , yi ) f ( xi , yi ) dy
But f ' ( xi , yi )
x y dx
f ( xi , yi ) f ( xi , yi ) dy h 2
Then yi 1 yi f ( xi , yi )h 2!
x y dx
k1 f ( x i , yi )
k2 f ( xi p1h, yi q11k1h )
We now expand k2 f ( xi p1h, yi q11k1h )
f ( xi , yi ) f ( xi , yi )
k2 f ( xi , yi ) p1h q11k1h
x y
34
We replace k1 and k2 in yi 1 yi (a1k1 a2 k2 )h to get
f ( xi , yi ) f ( xi , yi )
yi 1 yi a1 f ( xi , yi ) a2 f ( xi , yi ) p1h q11k1h h
x y
or f ( xi , yi )
yi 1 yi a1h f ( xi , yi ) a2 h f ( xi , yi ) a2 p1h 2
x
2 f ( xi , yi )
a2 q11 f ( xi , yi )h
y
Compare with f ( xi , yi ) f ( xi , yi ) h2
yi 1 yi f ( xi , yi )h f ( xi , yi )
x y 2!
a1 a2 1
1
a 2 p1
and obtain 2 (3 equations-4 unknowns)
1
a2 q11 # Note: p1=q11
2
35
Because we can choose an infinite number of values
for a2, there are an infinite number of second-order RK
methods.
Every version would yield exactly the same results if
the solution to ODE were quadratic, linear, or a
constant.
However, they yield different results if the solution is
more complicated (typically the case).
Three of the most commonly used methods are:
Know as RK2
Equivalent to Heun' s method with a single corrector
k1 f ( xi , yi )
k 2 f ( xi h , yi k1 h )
yi 1 yi k1 k 2
h
2
3 2
Local error is O( h ) and global error is O( h )
37
Higher-Order Runge-Kutta
38
3rd Order Runge-Kutta RK3
Know as RK3
k1 f ( xi , yi )
h 1
k 2 f ( xi , yi k1h )
2 2
k3 f ( xi h , yi k1h 2k 2 h )
yi 1 yi k1 4k 2 k3
h
6
39
4th Order Runge-Kutta RK4
k1 f ( xi , yi )
h 1
k 2 f ( xi , yi k1h )
2 2
h 1
k3 f ( xi , yi k 2 h )
2 2
k 4 f ( xi h , yi k3 h )
yi 1 yi k1 2k 2 2k3 k 4
h
6
40
Higher-Order Runge-Kutta
k1 f ( xi , yi )
1 1
k2 f ( xi h, yi k1h )
4 4
1 1 1
k3 f ( xi h, yi k1h k2h )
4 8 8
1 1
k4 f ( xi h, yi k2h k3h )
2 2
3 3 9
k5 f ( xi h, yi k1h k4h )
4 16 16
3 2 12 12 8
k6 f ( xi h, yi k1h k2h k3h k4h k5h )
7 7 7 7 7
yi 1 yi 7k1 32k3 12k4 32k5 7k6
h
90
41
Second order Runge-Kutta Method
Example
STEP 1 :
K1 h f ( t0 1, x0 4 ) 0.01( 1 x0 t0 ) 0.18
2 3
K 2 h f ( t0 h , x0 K1 )
0.01( 1 ( x0 0.18 )2 ( t0 .01 )3 ) 0.1662
x( 1 0.01 ) x( 1 ) K1 K 2 / 2
4 ( 0.18 0.1662 ) / 2 3.8269
42
STEP 2
43
Example
4th-Order Runge-Kutta Method RK4
dy
1 y x 2
dx
y (0) 0.5
h 0.2
Use RK 4 to compute y (0.2) and y (0.4)
44
Example: RK4
See RK4 Formula
Problem : h 0.2
dy
1 y x2 , y (0) 0.5 f ( x, y ) 1 y x 2
dx
x0 0, y0 0.5
Use RK 4 to find y (0.2), y (0.4)
k1 f ( x0 , y0 ) (1 y0 x02 ) 1.5
y1 y0
h
k1 2k2 2k3 k4 0.8293
6
45
Example: RK4
Problem :
h 0.2
dy
1 y x2 , y (0) 0.5 f ( x, y ) 1 y x 2
dx
x1 0.2, y1 0.8293
Use RK 4 to find y (0.2), y (0.4)
k1 f ( x1, y1 ) 1.7893
1 1
k2 f ( x1 h, y1 k1h ) 1.9182
2 2
Step 2
1 1
k3 f ( x1 h, y1 k2h ) 1.9311
2 2
k4 f ( x1 h, y1 k3h ) 2.0555
y2 y1
0.2
k1 2k2 2k3 k4 1.2141
6
46
Example: RK4
Problem :
dy
1 y x2 , y (0) 0.5
dx
Use RK 4 to find y (0.2), y (0.4)
47
48
RK method give a more accurate solution
compared to the simpler Eulers explicit
method.
The accuracy increases (i.e. truncation
error decreases) with increasing order.
49