# Runge-Kutta 4 with multiple equations and no time dependence

I'd like to believe that I understand the Runge-Kutta 4 method; however, I am having difficulty applying to non-standard cases.

Runge-Kutta 4:

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

I would have no issues (I think) if the equation I had was something like this: $$\frac{dx}{dt} = xt$$

But the problem I am trying to solve has this system of equations where A and B are just constants:

$$\frac{dx}{dt} = A + x^2y - (B+1)x \\ \frac{dy}{dt} = Bx - x^2y$$

I have tried something like this for the first equation:

$$k_1 = hf(x,y) \\ k_2 = hf(x + \frac{k_1}{2},y + \frac{h}{2}) \\ k_3 = hf(x + \frac{k_2}{2},y + \frac{h}{2}) \\ k_4 = hf(x + k_3,y + h) \\~\\ x(t+h) = x(t) + \frac{1}{6}​(k_1​+2k_2+2k_3+k_4​)$$

and for the second equation:

$$k_1 = hf(y,x) \\ k_2 = hf(y + \frac{k_1}{2},x + \frac{h}{2}) \\ k_3 = hf(y + \frac{k_2}{2},x + \frac{h}{2}) \\ k_4 = hf(y + k_3,x + h) \\~\\ y(t+h) = y(t) +​ \frac{1}{6}(k_1​+2k_2+2k_3+k_4​)$$

Though I don't (think) that it worked. I also tried something similar but instead of treating the "not main variable" (???) (x in $$\frac{dy}{dt} =~ ...$$ or y in $$\frac{dx}{dt} =~ ...$$) as a "time variable" (x in $$\frac{dy}{dx} =~ ...$$ or t in $$\frac{dy}{dt} =~ ...$$), I treated it as a constant. For the first equation:

$$k_1 = hf(x,y) \\ k_2 = hf(x + \frac{k_1}{2},y) \\ k_3 = hf(x + \frac{k_2}{2},y) \\ k_4 = hf(x + k_3,y) \\~\\ x(t+h) = x(t) + \frac{1}{6}​(k_1​+2k_2+2k_3+k_4​)$$

This didn't seem to work either. I would appreciate help in understanding how to solve these types of systems of equations. Somehow, I haven't been able to find anything that explains beyond the first case that I mentioned, and my textbook does not explain it either, or at least explain it well.

You need to treat the given system as a system. Thus $$f$$ is a vector-valued function with vector arguments. $$\pmatrix{\dot x\\\dot y}=f\left(\pmatrix{x\\y},t\right) =\pmatrix{A + x^2y - (B+1)x \\ Bx - x^2y}$$ Then the $$k$$ are also vectors, and the first version of RK4 applies again.

In this case of state space dimension 2 (also 3, no longer sensible for 5) you can also use an intermediate form of the method, where the equations are formalized as $$\dot x=f(x,y)$$, $$\dot y=g(x,y)$$ (note that the independent time variable is not present as argument). Then the RK4 step can be computed as

k1x = h*f(x,y)
k1y = h*g(x,y)
k2x = h*f(x+0.5*k1x, y+0.5*k1y)
k2y = h*g(x+0.5*k1x, y+0.5*k1y)
k3x = h*f(x+0.5*k2x, y+0.5*k2y)
k3y = h*g(x+0.5*k2x, y+0.5*k2y)
k4x = h*f(x+k3x, y+k3y)
k4y = h*g(x+k3x, y+k3y)
t = t + h
x = x + (k1x+2*k2x+2*k3x+k4x)/6
y = y + (k1y+2*k2y+2*k3y+k4y)/6