def trap_eq_factory(x_n, dt, f):
    """Factory function to return a function for evaluating the
    trapezoidal integration function. This returns a function of
    x_(n+1) (x at the next time step) for the given current x (x_n),
    time step (dt), and function (f).

    :param x_n: one-dimensional numpy array giving the values of x at
        the current time step.
    :param dt: Time step.
    :param f: Function of x.

    :returns: trap_eq, callable which takes x_(n+1) and evaluates the
        trapezoidal formula.
    """

    def trap_eq(x_n_1):
        """Simply write up the trapezoidal formula."""
        return x_n_1 - x_n - dt / 2 * (f(x_n) + f(x_n_1))

    # Return our new function.
    return trap_eq