You are on page 1of 37

Solution of Ordinary Differential

Equations

(Initial Value Problems)

Runge-Kutta Methods
Chapter 25
This chapter is devoted to solving first-order
ordinary differential equations (linear/non-linear)
of the form:

dy
f ( x, y ) with initial condition y ( X 0 ) Y0
dx
This type of problem is called IVP (Initial Value
Problem).
[IVPs provides all the conditions (values) at some initial points of the independent variables]
2

Eulers Method

dy
yi 1 yi
( xi 1 xi )
dx i

dy
f ( x, y )
dx

The first derivative provides a direct estimate of the


slope at xi
dy
f ( xi , yi )
dx i

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

+ = + ,
Each new iteration for +1 is done by adding step size
with the previous iteration, i.e., the iterations must
4
progress one step size (constant) at a time.

Eulers Method An Example


Solve the following ordinary differential equation using
Eulers method to estimate y(0.5) with a step size of 0.25.

=+
0 =1

Solution: We have, +1 = + , .
where, , = + = 0.25
Iteration-1: = 0, = 1, , = 0, 1 = 1
+ = 0+0.25 = 0 + 0, 1
. = 1 + 1 0.25 = .
Iteration-2: = 0.25, = 1.25, , = (0.25, 1.25) = 1.50
+ = 0.25+0.25 = 0.25 + 0.25, 1.25
. = 1.25 + 1.50 0.25 = .
5

Eulers Method An Example


The exact solution is :
So
Error% =

= 2 1
. = .

100%

1.7974 1.625
=
100% = 9.6%
1.7974

Now lets estimate (0.5) with = .


yi
f(xi, yi ) = xi+yi
. Itera- xi
tion
0

1.0

1.0

0.1

1.1

1.2

0.2

1.2200

1.4200

0.3

1.3620

1.6620

0.4
0.5

1.5282
1.7210 (Error 4.2%)

1.9282
2.2210

Eulers Method Exercises


Use the simple Eulers method to solve the following equations
to estimate (1) using = 0.5, 0.25 0.1 and find the
Error% in each case comparing the numerical result with the
exact value.
(i)
(ii)
(iii)
(iv)

(v)

= 2 3 + 12 2 20 + 8.5

0 =1

= 2

0 =1

= 2 + 2

0 =2

2+1

= + +

0 =1

0 =1
7

dy
f ( x, y ) 2 x 3 12x 2 20x 8.5
dx
Starting point x0 0, y0 1

yi 1 yi f ( xi , yi )h 1 8.5 * 0.5 5.25

Not good

Error Analysis for Eulers Method/


Numerical solutions of ODEs involves two types of
error:
Truncation error
Local truncation error

f ( xi , yi ) 2
Ea
h
2!
Ea O ( h 2 )
Propagated truncation error

The sum of the two is the total or global truncation error


Round-off errors
9

The Taylor series provides a means of quantifying the


error in Eulers method. However;
The Taylor series provides only an estimate of the local
truncation error-that is, the error created during a single
step of the method.
In actual problems, the functions are more complicated
than simple polynomials. Consequently, the derivatives
needed to evaluate the Taylor series expansion would not
always be easy to obtain.

In conclusion,
the error can be reduced by reducing the step size
If the solution to the differential equation is linear, the
method will provide error free predictions as for a straight
line the 2nd derivative would be zero.
10

Figure 25.4

11

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
12

Heuns Method/
One method to improve the estimate of the slope
involves the determination of two derivatives for the
interval:
At the initial point
At the end point

The two derivatives are then averaged to obtain an


improved estimate of the slope for the entire interval.

Predictor :

yi01 yi f ( xi , yi )h

f ( xi , yi ) f ( xi 1 , yi01 )
Corrector : yi 1 yi
h
2
13

Heuns Method/
Predictor :

yi01 yi f ( xi , yi )h

f ( xi , yi ) f ( xi 1 , yi01 )
Corrector : yi 1 yi
h
2

14

The Midpoint (or Improved


Polygon) Method
Uses Eulers method to predict a
value of y at the midpoint of the
interval:

+1 = + ,
2

+1 = + (

1, 1)
+2 +2

15

Heuns Method An Example


Lets estimate (0.5) with = 0.25 from the same
example shown with Eulers method:

=+
0 =1

Iteration

xi

yi

f(xi, yi )

1.25

0.25

1.5

0.25

1.56

1.7031

0.5

2.2031

0.50

1.3125
1.7832
Error
0.8%

y0i+1 xi+1

f(xi+1, y0i+1)

16

