You are on page 1of 49

Numerical Solutions of Ordinary

Differential Equations
5. Ordinary Differential Equations

Equations which are composed of an unknown


function and its derivatives are called differential
equations.

Differential equations play a fundamental role in


engineering because many physical phenomena are
best formulated mathematically in terms of their rate
of change.
dv c v- dependent variable
g v
dt m t- independent variable
2
When a function involves one dependent variable, the
equation is called an ordinary differential equation
(or ODE). A partial differential equation (or PDE)
involves two or more independent variables.

Differential equations are also classified as to their


order.
A first order equation includes a first derivative as its
highest derivative.
A second order equation includes a second derivative.

Higher order equations can be reduced to a system of


first order equations, by redefining a variable.

3
The differential equations will be replaced with algeb-
raic equations whose solutions approximate those of the
given differential equations
More differential examples:

d 2s Free Falling Body


2
g
dt

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

Given equations like these we would like to solve them.


Solutions to Equations
The solution to a differential equation (unlike an algebraic equation) is a function. The
problem with this is there are many ways to describe functions. Some can be described in
terms of some equation relating the dependent and independent variable, some by a
graph and some by a very complicated rule.
The way we will solve a differential equation is to use the definition of a function.
That is to say if we are given a differential equation y(x) = f(x,y) with a boundary
condition y(x0) = y0 and another value of x, say xact we can find a number yact so that
y(xact) = yact.

The problem that exists is that yn can not be


exactly computed only estimated. There are yact
several different ways in which to estimate the
y(x) (xact,yact)
value for yn. We will study a couple of them in
this section.
y0
The point (x0,y0) is called the initial point and
(x0,y0)
the point (xact,yact) is called the terminal
point.
x0 xact
5.1. Initial Value Problem
5.1.1. Eulers Method
The method that Euler used to estimate a solution (i.e. the corresponding value of y for a
given value of x) of a differential equation was to follow the tangent line from the initial
point to the terminal point. y n
Here we use the value yn to estimate the value yact
of yact. This can be directly computed from the (xact,yact)
information given by the following equation. y(x)

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

Et xT x A where, xT true value


xA approximate 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

conditions find the value of y(2) using Eulers method with 4


iterations. y (0) 1
1. x0 0 and y0 1 and x4 2
The actual solution is:
2. h x 2 0
12
4 1 x3

1
3.
x 0 1
2 1
2 ye 3


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

we can use Eulers method with h = 0.1 to approximate


the solution at t = 0.1, 0.2, 0.3, 0.4, as shown below.

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)

We can find the exact solution to our IVP,

y 9.8 0.2 y, y (0) 0


y 0.2 y 49
dy
0.2dt
y 49
ln y 49 0.2t C
y 49 ke0.2t , k e C
y (0) 1 k 49

