RK4 method applied to $\frac{dy}{dt}=-\frac{y-t}{2}$ with $y(0)=1$ I tried to solve this question, I did it by Huen's method also, but I'm getting very different results than the answers sheet.


  Use the fourth-order Runge-Kutta method (RK4) to solve the following initial-value problem
  $$\frac{dy}{dt}=-\frac{y-t}{2} \text{ with } y(0) = 1$$
  on the interval $[0,3]$ by taking $\Delta t=0.125,\;0.25,\;0.5,$ and $1$ and compare the solutions. You may want to write a computer code to conduct the calculations for the time steps $\Delta t &lt; 1$.
The Runge-Kutta algorithm can be found on the Wiki.

We are given the DEQ:

$$\frac{dy}{dt}=-\frac{y-t}{2} , y(0) = 1$$

We want to use Runge-Kutta-4 on the interval $[0,3]$ by taking $\Delta t=0.125,\;0.25,\;0.5,$ and $1$ and compare the solutions. You can fill in the details of the calculations.

For $\Delta t= 1$, we get $(t, y)$ values of:

$$\left(
\begin{array}{cc}
 0. &amp; 1. \\
 1. &amp; 0.5 \\
 2. &amp; 0.75 \\
 3. &amp; 1.375 \\
\end{array}
\right)$$

For $\Delta t= 0.5$, we get $(t, y)$ values of:

$$\left(
\begin{array}{cc}
 0. &amp; 1. \\
 0.5 &amp; 0.75 \\
 1. &amp; 0.6875 \\
 1.5 &amp; 0.765625 \\
 2. &amp; 0.949219 \\
 2.5 &amp; 1.21191 \\
 3. &amp; 1.53394 \\
\end{array}
\right)$$

For $\Delta t= 0.25$, we get $(t, y)$ values of:

$$\left(
\begin{array}{cc}
 0. &amp; 1. \\
 0.25 &amp; 0.875 \\
 0.5 &amp; 0.796875 \\
 0.75 &amp; 0.759766 \\
 1. &amp; 0.758545 \\
 1.25 &amp; 0.788727 \\
 1.5 &amp; 0.846386 \\
 1.75 &amp; 0.928088 \\
 2. &amp; 1.03083 \\
 2.25 &amp; 1.15197 \\
 2.5 &amp; 1.28923 \\
 2.75 &amp; 1.44057 \\
 3. &amp; 1.60425 \\
\end{array}
\right)$$

For $\Delta t= 0.125$, we get $(t, y)$ values of:

$$\left(
\begin{array}{cc}
 0. &amp; 1. \\
 0.125 &amp; 0.9375 \\
 0.25 &amp; 0.886719 \\
 0.375 &amp; 0.846924 \\
 0.5 &amp; 0.817429 \\
 0.625 &amp; 0.797589 \\
 0.75 &amp; 0.786802 \\
 0.875 &amp; 0.784502 \\
 1. &amp; 0.790158 \\
 1.125 &amp; 0.803274 \\
 1.25 &amp; 0.823381 \\
 1.375 &amp; 0.850045 \\
 1.5 &amp; 0.882855 \\
 1.625 &amp; 0.921426 \\
 1.75 &amp; 0.9654 \\
 1.875 &amp; 1.01444 \\
 2. &amp; 1.06822 \\
 2.125 &amp; 1.12646 \\
 2.25 &amp; 1.18887 \\
 2.375 &amp; 1.25519 \\
 2.5 &amp; 1.32518 \\
 2.625 &amp; 1.3986 \\
 2.75 &amp; 1.47525 \\
 2.875 &amp; 1.55492 \\
 3. &amp; 1.63743 \\
\end{array}
\right)$$

For this DEQ, we can use an Integrating Factor (or several other approaches), to find the closed-form solution:

$$y(t) =  3 e^{-t/2} + t - 2$$

If we compare the value at $t=3$, for each $\Delta t$, we have an error:

$$ 0.29439, 0.13545, 0.0651405, 0.0346605$$

You can do more comparisons as desired, but you see that the error is decreasing as $\Delta t$ is decreasing.
I'm sorry but I just feel it strange: why you take so much trouble to solve this equation in a numerical way since it is a very typical ODE that can be solved with the method of variation of constant?
Anyway I have got an explicit solution and I find it fits your ODE, which goes as follows:
$$y(t)=t+3e^{-\frac{1}{2}t}-2$$
Is this answer what you expect? I hope maybe you would find it helpful to you.