physics-labs-collection/labs/21/processor.py

52 lines
1.4 KiB
Python
Raw Normal View History

2023-04-02 18:15:09 +03:00
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']))}")