def rk4(x, v, t, h, deriv):
    """
    Core Rk4 algo for calculating one step ahead.
    This version is limited to 2nd order eq's of the type
    dx^2/dt^2 + p(t,x,v)*dx/dt + q(t,x,v)*x = r(t,x,v)

    Input:
    - x:      x (t)
    - v:      v (t)
    - t:      Initial time (t_n)
    - h:      Stepsize (t_{n+1} = t_n + h)
    - deriv:  Pointer to a function that calculates
              and returns the derivatives of x and v
    Output:
    (x,v):    Tuple containing the calculated
              values of x and v at time t+h
    """

    (dxdt,dvdt) = deriv(t,x,v)
    kv1 = h*dvdt
    kx1 = h*dxdt

    (dxdt,dvdt) = deriv(t+h/2,x+kx1/2,v+kv1/2)
    kv2 = h*dvdt
    kx2 = h*dxdt
    
    (dxdt,dvdt) = deriv(t+h/2,x+kx2/2,v+kv2/2)
    kv3 = h*dvdt
    kx3 = h*dxdt

    (dxdt,dvdt) = deriv(t+h,x+kx3,v+kv3)
    kv4 = h*dvdt
    kx4 = h*dxdt

    x = x + (kx1 + 2*(kx2+kx3) + kx4)/6
    v = v + (kv1 + 2*(kv2+kv3) + kv4)/6

    return(x,v)