# 4th-order Runge-Kutta method for coupled harmonic oscillator

I’m attempting to write a C program to gather values from a coupled spring system: There is a wall, connected to a mass $m_1$ by a spring, then this mass is connected to a second mass $m_2$ by another spring.

The values I require are the positions of the two masses and their velocities.

Using initial values for position and velocity I intend to calculate the movement of the two masses using the 4th-order Runge–Kutta method. I have successfully done this for positions, but I can’t figure out how to do it for the velocities. The differential equations for the velocities are:

\begin{alignat}{2} \dot v_1 &= -\omega_1^2(x_1-R_1) &+ \omega_2^2(x_2-x_1-w-R_2)\\ \dot v_2 &= &- \omega_2^2(x_2-x_1-w-R_2) \end{alignat}

Where $R$ is the rest length of the springs, $x$ is the position, $w$ is the width. The equations are correct; that isn’t the problem.

The problem I have is that I just don’t know how to express the Runge–Kutta algorithm for the velocities when both $x$ positions are changing, do I increase them both at the same time in the algorithm? Do I increase one and then take the average?

## migrated from physics.stackexchange.comDec 3 '13 at 16:44

This question came from our site for active researchers, academics and students of physics.

pressure is on the right track, but I will elaborate a bit.

You are solving a system of coupled ODEs, however it appears you are viewing this as two systems of two equations. You need to combine them so that your system looks like:

\left(\begin{array}{c} \dot{x}_1 \\ \dot{x}_2 \\ \dot{v}_1 \\ \dot{v}_2 \end{array}\right) = \left(\begin{alignat}{1} & v_1 \\ & v_2 \\ -\omega_1^2(x_1-R_1) &+ \omega_2^2(x_2-x_1-w-R_2)\\ &-\omega_2^2(x_2-x_1-w-R_2) \end{alignat}\right)

Solving this system of four ODEs with RK4 will solve for all your state variables simultaneously. Position and velocity do not need to be handled separately (and shouldn’t be).

Since your system is Hamiltonian, no frictions involved, you can also use the Newton-Stoermer-Verlet-Leapfrog (there were more inventors, see the paper of Hairer etal. mentioned in the wikipedia article) method for a fast simple method with reasonable physical properties. These so-called symplectic integrators treat position and velocity separately, in lockstep.

What you intend to do is not possible nor needed. Within Runge-Kutta you should already have an expression for your velocity. If not, you can calculate the velocity by using finite difference to approximate the first derivative of the position with respect to the time.