MATH 371 -- Numerical Analysis
Runge-Kutta Examples
November 26, 2001
The fourth-order Runge-Kutta method is a superior ODE method with
local truncation error
. Each step of the method involves 4 evaluations
of the slope function as follows:
We illustrate this with the initial value problem
y' =
y(0) = 1
In this case there is an elementary solution (first order linear
equation):
> f:=(t,y) -> 3*t^2-y:
> dsolve({diff(y(t),t)=f(t,y(t)),y(0)=1});
> ansol:=t->3*t^2-6*t+6-5*exp(-t):
Applying Runge-Kutta to find approximate solution
using
and 4 steps to get the solution at
.
> T[0] := 0: RKW[0]:=1:
> h:=.25:
> for i to 4 do
> k[1]:=f(T[i-1],RKW[i-1])*h;
> k[2]:=f(T[i-1]+h/2,RKW[i-1]+k[1]/2)*h;
> k[3]:=f(T[i-1]+h/2,RKW[i-1]+k[2]/2)*h;
> k[4]:=f(T[i-1]+h,RKW[i-1]+k[3])*h;
> RKW[i]:=RKW[i-1]+(k[1]+2*k[2]+2*k[3]+k[4])/6;
> T[i]:=T[i-1]+h;
> end do:
> RKpts:=[[T[0],RKW[0]],[T[1],RKW[1]],[T[2],RKW[2]],[T[3],RKW[3]],[T[4],RKW[4]]]:
Compare with Euler's Method and the exact solution
> T[0]:=0: EulerW[0]:=1: h:=.25:
> for i to 4 do T[i]:=T[i-1] + h; EulerW[i]:=EulerW[i-1]+f(T[i-1],EulerW[i-1])*h; end do:
The exact and approximate solution values
t Exact solution Euler approx. Runge-Kutta approx.
0 1 1 1
.25 .793496084 .75 .7935180663
.50 .717346702 .609375 .7173944413
.75 .825667236 .64453125 .8257417646
1.00 1.160602794 .9052734375 1.160703425
Note the largest RK error is about .0001 (!)
>