In [77]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
modelová funkce: $y = a \sqrt{x}$
In [78]:
def model_fce(x,a):
return a*np.sqrt(x)
In [79]:
data=np.loadtxt('zavislost.dat')
x=data[:,0]
y=data[:,1]
ey=data[:,2]
fit metodou nejmenších čtverců
In [98]:
a_init=1
pars,cov=curve_fit(model_fce,x,y,sigma=ey,absolute_sigma=True,p0=[a_init])
print(f"nejlepší odhad: a = {pars[0]:6.4f} +/- {np.sqrt(cov[0,0]):6.4f}")
nejlepší odhad: a = 0.2164 +/- 0.0051
In [104]:
plt.errorbar(x,y,ey,lw=0,elinewidth=1,capsize=5,marker='o',label='data')
plt.plot(x,model_fce(x,pars[0]),c='green',label='fit')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
In [ ]: