RungeKutta4

ODE with the option RungeKutta4 numerically solves the initial value problem

displaymath1135

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

eqnarray868

for tex2html_wrap_inline1169 , where tex2html_wrap_inline1143 . 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}]



Example.

Solve the initial value problem

displaymath1138

on the interval 0<t<1 using the Runge-Kutta method with a step size of 0.1.

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]