Compare commits

...

2 Commits

Author SHA1 Message Date
dymik739 34da704168 add script for lab 21 2023-04-02 18:15:09 +03:00
dymik739 77a51eac70 improve lab 51 script to provide more useful data 2023-04-02 18:10:49 +03:00
5 changed files with 2650 additions and 7 deletions

15
labs/21/README.md Normal file
View File

@ -0,0 +1,15 @@
# Як користуватися
Необхідно клонувати цей репозиторій, наприклад:
```
git clone http://10.1.1.1:3000/dymik739/physics-labs-collection.git
```
Далі потрібно перейти у папку зі скриптом
```
cd physics-labs-collection/labs/21/
```
та запустити скрипт, вказавши файл зі вхідними даними, наприклад:
```
python3 processor.py sample_data.txt
```

2534
labs/21/prettytable.py Normal file

File diff suppressed because it is too large Load Diff

51
labs/21/processor.py Normal file
View File

@ -0,0 +1,51 @@
import sys
import math
def avg(a):
return sum(a) / len(a)
resistances = []
settings = {'r': 4, "R2": 0.0, "R3": 0.0}
for raw_i in open(sys.argv[1], "r"):
i = raw_i.rstrip("\r\n")
if len(i) > 0:
if i[0] == "#":
cmd = i[1:].split()
if cmd[0] in settings:
settings[cmd[0]] = cmd[1]
else:
resistances.append([i.split()[0]] + [float(j.replace(',', '.')) for j in i.split()[1:]])
table_data = []
for i in resistances:
Rxi = [round(r*float(settings["R2"])/float(settings["R3"]), int(settings['r'])) for r in i[1:]]
R_avg = round(avg(Rxi), int(settings['r']))
table_data.append([i[0]] + Rxi + [R_avg])
from prettytable import PrettyTable
pt = PrettyTable()
pt.add_column("", list(range(1, len(table_data[0][1:-1]) + 1)) + ["❬Rx❭"])
for i in table_data:
pt.add_column(i[0], i[1:])
print(pt)
#print(table_data)
print("Обчислюємо значення S для кожного стовпця:")
for j in table_data:
s = math.sqrt( sum([ (i - j[-1])**2 for i in j[1:-1] ]) / (len(j[1:-1])*(len(j[1:-1])-1)) )
print(f"S ❬{j[0]}❭ = {round(s, int(settings['r']))}")
print("\nОбчислюємо значення σ для кожного стовпця:")
for j in table_data:
sigma = math.sqrt(avg([i**2 for i in j[1:-1]]) - avg(j[1:-1])**2)
print(f"σ{j[0]}❭ = {round(sigma, int(settings['r']))}")

9
labs/21/sample_data.txt Normal file
View File

@ -0,0 +1,9 @@
# R2 35000
# R3 4600
# r 15
R1x 3403,1 3400,0 3402,9 3403,1 3401,1
R2x 34,1 33,4 33,3 33,5 34,2
R3x 1771,7 1772,2 1772,2 1771,8 1771,5
R4x 740,0 741,0 740,7 741,2 740,8
R5x 2530,0 2533,0 2532,0 2532,9 2531,0

View File

