def rk4(t, dt, x, deriv):
    """Fourth-order Runge-Kutta integrator.

    Parameters
    ----------
    t : float
        time
    dt : float
        time step
    x : float or broadcastable sequence
        state vector
    deriv : function(x, t)
        callback to compute xdot at time t

    Returns
    -------
    (x_new, t_new) : new state and time
    """
    k1 = dt * deriv(x, t)
    k2 = dt * deriv(x + k1 / 2, t + dt / 2)
    k3 = dt * deriv(x + k2 / 2, t + dt / 2)
    k4 = dt * deriv(x + k3, t + dt)
    return x + (k1 + 2 * (k2 + k3) + k4) / 6, t + dt