You are on page 1of 4

The 4th -order Runge-Kutta method for a 2nd order ODE -----------------------------------------------------By Gilberto E. Urroz, Ph.D., P.E.

January 2010 Problem description ------------------Consider the 2nd-order ODE: y" y y' 3 y sin x 1 y' 0 1

subject to the initial conditions: y 0

Variable substitution to form a system of ODEs: ---------------------------------------------This 2nd-order ODE can be converted into a system of two 1st-order ODEs by using the following variable substitution: u 1 y u 2 y'

with initial conditions: u 1 1 and u 2 1 2 y' at x 0 . is equivalent to: [Eq. 1]

The variable substitution u d u u 2 dx 1

while the ODE is re-written as: or: d u dx 2 u u 3 u

y"

y y' 3 y sin x [Eq. 2]

sin x

The system of equations [Eq. 1] and [Eq. 2] is transformed into the vector ODE: u 1 d dx u 2 2 3 u u

sin x

or,

d u f x , u , where u dx 2 3 u u

u u

1 2

and

f x, u

sin x

The initial conditions are

us

1 1

at

xs 0

We'll solve the ODEs in the interval: 0 x 20 using 100 intervals.

-1-

Solution (version 1): --------------------First, define the vector function f(x,u): 2 3 u u

f x, u

sin x 1 1

The initial conditions are: The end of the solution interval is: Use 100 intervals: Calculate the increment size, x: x eval xe xs n

xs xe n

0 20 100

us

x 0.2 xsol eval xs , xs x .. xe

Create the x solution vector:

The y-solution vector gets initialized as follows: usol us usol 1 1

The following "for" loop calculates the Runge-Kutta algorithm (version 1) to produce the solution: for k 1 .. n x0 eval xsol u0 xM K1 uM K2 uM K3 u1 x1 K4 u1

eval col usol , k eval x0 1 x 2

eval x f x0 , u0 1 eval u0 K1 2 eval x f xM , uM eval u0 1 K2 2

eval x f xM , uM eval u0 K3 eval xsol k 1 eval x f x1 , u1 eval u0

1 K1 2 K2 2 K3 K4 6 usol augment usol , u1 After completing the iterative process, the solution is stored in a row vector called "ysol". This vector can be transposed to put together the graph of the two solutions as illustrated here: usol usol T
-2-

M1 M2
3

augment xsol , col usol , 1 augment xsol , col usol , 2 y

-1 0 2 4 6 8 10 12 14 16 18 20

M1 M2 The blue line represents u[1]=y while the red line represents u[2] = dy/dx. Solution (version 2): --------------------First, define the vector function f(x,y): 2 3 u u

f x, u

sin x 1 1

The initial conditions are: The end of the solution interval is: Use 100 intervals: Calculate the increment size, x: x eval xe xs n

xs xe n

0 20 100

us

x 0.2 xsol eval xs , xs x .. xe

Create the x solution vector:

The y-solution vector gets initialized as follows: usol us usol 1 1

The following "for" loop calculates the Runge-Kutta algorithm (version 1) to produce the solution:

-3-

for k 1 .. n x0 eval xsol u0 x13 x23 K1

k 1 x 3 2 x 3

eval col usol , k eval x0 eval x0

eval x f x0 , u0 1 K1 u13 eval u0 3 K2 u23 K3 u1 x1 K4 u1 eval x f x13 , u13 eval u13 1 K2 3

eval x f x23 , u23 eval u0 K1 K2 K3 eval xsol k 1 eval x f x1 , u1 eval u0

1 K1 3 K2 3 K3 K4 8 usol augment usol , u1 After completing the iterative process, the solution is stored in a row vector called "ysol". This vector can be transposed to put together the graph of the two solutions as illustrated here: usol usol N1 N2 y T

augment xsol , col usol , 1 augment xsol , col usol , 2

-1 -2 0 2 4 6 8 10 12 14 16 18 20

N1 N2 The blue line represents u[1]=y while the red line represents u[2] = dy/dx.

-4-

You might also like