ODE with the option RungeKutta4 numerically solves the initial value problem
implementing the classical fourth-order Runge-Kutta method. The command form is RungeKutta4[f,{t0,Y0},h,steps][t,y]; it assumes a constant step size equal to h and number of steps equal to steps. RungeKutta4 is an iterative numerical procedure defined by
for
,
where
.
NestList[F,expr,n] gives a list of the results of applying F to expr 0 through n times.
Therefore,
NestList[rkmstep[f,{t,y},#,h]&,{t0,Y0},steps]
applies the pure function
rkmstep[f,{t,y},#,h]&
to {t0,Y0} 1 through steps times. The function rkmstep returns the next step in the iteration.
RungeKutta4[f_,{t0_,Y0_},h_,steps_][t_,y_]:=
NestList[rkmstep[f,{t,y},#,h]&,{t0,Y0},steps]
rkmstep[f_,{t_,y_},{tn_,yn_},h_]:=
Module[{k1,k2,k3,k4},
k1 = f /. nrules[{t,y},{tn,yn}];
k2 = f /.
nrules[{t,y},{tn + h/two,yn + h/two*k1}];
k3 = f /.
nrules[{t,y},{tn + h/two,yn + h/two*k2}];
k4 = f /. nrules[{t,y},{tn + h ,yn + h*k3}];
{tn + h,yn + h(k1 + two k2 + two k3 + k4)/six}]
Solve the initial value problem
on the interval 0<t<1 using the
Runge-Kutta method with a step size of 0.1.
Example.
RungeKutta4[t y + t y^2,{0,1},0.1,10][t,y]
ODE[{y' == t y(1 + y),y[0] == 1},y,{t,0,1},Method->RungeKutta4]