def rk4(rhs, initial, t_initial, t_final, dt):
    """RK4 integrator.

    Inputs:
        - rhs: a callable that takes arguments (t, y)
        - initial: initial value
        - t_initial: initial time
        - t_final: final time
        - dt: step size

    Returns:
        The solution computed at the final time.
    """
    t = t_initial
    sol = initial

    while t < t_final:
        dt = min(dt, t_final - t)
        s0 = rhs(t, sol)
        s1 = rhs(t + dt/2, sol + dt/2 * s0)
        s2 = rhs(t + dt/2, sol + dt/2 * s1)
        s3 = rhs(t + dt, sol + dt * s2)
        sol = sol + dt / 6 * (s0 + 2 * s1 + 2 * s2 + s3)
        t += dt

    return sol