# Euler, Heun and Runge-Kutta

Im supposed to use Euler and Heun(improved Euler) method with step size h=0.5 and Runge-Kutta with step size h=1 to find y(2). Meaning three answers

$$\frac{dy}{dx}=2x,~~y(0)=1$$ First Using Eulers: $$y_1=1+\frac{1}{2}(2*0)=1$$ $$y_2=1+\frac{1}{2}(2*\frac{1}{2})=\frac{5}{4}$$ Then using improved Euler: $$y_1=1+\frac{1}{4}(2*0+2*\frac{1}{2})=\frac{5}{4}$$ $$y_2=\frac{5}{4}+\frac{1}{4}(2*\frac{1}{2}+2*1)=2$$

The using Runge-Kutta $$y_1=1+\frac{1}{6}(2*0+2*2*\frac{1}{2}+2*2*\frac{1}{2}+2*1)=2$$ $$y_2=2+\frac{1}{6}(2*1+2*2*\frac{3}{2}+2*2*\frac{3}{2}+2*2)=5$$

But it's not the right answer. Any ideas where it goes wrong?

• You didn't explicitly mention this, but is the initial condition $y(0)=0$? Nov 17, 2013 at 21:34
• Wait, sorry bout that,Yes y(0)=1 in fact. Nov 17, 2013 at 21:37
• Unsure, Im doing this as an online test and the only feedback I get is that the total answer is wrong, I get no indication of which part it is. Nov 17, 2013 at 22:05
• Where RK $$y_{n+1}=y_n+\frac{h}{6}(P_n+2q_n+2r_n+s_n)$$ Nov 17, 2013 at 22:12

If you're going to approximate $y(2)$ using Euler and Improved Euler, you're going to need to compute up to $y_4$ since your step size is $h=.5$. Let $f(x,y) = 2x$, $x_0=0$ and hence $x_i=x_0 + ih= .5i$. Given that the initial condition is $y(0)=1$, we have by Euler's Method that

$$y_1 = 1 + .5f(0,1) 1 + .5(2(0)) = 1$$

$$y_2 = 1 + .5f(.5,1) = 1+ .5(2(.5)) = 1.5$$

$$y_3 = 1.5 + .5f(1,1.5) = 1.5 + .5(2(1)) = 2.5$$

$$y_4 = 2.5 + .5f(1.5,2.5) = 2.5 + .5(2(1.5)) = 4$$

Thus $y_4=4$ is our approximation for $y(2)$.

Likewise, using Heun's (Improved Euler's) method

$$y_n = y_{n+1} = y_n + \frac{h}{2}\left(f(x_n,y_n) + f(x_n+h,y_n+hf(x_n,y_n))\right)$$

where $f(x,y) = 2x$, $x_0=0$, $x_i=.5i$ and $y_0=1$, computing up to $y_4$ (which I leave to you to verify) leaves us with

\begin{aligned}y_1 &= 1.25\\ y_2 &= 2\\ y_3 &= 3.25\\ y_4 &= 5\end{aligned}

And thus $y_4=5$ is our approximation for y(2)$. Finally, for Runge-Kutta, you only need to do two iterations since the step size is$h=1\$. Using the same set up as in the other two iteration methods, we see that $$y_1 = 1 + \frac{1}{6}\left(2(0) + 2[2(.5)]+ 2[2(.5)] + 2(1)\right) = 2$$

$$y_2 = 2 + \frac{1}{6}\left(2(1) + 2[2(1.5)] + 2[2(1.5)] + 2(2)\right) = 5$$

So your calculations for Runge-Kutta were correct.