# 4th order Runge-Kutta method

I am struggling with the following question regarding the 4th order Runge-Kutta method. I wish to find an approximate solution to the ODE:

$$\frac{dx}{dt} = f(x)$$

using the 4th Order Runge Kutta method:

\begin{aligned} k_1 &= h f(x(t), t)\\ k_2 &= h f(x(t) + \frac{k_1}{2}, t + \frac{h}{2})\\ k_3 &= hf(x(t) + \frac{k_2}{2}, t + \frac{h}{2})\\ k_4 &= hf(x(t) + k_3, t + h)\end{aligned}

$$x(t + h) = x(t) + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4)$$

If $$x(t)$$ obeys the ODE:

$$\frac{dx}{dt} = (x + 1)t$$

with initial condition $$x(0) = 0$$

1. Find an analytic expression for $$x(t)$$? (Hint: use the substitution $$y(t) = x(t) \exp({\frac{-t^2}{2}})$$)

2. Compute an approximate solution $$x(h)$$ for one RK4 iteration with step size h neglecting terms at $$O(h^6)$$.

You have an ODE $$\frac{dx}{dt} = f(t,x) = (x+1)t$$ which separates to $$\frac{dx}{x+1} = tdt\\ \log |x+1| + C = \frac{t^2}{2}\\ x = C_1 e^{t^2/2} - 1.$$ With $x(0) = 0$ that gives $C_1 = 1$ and $$x(t) = e^{t^2/2} - 1.$$

Computing the first step we get \begin{aligned} k_1 &= hf(0, 0) = 0\\ k_2 &= hf\left(\frac{h}{2}, \frac{k_1}{2}\right) = h\frac{h}{2}\\ k_3 &= hf\left(\frac{h}{2}, \frac{k_2}{2}\right) = h\frac{h(h^2 + 4)}{8}\\ k_4 &= hf\left(h, k_3\right) = h\frac{h(8+4h^2+h^4)}{8}\\ x_{1} &= \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4) = \frac{1}{48} h^2 \left(h^4+6 h^2+24\right) = \\ &= \frac{h^2}{2} + \frac{h^4}{8} + O(h^6). \end{aligned} And the exact solution is $$x(h) = e^{h^2/2} - 1 = \frac{h^2}{2} + \frac{h^4}{8} + O(h^6).$$ This agrees with the fact that method is of fourth order, thus the local truncation error is $O(h^5)$.

Hint:

1. $x(t) = c_1 e^{\frac{t^2}{2}}-1$,

2. Mathematica gives the following. It remains to compute it:

$$x'(t) = f(t, x) = t (x(t)+1), x(0) = 0$$ $$x_{n + 1} = x_n + k_1/6+k_2/3+k_3/3+k_4/6$$ $$t_{n + 1} = t_n + h$$ $$k_1 = h f(t_n, x_n)$$ $$k_2 = h f(t_n + h/2, x_n + (h k_1)/2)$$ $$k_3 = h f(t_n + h/2, x_n + (h k_2)/2)$$ $$k_4 = h f(t_n + h, x_n + h k_3)$$ $$x_0 = 0$$ $$t_0 = 0$$

Runge-Kutta Method is a way to generate a iterative sequence. So I'll treat x(0)=0 as $x_0 = 0$

Let h = 0.5;(thus $\frac{h}{2}=0.25$); $$x_{n+1} = x_n + (0.5) [\frac{1}{6}(k_1+2 k_2 + 2 k_3 + k_4)]$$ $$k_1=(0.5)[(0+1)0]=0$$ $$k_2=(0.5)[(0+\frac{0}{2}+1)(0+0.25)]=0.125$$ $$k_3=(0.5)[(0+\frac{0.125}{2}+1)(0+0.25)]=0.1328125$$ $$k_4=(0.5)[(0+0.132813+1)(0+0.5)]=0.283203125$$ Thus, $$x_1=0 + (0 + 2*0.125 + 2*0.1328125 + 0.283203125)/6 =0.13313802083333331$$ Furthermore: $$x_2=0.6485277017, x_3 = 2.07797616, x_4=6.366803294$$ Ergo, the function's approximations would be the following: $$f(0)=0, f(0.5)=0.13,f(1)=0.65,f(1.5) = 2.08, f(2)=6.37$$ I made this spreadsheet; you can change the value of h for a more accurate approximation.