You are on page 1of 12

Example:

Convert a high order ODE into a system of 1st order ODEs and solve the system with R-K 4th Order
Method

Step-1: Convert the following high-order ODE to a system of first order equations:

() + 4 () + 5 () + 2() = 2 2 + 10 + 8
ICs

(0) = 1, (0) = 1, (0) = 3

Eq #1
Eq#2

Notation can be simplified to:


+ 4 + 5 + 2 = 2 2 + 10 + 8
ICs

=1
@ =0
= 1 @ = 0
= 3 @ = 0

Eq #1

Eq#2

Before doing anything, lets realize that the original 3rd order ODE will yield 3 first order
simultaneous ODEs.
Now lets define 3 new variables and obtain a 1st order ODE for each one of them:
Define new
variables
=
= =
= =

ODE for X, Y, Z:
Eq#3
Eq#4
Eq#5

=
=
=

Eq#6
Eq#7
Eq#8

Need to develop the ODE for (Eq#8) in terms of the new variables X, Y and Z. Starting with the
original Eq#1, plug in Eq#3, 4, 5 and 8:

+ 4 + 5 + 2 = 2 2 + 10 + 8

+ 4Z + 5Y + 2X = 2 2 + 10 + 8
Rearranging Eq#9:

= 4Z 5Y 2X + 2 2 + 10 + 8
With Eqs #6, 7 and 10 we assemble the set of symultaneous ODEs:
ODEs for X, Y, Z:
=
=
= 4 5 2 + 2 2 + 10 + 8

Eq#6
Eq#7
Eq#10

Eq#10

Eq#9

Lets develop the initial conditions (ICs) for each ODE in the system (i.e., one for each equation in X,
Y and Z):

ICs:

=1

@ =0

=1

@ =0

= 1 @ = 0

= 1 @ = 0

= 3 @ = 0

=3 @ =0

A complete system of Equations are obtained:


ODEs for X, Y, Z:

ICs:

=
=
= 4 5 2 + 2 2 + 10 + 8

=1 @ =0
= 1 @ = 0
=3 @ =0

Eq#6
Eq#7
Eq#10

Step 2: Numerical Solution 4th Order with R-K Scheme


Lets develop a simpler notation (i.e., one suitable for computer code):

Equation
X
Y
Z

Runge-Kutta Functions
K1,K2,K3,K4
L1,L2,L3,L4
M1,M2,M3,M4

Applying R-K 4th order to each equation, the order of computation follows:

1 = 1( , , , )
1 = 2( , , , )
1 = 3( , , , )

2 = 1(+/2 ,

)
, ,
+ 2 1 + 2 1 + 2 1

2 = 2(+/2 ,

)
, ,
+ 2 1 + 2 1 + 2 1

2 = 3(+/2 ,

)
, ,
+ 1 + 1 + 1
2
2
2

3 = 1(+/2 ,

)
, ,
+ 2 2 + 2 2 + 2 2

3 = 2(+/2 ,

)
, ,
+ 2 2 + 2 2 + 2 2

3 = 3(+/2 ,

)
, ,
+ 2 2 + 2 2 + 2 2

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

,
,
)
,
+ 2 +3 +3 +3

And the solution for each equation is:


