def _switch(left, right, on, loc, k=100, m=None):
    """Approximates the switch function using gekko.

    The switch function returns left if `on <= loc`, and right otherwise.

    Parameters
    ----------
    left : gekko variable or intermediate
    right : gekko variable or intermediate
    on : gekko variable or intermediate
    loc : gekko variable or intermediate
    k : int >= 1, default = 100
        The quality of the switch approximation. Larger is better.
    m : GEKKO

    Returns
    -------
    fx : gekko intermediate
        An approximation of the switch function.
    """
    if m is None:
        raise Exception('Missing GEKKO model definition.')

    sig = 1 / (1 + m.exp(-k * (on - loc)))
    return m.Intermediate((1 - sig) * left + sig * right)