Midpoint Method An Example


Lets estimate again (0.5) with = 0.25 from the same
example shown with Eulers method:

=+
0 =1

Iteration

xi

yi

0.25

1.3125
1.7832
0.50 Error 0.8%

f(xi, yi ) xi+1/2

yi+1/2

f(xi+1/2, yi+1/2 )

0.125

1.125

1.25

1.56

0.375

1.5078

1.8828

17

Heuns and Midpoint Methods Exercises


Use the Heuns and Midpoint methods to solve the following
equations to estimate (1) using = 0.5 0.25 and find
the Error% in each case comparing the numerical result with the
exact value.
(i)
(ii)
(iii)
(iv)

(v)

= 2 3 + 12 2 20 + 8.5

0 =1

= 2

0 =1

= 2 + 2

0 =2

2+1

= + +

0 =1

0 =1
18

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 i h

slope weighted averaged at i


i ( xi , yi , h) a1k1 a2 k 2 an k n order (number of Slope components)
a ' s predetermined constants (method - specific, i.e., independent of the problem)
k's problem - dependent constants to be calculated using the data from the problem
k1 f ( xi , yi )
ps and qs are also method-specific
k 2 f ( xi p1h, yi q11k1h)
pre-determined constants
k f ( x p h, y q k h q k h)
3

21 1

22 2

k 4 f ( xi p3 h, yi q31k1h q32 k 2 h q33k3 h)

k n f ( xi pn 1h, yi qn 1k1h qn 1, 2 k 2 h qn 1,n 1k n 1h)

19

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
20

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 a2 k 2 )h

f ' ( xi , yi ) 2
h
2!
f ( xi , yi ) f ( xi , yi ) dy
f ' ( xi , yi )

x
y
dx

However yi 1 yi f ( xi , yi )h
But

f ( xi , yi ) f ( xi , yi ) dy h 2
Then yi 1 yi f ( xi , yi )h

2!

y
dx

k1 f ( x i , yi )
k 2 f ( xi p1h, yi q11k1h)
We now expand k 2 f ( xi p1h, yi q11k1h)
f ( xi , yi )
f ( xi , yi )
k 2 f ( xi , yi )
p1h
q11k1h
x
y
21

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 )

2!

a1 a2 1

and obtain

1
2
1
a2 q11
2
a 2 p1

(3 equations-4 unknowns)
22

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:
Heuns Method with a Single Corrector (a2=1/2)
The Midpoint Method (a2=1)
Raltsons Method (a2=2/3)
23

Figure 25.14

24

Classical 4th order R-K Method


In this case, 13 unknowns (constants) with 11 equations:

yi 1 yi i h

a1 = 1/6, a2 =1/3, a3 = 1/3, a4 = 1/6

i ( xi , yi , h ) a k a2k2 a k a4k4
i
1 1

k f ( xi , yi )
i
1

i
3 3

p1 = 1/2,

q11 =1/2

p2 = 1/2,

q21 =0,

q22 = 1/2

p3 = 1,

q31 =0,

q32 = 0,

k2 f ( xi p1h, yi q11k1h )
i

k f ( xi p2h, yi q21k1h q22k2h )


i
3

q33 = 1

k4 f ( xi p3h, yi q31k1h q32k2h q33k3h )


i

25

Classical 4th order R-K Method


Thus the classical 4th order R-K method becomes:

1 = ,
2 =

,
2

1
2

2
+ , +
2
2
4 = ( + , + 3 )

3 =

1 + 22 + 23 + 4
=
+1 = +
6

In this method, for each new iteration (i+1) with increased stepsize, the values of ks and (weighted average of slope
components) are calculated at i, then estimate of +1 is made.
26

Classical 4th order R-K Method An Example


Finally, lets estimate (0.5) with = 0.25 from the same
example shown with Eulers method:

=+
0 =1

Itera
tion

xi

yi

k1 i

0.125

1.125

1.25

1.1563

1.2813

0.25

1.3203

1.5703

0.25

1.3180

1.5680

0.375

1.514

1.8890

1.5542

1.9292

0.50

1.8003

2.3003

i
i
xi+h/2 yi+k1h/2 k2 yi+k2h/2 k3

xi+h yi+k3h

1.7974
Error
0.50
0%
27

k4 i

Classical 4th order R-K Method Exercises


Use the classical 4th order R-K method to solve the following
equations to estimate (0.5) and (1) using = 0.25 and find
the Error% in each case comparing the numerical result with the
exact value.
(i)
(ii)
(iii)
(iv)

(v)

0 =1