1 2 3 4
+
+
+ )
6
3
3
6
1 2 3 4
+1 = + ( +
+
+ )
6
3
3
6
1 2 3 4
+1 = + (
+
+
+
)
6
3
3
6
+1 = + (

Assignment:
Solve the high-order ODE with R-K 4th Order.
() + 4 () + 5 () + 2() = 2 2 + 10 + 8
ICs

(0) = 1, (0) = 1, (0) = 3

Specifications
1. t=[0,3], h=0.1 and h=0.05 (two runs)
2. Modify the MATLAB code for a R-K single equation in Appendix

Eq #1
Eq#2

APPENDIX: 4th RUNGE-KUTTA METHOD w/ MATLAB


The Runge-Kutta method is the most popular method for solving ordinary differential equations
(ODEs) by means of numerical approximations. There are several version of the method
depending on the desired accuracy. The most popular Runge-Kutta method is of fourth order,
which in simpler terms means that the error is of the order of h4 , abbreviated as O(h4).
We will attempt to solve a simple differential equation with the simplest Runge-Kutta method
available. The formulas for the fourth-order Runge-Kutta are

where
h = (b-a)/n, the step size of the independent variable
n = the number of values of the independent variable in which we wish to calculate the
approximate solution
[a, b] = the limits of the x interval.
k1, k2, k3, k4 Runge-Kutta parameters or functions
f(ti,yi)=function evaluated at xi and yi

1
1
1
1
f (t i h, yi k1 ) function evaluated at ti h, yi k1
2
2
2
2
In order to calculate a new point in the solution yi+1 you need the previous solution yi and k1 , k2
, k3 , and k4 in that order. Then the calculation sequence is k1, k2, k3, k4, and then yi+1.

EXAMPLE-1
Below a MATLAB program to implement the fourth-order Runge-Kutta method to solve

y' 3et 0.4 y, y(1) 5 on t [0,3] .


For h= 0.1; The RK method is highly accurate for small h.

% Runge-Kutta 4th order with MATLAB


% It calculates an ODE using Runge-Kutta 4th order method
% Equation to solve: Y'=3*exp(-X)-0.4*Y; Y(1)=5; X=[0,3];
% Author Ido Schwartz/ Marco Arocha
clc, clear all, close all
% instruction to write results on external file
fid=fopen('RKout.m','w');
h=0.1; a=0; b=3;
x = a:h:b;
y = zeros(1,length(x));

% h is the step size


% Calculates up to y(3)

y(1) = 5;
% initial condition
Fxy = @(t,r) 3.*exp(-t)-0.4*r;
% change the function as you desire
% table title
fprintf(fid,'%7s %7s %7s %7s %7s %7s %7s \n','i','x(i)','k1','k2','k3', 'k4','y(i)');

for ii=1:1:length(x)
k1 = Fxy(x(ii),y(ii));
k2 = Fxy(x(ii)+0.5*h,y(ii)+0.5*h*k1);
k3 = Fxy((x(ii)+0.5*h),(y(ii)+0.5*h*k2));
k4 = Fxy((x(ii)+h),(y(ii)+h*k3));
y(ii+1) = y(ii) + (h/6)*(k1+2*k2+2*k3+k4); % main equation
fprintf(fid,'%7d %7.2f %7.3f %7.3f',ii, x(ii), k1, k2);
fprintf(fid,' %7.3f %7.3f %7.3f \n', k3, k4, y(ii));
end
y(length(x))=[ ];

% erase the last computation of y(n+1)

plot(x,y,' ok')
title('RK-O(h4)---Numerical Solution---');
ylabel('y'); xlabel('x'); legend('numerical');
grid on
fclose(fid);

MATLAB solution
RKout.m (output file):
i
1
2
3
4
5
6
7
8
9
10
11
12
13

x(i)
0.00
0.10
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
1.00
1.10
1.20

k1
1.000
0.681
0.401
0.156
-0.057
-0.242
-0.402
-0.540
-0.658
-0.758
-0.842
-0.912
-0.969

k2
0.834
0.535
0.273
0.045
-0.154
-0.326
-0.475
-0.602
-0.711
-0.802
-0.879
-0.942
-0.994

k3
0.837
0.538
0.276
0.047
-0.152
-0.324
-0.473
-0.601
-0.709
-0.801
-0.878
-0.942
-0.993

k4
0.681
0.401
0.156
-0.057
-0.242
-0.402
-0.540
-0.658
-0.758
-0.842
-0.912
-0.969
-1.015

y(i)
5.000
5.084
5.138
5.165
5.170
5.155
5.122
5.075
5.015
4.944
4.864
4.776
4.682

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

1.30
1.40
1.50
1.60
1.70
1.80
1.90
2.00
2.10
2.20
2.30
2.40
2.50
2.60
2.70
2.80
2.90
3.00

-1.015
-1.052
-1.080
-1.100
-1.113
-1.121
-1.123
-1.121
-1.115
-1.105
-1.093
-1.078
-1.061
-1.042
-1.022
-1.001
-0.979
-0.956

-1.035
-1.067
-1.091
-1.107
-1.118
-1.122
-1.122
-1.118
-1.110
-1.099
-1.086
-1.070
-1.052
-1.032
-1.012
-0.990
-0.967
-0.944

-1.035
-1.067
-1.090
-1.107
-1.117
-1.122
-1.122
-1.118
-1.110
-1.099
-1.086
-1.070
-1.052
-1.033
-1.012
-0.990
-0.968
-0.944

-1.052
-1.080
-1.100
-1.113
-1.121
-1.123
-1.121
-1.115
-1.105
-1.093
-1.078
-1.061
-1.042
-1.022
-1.001
-0.979
-0.956
-0.932

4.583
4.479
4.372
4.263
4.153
4.041
3.929
3.817
3.705
3.594
3.484
3.375
3.268
3.163
3.060
2.959
2.860
2.763

Next, we show the plot of the numerical solution versus x. As the numerical solution occurs
only at discrete points, the typical convention is to plot the solution with marks (no line, in this
case small circles) as it just occurs at discrete points.
RK-O(h4)---Numerical Solution--5.5
numerical
5

4.5

3.5

2.5

0.5

1.5
x

2.5

You might also like