# 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 < 1$.

• Why didn't you try it with RK4? Juts Google it if you are lost. Jan 4, 2015 at 16:50

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. & 1. \\ 1. & 0.5 \\ 2. & 0.75 \\ 3. & 1.375 \\ \end{array} \right)$$

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

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

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

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

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

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

Here is some C# code for this to use as an example:

static Func<double, double, double> f=(t, y) => -(y-t)/2;

public static void SoTest()
{
double t0=0, y0=1, tf=1;
Func<double, double> ode=(h) =>
{
double t=t0, y=y0;
while (t<tf)
{
double K0=f(t, y);
double K1=f(t+h/2, y+h*K0/2);
double K2=f(t+h/2, y+h*K1/2);
double K3=f(t+h, y+h*K2);
t+=h;
y+=h*(K0+2*K1+2*K2+K3)/6;
}
return y;
};

double[] step = new double[] { 0.125, 0.25, 0.5, 1};
double[] result=step.Select(ode).ToArray();
// result = {0.81####,0.81####,0.81####,0.82####}
}


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.