0 =1

= +

0 =2

= +

0 =1

= 2

0 =1
28

Higher-Order/Simultaneous (Coupled)
Differential Equations
Given a second-order differential equation (IVP) as shown below:
2

2 2+3
+ 5 = 11

with initial conditions: 0 = 7,

=
We can set

2
Then
=
2

The given equation becomes

2
+ 3 + 5 = 11

0 = 13

29

Higher-Order/Simultaneous (Coupled)
Differential Equations
So we have two equations of the first order as follows:

= = , ,

0 =7

11 3 5
=
= (, , ) 0 = 13

2
Thus we can reduce the higher-order differential equations into
a set of first-order differential equations and then solve them
simultaneously using any of the Euler, Heun, Midpoint or R-K
methods.
[ Note that each step, both the equations are to be solved, (i.e., both y and z
are to be solved) before going to the next higher step ]
30

Higher-Order/Simultaneous (Coupled)
Differential Equations
Lets solve the following to estimate y(0.5) using Eulers method:
2
2 2

+ 5 = 11
0 = 7,

0 = 13

Solution: As we have from previous slide (let h = 0.25),

= = , ,

0 =7

11 3 5
=
= (, , )

0 = 13
31

Higher-Order/Simultaneous (Coupled)
Differential Equations
From Eulers Method, +1 = + 1 , , .
And
+1 = + 2 , , .
where, , , =

, , =

Iteration-1: ( = 0, = 7, = 13, 1 , , = 1 0, 7,13 =


13 and 2 , , = 2 0, 7,13 = 31.5 )

0+0.25 = 0 + 1 0, 7, 13
. = 7 + 13 0.25 = .
And

0+0.25 = 0 + 2 0, 7, 13
. = 13 + 31.5 0.25 = .
32

Higher-Order/Simultaneous (Coupled)
Differential Equations
From Eulers Method, +1 = + 1 , , .
And
+1 = + 2 , , .
where, , , =

, , =

Iteration-2: ( = 0.25, = 10.25, = 5.125, 1 , , =


1 0.25, 10.25, 5.125 = 5.125 and 2 , , =
2 0.25, 10.25, 5.125 = 29.0291)

0.25+0.25 = 0.25 + 1 0.25, 10.25, 5.125


. = 10.25 + 5.125 0.25 = .
[ . = . ]
And 0.25+0.25 = 0.25 + 2 0.25, 10.25, 5.125
. = 5.125 + 29.0291 0.25 = .

33

Higher-Order/Simultaneous (Coupled) Differential


Equations Practice Problems
Solve the following equations to estimate (0.2), Using Heuns
method with = 0.1.
(i)

2
2

+2

3 = 6

with initial conditions: 0 = 0,


(ii)

2
10 2

0 =1

+ 6 = 0

with initial conditions: 0 = 1,

0 =0

34

Higher-Order/Simultaneous (Coupled)
Differential Equations
2

+2
3 = 6
2

We have,

0 = 0,

= = , ,

0 =1

0 =0

= 6 + 3 2 = (, , )

0 =1

0
Heuns Method, +1
= + 1 , , .
0
And
+1
= + 2 , , .
where, , , = , , = +

Then, +1 = +

0
0
1 , , +1 +1 ,+1
,+1

35

Higher-Order/Simultaneous (Coupled)
Differential Equations
2

+2
3 = 6
0 = 0,
0 =1
2

Heuns Method (each iteration should complete predictor step for all the
variables and then go for corrector step)
0
Predictor Step: +1
= + 1
0
+1
= + 2

where, , , =

, , .
, , .

, , = +

Corrector Step:

+1

0
0
1 , , + 1 +1 , +1
, +1
= +
2

+1

0
0
2 , , + 2 +1 , +1
, +1
= +
2

.
36

Some Engineering Problems


Prob-1: The general equation relating to , , and
of a serial circuit is given by

+ =

Find the value of current after 2 , if


= 20 , = 50 and
= 240 . = 0 = 0.
[ Hints: 0 = 0; = 0.5; 2 = ? ]
Prob-2: An object with a mass of 10 kg is falling under the influence of earth gravity.
Find its velocity after 5 seconds if it starts from the rest. The object experiences a
retarding force equal to 0.25 of its velocity. [ Hints: 0 = 0; = 1; 5 = ? ]

= 0.25

Prob-3: A body is attached to a spring. The differential equation governing the


displacement of the body and time is given by
2

+
2
+ 5 = 0
2

Find 1.5 = 0.5 given that 0 = 2 and

0 = 4.

37

You might also like