def fitCD( f, Z, p0, Cgeom=Cgeom ):
    """ find the capacitance and loss of a set of data (in f,Z form, where Z is complex)
        and fit this with Ashoori's model to find fpeak, Clow, Chigh, and the conductivity G
        fpeak, Clow, Chigh, G = fitCD( f, Z, p0, Cgeom=80.0 ) 
        where
        p0 = [fpeak0, Clow0, Chigh0] # initial guesses
    """
    lsq = fmin_plain( sumresidualsCD, p0, args=(Z, f), ftol=1e-8, xtol=1e-8, maxfun=2000, disp=False )
    fpeak, Clow, Chigh = abs(lsq)
        
    G = 2*pi*fpeak*Cgeom**2/sqrt(Clow*Chigh)*(Clow/Chigh - 1) * 1e-12   # see Eq. 2 of Ashoori's PRB
    return fpeak, Clow, Chigh, G
def fitZ( f, Zdata, p0, pfixed ):
    """ find the capacitance and loss of a set of data (in f,Z form, where Z is complex)
        and fit this with Ashoori's model to find fpeak, Clow, Chigh, and the conductivity G
        Ctj, Cq, tau = fitZ( f, Zdata, p0, pfixed ) 
        where
        p0 = [Ctj0, Cq0, tau0]     # initial guesses
        pfixed = [Cg, Cbp, Rs, Rp] # fixed parameters
    """
    lsq = fmin_plain( sumresidualsZ, p0, args=(pfixed, Zdata, f), ftol=1e-8, xtol=1e-8, maxfun=2000, disp=False )
    if len(pfixed) == 4:
        Ctj, Cq, tau = abs(lsq)
        return Ctj, Cq, tau
    elif len(pfixed) == 5:
        Cq, tau = abs(lsq)
        return Cq, tau
    else:
        print "shit! wrong number fixed params to fitZ"
        return None
def fitCD(f, Z, p0, Cgeom=Cgeom):
    """ find the capacitance and loss of a set of data (in f,Z form, where Z is complex)
        and fit this with Ashoori's model to find fpeak, Clow, Chigh, and the conductivity G
        fpeak, Clow, Chigh, G = fitCD( f, Z, p0, Cgeom=80.0 ) 
        where
        p0 = [fpeak0, Clow0, Chigh0] # initial guesses
    """
    lsq = fmin_plain(sumresidualsCD,
                     p0,
                     args=(Z, f),
                     ftol=1e-8,
                     xtol=1e-8,
                     maxfun=2000,
                     disp=False)
    fpeak, Clow, Chigh = abs(lsq)

    G = 2 * pi * fpeak * Cgeom**2 / sqrt(Clow * Chigh) * (
        Clow / Chigh - 1) * 1e-12  # see Eq. 2 of Ashoori's PRB
    return fpeak, Clow, Chigh, G
def fitZ(f, Zdata, p0, pfixed):
    """ find the capacitance and loss of a set of data (in f,Z form, where Z is complex)
        and fit this with Ashoori's model to find fpeak, Clow, Chigh, and the conductivity G
        Ctj, Cq, tau = fitZ( f, Zdata, p0, pfixed ) 
        where
        p0 = [Ctj0, Cq0, tau0]     # initial guesses
        pfixed = [Cg, Cbp, Rs, Rp] # fixed parameters
    """
    lsq = fmin_plain(sumresidualsZ,
                     p0,
                     args=(pfixed, Zdata, f),
                     ftol=1e-8,
                     xtol=1e-8,
                     maxfun=2000,
                     disp=False)
    if len(pfixed) == 4:
        Ctj, Cq, tau = abs(lsq)
        return Ctj, Cq, tau
    elif len(pfixed) == 5:
        Cq, tau = abs(lsq)
        return Cq, tau
    else:
        print "shit! wrong number fixed params to fitZ"
        return None