def METHOD_NAME(z, z1, z2, M1, M2, M3):
    r"""Magnetic profile
    Return the following function:
    .. math::
        f(z) = \left{ \begin{array}{ll}
            C & \mbox{if } z < z_1 \\
            re^{kz} & \mbox{if } z_1 \leq z \leq z_2 \\
            az+b & \mbox{if } z > z_2
            \end{array} \right.
    where $C = M_1$, $r,k$ are set such that $re^{kz_1} = M_1$ and
    $re^{kz_2} = M_2$, and $a,b$ are set such that $az_2 + b = M_2$
    and $az_{\rm end} + b = M_3$.
    """
    # Make sure z1 > z2, swapping if they are different.  Note that in the
    # posterior probability this will set P(z1, z2)=P(z2, z1) always.
    if z1 > z2:
        z1, z2 = z2, z1
    C = M1
    k = (log(M2) - log(M1)) / (z2 - z1)
    r = M1/exp(k*z1)
    a = (M3 - M2) / (z[-1] - z2)
    b = M2 - a*z2
    part1 = z[z < z1]*0+C
    part2 = r*exp(k*z[(z >= z1)&(z <= z2)])
    part3 = a*z[z > z2] + b
    return hstack((part1, part2, part3))