In [1]:
import numpy as np
import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
import scipy as sp
from scipy.special import erf
import math as mt
In [2]:
x,y,err = np.loadtxt('Example_chi2.txt', delimiter=';')
In [3]:
plt.errorbar(x,y,err,fmt='o',color='r')
plt.errorbar(x,y,err,fmt='o',color='r')
plt.xlim(-1,10)
plt.show()
In [4]:
arr_a = np.linspace(-1,5,61)
arr_b = np.linspace(-1,5,61)
arr_c = np.linspace(-1,5,61)
arr3D = np.zeros((61,61,61))
for indexA in range(0,len(arr_a)):
for indexB in range(0,len(arr_b)):
for indexC in range(0,len(arr_c)):
ymod = arr_a[indexA] * np.power(x,2) + arr_b[indexB] * x + arr_c[indexC]
arr3D[indexA,indexB,indexC] = np.sum(np.power(ymod - y,2)/np.power(err,2))
In [5]:
tmin = np.unravel_index(arr3D.argmin(),arr3D.shape)
plt.pcolormesh(arr_a, arr_b, np.transpose(np.log10(arr3D[:,:,tmin[2]])),cmap='coolwarm')
plt.colorbar()
Out[5]:
In [6]:
plt.pcolormesh(arr_a, arr_b, np.transpose(np.log10(arr3D[:,tmin[1],:])),cmap='coolwarm')
plt.colorbar()
Out[6]:
In [7]:
plt.pcolormesh(arr_a, arr_b, np.transpose(np.log10(arr3D[tmin[0],:,:])),cmap='coolwarm')
plt.colorbar()
Out[7]:
In [101]:
arr_a[tmin[0]],arr_b[tmin[1]],arr_c[tmin[2]]
Out[101]:
In [102]:
plt.errorbar(x,y,err,fmt='o',color='r')
plt.plot(x,arr_a[tmin[0]] * np.power(x,2) + arr_b[tmin[1]] * x + arr_c[tmin[2]],color='b')
plt.xlim(-1,10)
plt.show()
In [103]:
a, b, c = np.polyfit(x, y, 2, w=1/err)
plt.errorbar(x,y,err,fmt='o',color='r')
plt.xlim(-1,10)
plt.plot(x, a*np.power(x,2) + b*x + c, '-')
Out[103]:
In [59]:
a = -0.3 b=2.6 c=4
In [9]:
levels=[8.18,14.35,21.8]
plt.contour(arr_a, arr_b, np.transpose(arr3D[tmin[0],:,:]),levels)
plt.plot(arr_b[tmin[1]], arr_c[tmin[2]], 'o')
plt.ylabel(r'$b$', fontsize=20)
plt.xlabel(r'$c$',fontsize=20)
plt.show()
# plt.contour(arr_a, arr_b, np.transpose(arr3D[:,tmin[1],:]),levels)
# plt.plot(arr_b[tmin[0]], arr_c[tmin[2]], 'o')
# plt.show()
# plt.contour(arr_a, arr_b, np.transpose(arr3D[:,:,tmin[2]]),levels)
# plt.plot(arr_b[tmin[0]], arr_c[tmin[1]], 'o')
# plt.show()
In [71]:
arr_a = np.linspace(-1,5,61)
arr_b = np.linspace(-1,5,61)
arr2D = np.zeros((61,61,61))
for indexA in range(0,len(arr_a)):
for indexB in range(0,len(arr_b)):
ymod = arr_a[indexA] * x + arr_b[indexB]
arr2D[indexA,indexB] = np.sum(np.power(ymod - y,2)/np.power(err,2))
tmin1 = np.unravel_index(arr2D.argmin(),arr2D.shape)
plt.pcolormesh(arr_a, arr_b, np.transpose(np.log10(arr2D[:,:,tmin1[2]])),cmap='coolwarm')
plt.colorbar()
plt.show()
In [72]:
arr_a[tmin1[0]],arr_b[tmin1[1]]
Out[72]:
In [75]:
plt.errorbar(x,y,err,fmt='o',color='r')
plt.plot(x,arr_a[tmin1[0]] * x + arr_b[tmin1[1]],color='b')
plt.xlim(-1,10)
plt.show()
In [82]:
levels=[8.18,14.35,21.8]
plt.contour(arr_a, arr_b, np.transpose(arr3D[tmin1[0],:]),levels)
plt.show()
plt.contour(arr_a, arr_b, np.transpose(arr3D[:,tmin1[1]]),levels)
plt.show()