-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (69 loc) · 2.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from typing import List
import matplotlib.pyplot as plt
import numpy as np
from numpy import math
import main_boilerplate
from functions import linear, quadratic, logarithmic, exponent, power
solvers = (
linear,
quadratic,
logarithmic,
exponent,
power
)
COLORS_CONST = [
'blue',
'red',
'green',
'black',
'orange'
]
def draw(approximations: List, data):
colors = COLORS_CONST.copy()
data_x, data_y = data
plt.title = 'Графики полученных эмпирических функций'
plt.grid(True, which='both')
plt.xlabel('X')
plt.ylabel('Y')
# plt.axhline(y=0, color='gray', label='y = 0')
# ---
plt.scatter(data_x, data_y, s=20, zorder=10)
for i in range(len(approximations)):
text, _, f = approximations[i]
text = text.split(':')[0][:4].replace('Лине', 'Лин').replace('Лога', 'Лог')
x = np.linspace(min(data_x), max(data_x), 100)
func = np.vectorize(f)(x)
plt.plot(x, func, colors.pop(), label=text, zorder=5)
# ---
plt.legend(loc='upper left', fontsize='medium', bbox_to_anchor=(1, 1))
plt.tight_layout()
plt.savefig('graph.png')
plt.show()
def newline_wrapper(solver, x, y):
print()
return solver(x, y)
def solve(data: List[int]):
x, y = data
approximations = [newline_wrapper(solver, x, y) for solver in solvers]
best = approximations[0]
for approximation in approximations:
function_text, s, _ = approximation
_, best_delta, _ = best
if s < best_delta:
best = approximation
filtered = list(filter(lambda approx: approx[1] != math.inf, approximations))
draw(filtered, data)
print('\n\t-------\n')
print('Наилучшее приближение:')
print(best[0])
print(f'СКО = {best_delta}')
print('\n\t----')
return data
if __name__ == '__main__':
while True:
dataset = main_boilerplate.read_initial_data()
print(dataset, 'dataset')
solve(dataset)
print()
if input('Еще раз? [y/n] ') != 'y':
break