if i < len(x) / 2:
            M2 = x[i + len(x) / 2]
            result.append(p[0] + p[1] * M2 + p[2] * (a_r0[i] ** 2))
        else:
            result.append(p[i - len(x) / 2 + 3])
    return np.asarray(result)


errfunc = lambda p, x, cov: np.dot(cov, (x - fitfunc(p, x)).T)
errfunc2 = lambda p, x, cov: np.dot(cov, (x - fitfunc2(p, x)).T)

nofit = False

if not nofit:
    # 	udres, udchi2, udpvalue = a.xyerrfits(udx2bstraps,r02M2bstraps,errfunc,[1,1],enablecheckcov = 0)
    udres, udchi2, dof, udpvalue, cov = a.xyerrfits_c(r02M2bstraps, udx2bstraps, errfunc, [1, 1], enablecheckcov=0)
# 	udsysres, udsyschi2, dof, udsyspvalue, cov = a.xyerrfits_c(udsys, udxnsys,errfunc,[1,1],cov,0)

matplotlib.rcParams.update({"font.size": 10})

pdfplot = PdfPages(os.path.join(destdir, "udb_var2.pdf"))

for i in range(0, N2):
    plt.errorbar(
        r02M2bstraps[0][i],
        udx2bstraps[0][i],
        xerr=np.std(r02M2bstraps, axis=0)[i],
        yerr=np.std(udx2bstraps, axis=0)[i],
        color=plotcolors[i],
        label=plotlabels[i],
        marker=plotmarkers[i],
def checkcov(cov1):
	cov = np.zeros(cov1.shape)
	for i in range(0,cov1.shape[0]):
		for j in range(0,cov1.shape[1]):
			if i == j or i == j+cov1.shape[0]/2 or i == j-cov1.shape[0]/2:
				cov[i,j] = cov1[i,j]
	return cov

cut = 0.158655254

#############################################################################################
# Fits
#############################################################################################

if fittingvar == 0:
	udres, udchi2, uddof, udpvalue, udcov = a.xyerrfits_c(r02M2bstraps,udNxbstraps,errfunc,[1,1])

	print "ud: "
	print "A = "+str(udres[0][0])+"+-"+str(np.std(udres[:,0]))
	print "B = "+str(udres[0][1])+"+-"+str(np.std(udres[:,1]))

	print "pvalue: "+str(udpvalue[0])
	print "red. chi2: "+str(udchi2[0]/uddof)


	ufres, ufchi2, ufdof, ufpvalue, ufcov = a.xyerrfits_c(r02M2bstraps,ufNxbstraps,errfunc,[1,1])

	print "uf: "
	print "A = "+str(ufres[0][0])+"+-"+str(np.std(ufres[:,0]))
	print "B = "+str(ufres[0][1])+"+-"+str(np.std(ufres[:,1]))