1

I am trying to solve a set of two 2nd order ODE using the 4th order Runge-Kutta method. The original equations are of the following form: $$\frac{d^2r}{dt^2}=f(r,\theta)$$ $$\frac{d^2\theta}{dt^2}=g(r,\theta)$$

Then, I expressed the above two 2nd order ODEs as four 1st order ODEs as follows: $$\frac{dr}{dt}=R \qquad\\ \frac{dR}{dt}=f(r,\theta)\\ \frac{d\theta}{dt}=\Theta \qquad\\ \frac{d\Theta}{dt}=g(r,\theta)$$

Now, I require to solve these four equations simultaneously using the 4th order Runge-Kutta method. According to the RK4 algorithm, I wrote the equations for a single iteration as follows:

$$k_1=hR_i \\ l_1=hf(r_i,\theta_i) \\ m_1=h\Theta_i \\ n_1=hg(r_i,\theta_i) \\ k_2=h(R_i+l_1/2) \\ l_2=hf(r_i+k_1/2,\theta_i+m_1/2) \\ m_2=h(\Theta_i+n_1/2) \\ n_2=hg(r_i+k_1/2,\theta_i+m_1/2) \\ k_3=h(R_i+l_2/2) \\ l_3=hf(r_i+k_2/2,\theta_i+m_2/2) \\ m_3=h(\Theta_i+n_2/2) \\ n_3=hg(r_i+k_2/2,\theta_i+m_2/2) \\ k_4=h(R_i+l_3) \\ l_4=hf(r_i+k_3,\theta_i+m_3) \\ m_4=h(\Theta_i+n_3) \\ n_4=hg(r_i+k_3,\theta_i+m_3) \\$$

Then the $r$ and $\theta$ variables can be calculated as $$r_{i+1}=r_i+\frac{1}{6}(k_1+2k_2+2k_3+k_4) \\ \theta_{i+1}=\theta_i+\frac{1}{6}(m_1+2m_2+2m_3+m_4)$$

Since there are dependencies in the numerical calculations, I am not sure whether the sequence in which the above equations are correct or not. All the individual equations written above are correct and I am only concerned with the sequence.

Can someone please check the sequence and suggest any changes if anything is incorrect?

Richard
  • 585
  • Yes, that is correct. You need to add the equations for $R_{i+1}$ and $Θ_{i+1}$. It is easier at this stage of advancement to switch to a vectorized approach, meaning there is only one vector-valued derivative function depending on one state vector. – Lutz Lehmann Jul 22 '22 at 11:16
  • @LutzLehmann Thank you so much. It would be helpful if you could you please provide some references to learn the vectorized approach that you have mentioned. – Richard Jul 22 '22 at 11:49
  • See https://math.stackexchange.com/questions/2026118/approximating-spring-cart-pendulum for an example, https://math.stackexchange.com/questions/4176684/how-to-implement-a-runge-kutta-method for a link collection. – Lutz Lehmann Jul 22 '22 at 12:33

0 Answers0