Runge Kutta

We are solving the equation 

       (dy/dt)  =  f(t,y).
       
This means that differentiation becomes the following:first differentiate, and then substitute (dy/dt) to f(t,y).  

Let's first consider the special case

    (dy/dx) = 1+cos(y t)

f[t_,y_]=1+Cos[y t]

DD[a_]:=Simplify[(∂_t)a/.y'[t]->f[t,y[t]]]

So let's try differentiating a few functions.

DD[y[t]]

DD[DD[y[t]]]

DD[DD[DD[y[t]]]]

Let's work out Taylor's series.  Let's do the first three terms.  This will give us a taylor method of order 3.

taylor=y[t]+h DD[y[t]]+(1/2) (h^2) DD[DD[y[t]]]+(1/6)(h^3)DD[DD[DD[y[t]]]] /. {t->t[i],y[t]->w[i]}

Now let's do the same thing with an indeterminate f(t,y):

Clear[f]
DD[a_]:=Simplify[(∂_t)a/.y'[t]->f[t,y[t]]]

DD[y[t]]

DD[DD[y[t]]]

DD[DD[DD[y[t]]]]

DD[DD[DD[DD[y[t]]]]]

Let's work out the Taylor's series.  Let's do the first four terms.

taylor=y[t]+h DD[y[t]]+(1/2) (h^2) DD[DD[y[t]]]+(1/6) (h^3) DD[DD[DD[y[t]]]]+(1/24) (h^4) DD[DD[DD[DD[y[t]]]]] /. {t->t[i],y[t]->w[i]}

Now we are going to try the Runge-Kutta method.

k1=h f[t[i],w[i]]
k2=h f[t[i]+(h/2),w[i]+(k1/2)]
k3=h f[t[i]+(h/2),w[i]+(k2/2)]
k4=h f[t[i]+h,w[i]+k3]
nextw=w[i]+(1/6) (k1+2 k2+2 k3+k4)

Let's expand it as a series.

seriesnextw=Simplify[Series[nextw,{h,0,4}]]

This should be the same as the fourth order Taylor method.  Let's check it out.

taylor-seriesnextw