0

How can I apply 4th order Runge Kutta to a function that requires multiple inputs (instead of $\dot x = f(x, t)$, something like $\dot x = f(a, b, c, d, t)$). For $\dot x = f(x, t)$, the Runge Kutta system is as follows:

$x_{n+1} = x_n + \frac{h}{6}(f_1 + 2f_2 + 2f_3 + f4)$

where:

  • $f_1 = f(x_n, t_n)$
  • $f_2 = f(x_n + \frac{h}{2}f_2, t_n, \frac{h}{2}$
  • $f_3 = f(x_n + \frac{h}{2}f_2, t_n + \frac{h}{2}$
  • $f_4 = f(x_n + hf_3, t_n + h)$

This only works if the function only accepts two inputs, the state and timestep. Sorry if this is a simple question, but if you could direct me to any sources or answers, it would be greatly appreciated.

For context: I'm trying to simulate a double pendulum. The equations of motion for the double pendulum describe acceleration in terms of some constants, angles, and angular velocities (the four inputs for the state are the first angle, the second angle, the first angular velocity, the second angular velocity).

  • What do you mean by $x$ in $\dot x=f(a, b, x_1, x_2, t)$? Is it a vector with components $x_1$ and $x_2$? And if so, are $a$ and $b$ just constant parameters? – joriki Mar 22 '24 at 23:18
  • 1
    Essentially, treat $x$ as vector and $f$ as vector-valued. I once wrote a longer answer to this general topic. Please comment where it could be more helpful or appears inappropriate. https://math.stackexchange.com/questions/4176684/how-to-implement-a-runge-kutta-method-rk4-for-a-second-order-differential-equa/4177218#4177218. An example implementation https://stackoverflow.com/questions/68339204/double-pendulum-rk4/68340713#68340713, and a different one of similar complexity https://math.stackexchange.com/questions/2026118/approximating-spring-cart-pendulum-system/2026235#2026235 – Lutz Lehmann Mar 22 '24 at 23:35

0 Answers0