Runge Kutta With MATLAB
Runge Kutta With MATLAB
The formula for the fourth order Runge-Kutta method (RK4) is given below. Consider the
problem
(
y 0 = f (t, y)
y(t0 ) =
Define h to be the time step size and ti = t0 + ih. Then the following formula
w0 =
k1 = hf (ti , wi )
k1
h
k2 = hf ti + , wi +
2
2
h
k2
k3 = hf ti + , wi +
2
2
k4 = hf (ti + h, wi + k3 )
1
wi+1 = wi + (k1 + 2k2 + 2k3 + k4 )
6
computes an approximate solution, that is wi y(ti ).
Let us look at an example:
(
y 0 = y t2 + 1
y(0) = 0.5
The exact solution for this problem is y = t2 + 2t + 1 21 et , and we are interested in the value of
y for 0 t 2.
1. We first solve this problem using RK4 with h = 0.5. From t = 0 to t = 2 with step size
h = 0.5, it takes 4 steps: t0 = 0, t1 = 0.5, t2 = 1, t3 = 1.5, t4 = 2.
Step 0 t0 = 0, w0 = 0.5.
Step 1 t1 = 0.5
k1
k2
K3
K4
w1
Step 2 t2 = 1
k1
k2
K3
K4
w2
Step 3 t3 = 1.5
k1
k2
K3
K4
w3
Step 4 t4 = 2
k1
k2
K3
K4
w4
Numerical solution wi
0.5
1.425130208333333
2.639602661132812
4.006818970044454
5.301605229265987
2. Solve the problem using RK4 with h = 0.2. All you need to do is to replace h = 0.5; and
for i=1:4 in the above Matlab program into h = 0.2 and for i=1:10. Then we have
ti
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
1.6
1.8
2.0
Numerical solution wi
0.5
0.829293333333333
1.214076210666667
1.648922017041600
2.127202684947944
2.640822692728752
3.179894170232231
3.732340072854980
4.283409498318406
4.815085694579435
5.305363000692655
3. Solve the problem using RK4 with h = 0.05. Again, all need to do is to set h = 0.05 and
for i=1:40. Then we have
ti
0.0
0.05
0.10
1.80
1.85
1.90
1.95
2.00
4.815176267793529 4.815175898599096
4.942590238699086 4.942589852008494
5.067052778860367 5.067052374183828
5.188156209705356 5.188155786548850
5.305471950534677 5.305471508400809
0.000000369194433
0.000000386690592
0.000000404676539
0.000000423156505
0.000000442133868
4. Comparing the results By comparing the above results, we can see that
for h = 0.5,
for h = 0.2,
for h = 0.05,
This brings out a question. How to find the most appropriate step size, if we want to make sure the
error is less than a given , for example, = 0.00001?
5. Adaptive step size control and the Runge-Kutta-Fehlberg method The answer is, we will
use adaptive step size control during the computation. The idea is to start with a moderate step
size. When we detect the expected error is larger than , reduce the step size and recalculate the
current step. When we detect the expected error is less than , keep the current step and slightly
enlarge the step size in the next step. This requires us to have a good estimation of the expected
error.
if R
if R >
k2 = h*f(t+h/4, w+k1/4);
k3 = h*f(t+3*h/8, w+3*k1/32+9*k2/32);
k4 = h*f(t+12*h/13, w+1932*k1/2197-7200*k2/2197+7296*k3/2197);
k5 = h*f(t+h, w+439*k1/216-8*k2+3680*k3/513-845*k4/4104);
k6 = h*f(t+h/2, w-8*k1/27+2*k2-3544*k3/2565+1859*k4/4104-11*k5/40);
w1 = w + 25*k1/216+1408*k3/2565+2197*k4/4104-k5/5;
w2 = w + 16*k1/135+6656*k3/12825+28561*k4/56430-9*k5/50+2*k6/55;
R = abs(w1-w2)/h;
delta = 0.84*(epsilon/R)(1/4);
if R<=epsilon
t = t+h;
w = w1;
i = i+1;
fprintf(Step %d: t = %6.4f, w = %18.15f\n, i, t, w);
h = delta*h;
else
h = delta*h;
end
end
%%%%%%%%%%%%%%%%%%
function v = f(t,y)
v = y-t2+1;
Run this program, we see that starting with h = 0.2, the program outputs
Step
Step
Step
Step
Step
Step
Step
Step
Step
0:
1:
2:
3:
4:
5:
6:
7:
8:
t
t
t
t
t
t
t
t
t
=
=
=
=
=
=
=
=
=
0.0000,
0.2000,
0.4353,
0.6766,
0.9264,
1.1902,
1.4806,
1.8537,
2.0000,
w
w
w
w
w
w
w
w
w
=
=
=
=
=
=
=
=
=
0.500000000000000
0.829299076923077
1.287432405787216
1.827289794651997
2.448301479233138
3.153049280338359
3.955581050460808
4.952039512278185
5.305486816572746