y 49 1 e 0.2t
Example 2: Error Analysis (3 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

t Exact y Approx y Error % Rel Error


0.00 0 0.00 0.00 0.00
0.10 0.97 0.98 -0.01 -1.03
0.20 1.92 1.94 -0.02 -1.04
0.30 2.85 2.88 -0.03 -1.05
0.40 3.77 3.8 -0.03 -0.80
Example 3: Eulers Method (1 of 3)

For the initial value problem


y 4 t 2 y, y(0) 1
we can use Eulers method with h = 0.1 to approximate the
solution at t = 1, 2, 3, and 4, as shown below.
y1 y0 f 0 h 1 4 0 (2)(1) (0.1) 1.6
y2 y1 f1 h 1.6 4 0.1 (2)(1.6) (0.1) 2.31
y3 y2 f 2 h 2.31 4 0.2 (2)(2.31) (0.1) 3.15
y4 y3 f 3 h 3.15 4 0.3 (2)(3.15) (0.1) 4.15

Exact solution :
7 1 11
y t e 2t
4 2 4
Example 3: Error Analysis (2 of 3)

The first ten Euler approxs are given in table below on


left. A table of approximations for t = 0, 1, 2, 3 is given
on right. See text for numerical results with h = 0.05,
0.025, 0.01.
The errors are small initially, but quickly reach an
unacceptable level. This suggests a nonlinear
solution.
t Exact y Approx y Error % Rel Error t Exact y Approx y Error % Rel Error
0.00 1.00 1.00 0.00 0.00 0.00 1.00 1.00 0.00 0.00
1.00 19.07 15.78 3.29 17.27
0.10 1.66 1.60 0.06 3.55
2.00 149.39 104.68 44.72 29.93
0.20 2.45 2.31 0.14 5.81
3.00 1109.18 652.53 456.64 41.17
0.30 3.41 3.15 0.26 7.59 4.00 8197.88 4042.12 4155.76 50.69
0.40 4.57 4.15 0.42 9.14
0.50 5.98 5.34 0.63 10.58
0.60 7.68 6.76 0.92 11.96
0.70 9.75 8.45 1.30 13.31
Exact Solution :
0.80 12.27 10.47 1.80 14.64 7 1 11
0.90 15.34 12.89 2.45 15.96 y t e 2t
1.00 19.07 15.78 3.29 17.27
4 2 4
Example 3: Error Analysis & Graphs (3 of 3)

Given below are graphs showing the exact solution (red)


plotted together with the Euler approximation (blue).

t Exact y Approx y Error % Rel Error Exact Solution :


0.00 1.00 1.00 0.00 0.00
7 1 11
1.00 19.07 15.78 3.29 17.27 y t e 2t
2.00 149.39 104.68 44.72 29.93 4 2 4
3.00 1109.18 652.53 456.64 41.17
4.00 8197.88 4042.12 4155.76 50.69
5. 1.2. Improvements of Eulers method

A fundamental source of error in Eulers


method is that the derivative at the beginning
of the interval is assumed to apply across the
entire interval.
Two simple modifications are available to
circumvent this shortcoming:
Heuns Method
The Midpoint (or Improved Polygon) Method
(Reading Assignment)
22
5.1.2.1. Heuns Method/improved eulers method
One method to improve the estimate of the slope
involves the determination of two derivatives for the
interval:
At the initial point, y(true)
At the end point, y(dummy)
The two derivatives are then averaged to obtain an
improved estimate of the slope for the entire interval.

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).

Again, let h 0.1


f ( x0 , y0 ) y0 x0 0.5 0 0.5

y 0.5 0.1 0.5 0.55


*
1

f ( x1 , y ) y x1 0.55 0.1 0.45


*
1
*
1

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

y 0.5475 0.1 0.4475


*
2

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)

Runge-Kutta methods achieve the accuracy of a Taylor


series approach without requiring the calculation of
higher derivatives.
yi 1 yi ( xi , yi , h)h
a1k1 a2 k 2 an k n Increment function
a' s constants
k1 f ( xi , yi )
k 2 f ( xi p1h, yi q11k1h) ps and qs are constants
k3 f ( xi p3h, yi q21k1h q22k 2 h)

k n f ( xi pn 1h, yi qn 1k1h qn 1, 2 k 2 h qn 1,n 1k n 1h)
32
ks are recurrence functions. Because each k is a functional
evaluation, this recurrence makes RK methods efficient for
computer calculations.
Various types of RK methods can be devised by employing
different number of terms in the increment function as
specified by n.
First order RK method with n=1 is in fact Eulers method.
Once n is chosen, values of as, ps, and qs are evaluated by
setting general equation equal to terms in a Taylor series
expansion.

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:

Improved Euler method (a2=1/2, a1=1/2, p1=1, q11=1)


The Midpoint Method (a2=1, a1=0, p1=1/2, q11=1/2)
Heuns Method (a2=3/4, a1=1/4, p1=2/3, q11=2/3)
36
2nd Order Runge-Kutta RK2

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

Higher order Runge-Kutta methods are available.

Derived similar to second-order Runge-Kutta.

Higher order methods are more accurate but


require more calculations.

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

Solve the following systemto find x(1.02) using RK2


x ( t ) 1 x 2 t 3 , x( 1 ) 4 , h 0.01,

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

K1 h f (t1 1.01, x1 3.8269) 0.01(1 x12 t13 ) 0.1668


K 2 h f (t1 h, x1 K1 )
0.01(1 ( x1 0.1668)2 (t1 .01)3 ) 0.1546

x (1.01 0.01) x (1.01) K1 K 2


1
2
1
3.8269 (0.1668 0.1546) 3.6662
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

k2 f ( x0 h, y0 k1h ) 1 y0 0.15 x0 0.12 1.64


1 1
2 2
Step 1

k3 f ( x0 h, y0 k2h ) 1 y0 0.164 x0 0.12 1.654


1 1
2 2
k4 f ( x0 h, y0 k3h ) 1 y0 0.16545 x0 0.2 2 1.7908

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)

Summary of the solution


xi yi
0.0 0.5
0.2 0.8293
0.4 1.2141

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

You might also like