from matplotlib import pyplot as plt
import numpy as np

fig = plt.figure(figsize=(7, 4))

plt.grid(linestyle='--')

plt.xlabel('m', fontsize=15)
plt.ylabel('r^2, mm^2', fontsize=15)

r = [9, 15, 19, 23, 26.5]
r2 = [9, 12, 17, 20.5, 23.5]
r3 = []
err = [3, 3, 2, 2, 2, 2, 1.5, 1.5, 1.5, 1.5]

for i in range(5):
    r3.append(r[i]**2)
    r3.append(r2[i]**2)

for i in range(10):
    print(r3[i])

err2 = []
for i in range(10):
    err2.append(2*np.sqrt(r3[i])*err[i])
print(err2)

m = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]

plt.errorbar(m, r3, xerr=0, yerr=err2, fmt='o', linewidth=1)

def MNK(m, r2):
    #r2 = r**2
    up = 0
    down = 0
    up2 = 0
    down2 = 0
    for i in range(10):
        up+=m[i]*r2[i]
        down+=m[i]**2
        up2+=r2[i]**2
        down2+=m[i]**2
    k=up/down
    print(k, 1/np.sqrt(10)*np.sqrt(up2/down2-k**2))


MNK(m, r3)
x = np.linspace(1, 5, 10)
y = 117.45*x

plt.plot(x, y, linewidth=2, label='k = 117$\pm 5$')


plt.title('МНК приближение')
plt.legend(loc='upper center', fontsize=15)
plt.show()