@ -4,7 +4,7 @@ import os
# defining some constants
r = 3 # round to n digits
r = 32 # round to n digits
column_width = 11 # set column width of final table
# checking if the file is provided correctly
@ -144,10 +144,44 @@ sigma_beta2 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_da
sigma_beta3 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[2]))) + avg(list(zip(*required_data["2"]['stats']))[2])**2), r)
sigma_beta4 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[5]))) + avg(list(zip(*required_data["2"]['stats']))[5])**2), r)
sigma_t1 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["1"]['stats']))[1]))) + avg(list(zip(*required_data["1"]['stats']))[1])**2), r)
sigma_t2 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["1"]['stats']))[3]))) + avg(list(zip(*required_data["1"]['stats']))[3])**2), r)
sigma_t3 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[1]))) + avg(list(zip(*required_data["2"]['stats']))[1])**2), r)
sigma_t4 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[3]))) + avg(list(zip(*required_data["2"]['stats']))[3])**2), r)
sigma_M1 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["1"]['stats']))[0]))) + avg(list(zip(*required_data["1"]['stats']))[0])**2), r)
sigma_M2 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["1"]['stats']))[3]))) + avg(list(zip(*required_data["1"]['stats']))[3])**2), r)
sigma_M3 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[0]))) + avg(list(zip(*required_data["2"]['stats']))[0])**2), r)
sigma_M4 = round(math.sqrt(avg(list(map(lambda x: x**2, list(zip(*required_data["2"]['stats']))[3]))) + avg(list(zip(*required_data["2"]['stats']))[3])**2), r)
print("σβ (1-4) (DO NOT USE):", sigma_beta1/avg(list(zip(*required_data["1"]['stats']))[2]), sigma_beta2/avg(list(zip(*required_data["1"]['stats']))[5]), sigma_beta3/avg(list(zip(*required_data["2"]['stats']))[2]), sigma_beta4/avg(list(zip(*required_data["2"]['stats']))[5]))
print("σt (1-4) (DO NOT USE):", sigma_t1/avg(list(zip(*required_data["1"]['stats']))[1]), sigma_t2/avg(list(zip(*required_data["1"]['stats']))[3]), sigma_t3/avg(list(zip(*required_data["2"]['stats']))[1]), sigma_t4/avg(list(zip(*required_data["2"]['stats']))[3]))
#print("σβ (1-4) (DO NOT USE):", sigma_beta1/avg(list(zip(*required_data["1"]['stats']))[2]), sigma_beta2/avg(list(zip(*required_data["1"]['stats']))[5]), sigma_beta3/avg(list(zip(*required_data["2"]['stats']))[2]), sigma_beta4/avg(list(zip(*required_data["2"]['stats']))[5]))
#print("σM (1-4) (DO NOT USE):", sigma_t1/avg(list(zip(*required_data["1"]['stats']))[0]), sigma_t2/avg(list(zip(*required_data["1"]['stats']))[3]), sigma_t3/avg(list(zip(*required_data["2"]['stats']))[0]), sigma_t4/avg(list(zip(*required_data["2"]['stats']))[3]))
print("σβ:", avg([math.sqrt(sigma_beta1), math.sqrt(sigma_beta2), math.sqrt(sigma_beta3), math.sqrt(sigma_beta4)]))
print("σM:", avg([math.sqrt(sigma_M1), math.sqrt(sigma_M2), math.sqrt(sigma_M3), math.sqrt(sigma_M4)]))
print(required_data)
'''
for t in required_data:
I_values_r1 = [(i[0] / i[1]) for i in zip(list(zip(*required_data[t]['stats']))[0], list(zip(*required_data[t]['stats']))[2])]
Mt1 = required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][0] - (min(I_values_r1) * required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][2])
print(f"Таблиця {t}, стовпець 1: Imin = {required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][0]} / {required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][2]} = {round(min(I_values_r1), r)}; Mт = {required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][0]} - {min(I_values_r1)}*{required_data[t]['stats'][I_values_r1.index(min(I_values_r1))][2]} = {round(Mt1, r)} (за мінімальний узято рядок {I_values_r1.index(min(I_values_r1))})")
I_values_r2 = [(i[0] / i[1]) for i in zip(list(zip(*required_data[t]['stats']))[3], list(zip(*required_data[t]['stats']))[5])]
Mt2 = required_data[t]['stats'][I_values_r2.index(min(I_values_r2))][3] - (min(I_values_r2) * required_data[t]['stats'][I_values_r2.index(min(I_values_r2))][5])
print(f"Таблиця {t}, стовпець 2: Imin = {round(min(I_values_r2), r)}; Mт = {round(Mt2, r)}")
#i_min_2 = min([(i[0] / i[1]) for i in zip(list(zip(*required_data[t]['stats']))[3], list(zip(*required_data[t]['stats']))[5])])
#print(f"Imin for {t}-2: {round(i_min_2, r)}")
'''
for t in required_data:
I_values_r1 = []
M_and_beta = list(zip(list(zip(*required_data[t]['stats']))[0], list(zip(*required_data[t]['stats']))[2]))
print(M_and_beta)
for i in range(5):
print((M_and_beta[i+1][0] - M_and_beta[i][0]) / (M_and_beta[i+1][1] - M_and_beta[i][1]))
I_values_r1.append((M_and_beta[i+1][0] - M_and_beta[i][0]) / (M_and_beta[i+1][1] - M_and_beta[i][1]))
print(f"Таблиця {t}, стовпець 1: Imin = {min(I_values_r1)}; Mт = {M_and_beta[I_values_r1.index(min(I_values_r1))][0] - min(I_values_r1)*M_and_beta[I_values_r1.index(min(I_values_r1))][1]}")
I_values_r2 = []
M_and_beta = list(zip(list(zip(*required_data[t]['stats']))[3], list(zip(*required_data[t]['stats']))[5]))
for i in range(5):
print((M_and_beta[i+1][0] - M_and_beta[i][0]) / (M_and_beta[i+1][1] - M_and_beta[i][1]))
I_values_r2.append((M_and_beta[i+1][0] - M_and_beta[i][0]) / (M_and_beta[i+1][1] - M_and_beta[i][1]))
print(f"Таблиця {t}, стовпець 2: Imin = {min(I_values_r2)}; Mт = {M_and_beta[I_values_r2.index(min(I_values_r2))][0] - min(I_values_r2)*M_and_beta[I_values_r2.index(min(I_values_r2))][1]}")