Generowanie x i y

Dodano na potrzeby punktu 7. w instrukcji
This commit is contained in:
2024-12-02 22:26:57 +01:00
parent df9bc002e1
commit 0f8b66f831

View File

@@ -1,10 +1,11 @@
from itertools import zip_longest
import matplotlib.pyplot as plt
import numpy as np
import random as rand
def print_table(*columns, result):
headers = ["x0", "x1", "x2", "x0y", "x1y"]
headers = ["Lp.", "x0", "x1", "x2", "x0y", "x1y"]
# Combine headers and columns to calculate max width for each column
max_widths = [
@@ -29,13 +30,16 @@ def print_table(*columns, result):
print(", ".join(result), end="\n\n")
x = [1, 2, 4, 5]
y = [3, 4, 6, 7]
# x = [1.00, 1.71, 2.42, -3.13, 3.84, 4.55, 5.26, 5.97]
# y = [12.49, 4.76, 2.55, 1.60, 1.11, 0.82, 0.63, 0.50]
# x = [1, 2, 4, 5]
# y = [3, 4, 6, 7]
# x = [1.00, 1.71, 2.42, -3.13, 3.84, 4.55, 5.26, 5.97, 6.32, 8.56]
# y = [12.49, 4.76, 2.55, 1.60, 1.11, 0.82, 0.63, 0.50, 8.35, 5.23]
# x = [1,2,3,4]
# y = [6,5,7,10]
x = [round(rand.uniform(-10, 10), 2) for _ in range(0,100)]
y = [round(rand.uniform(1,2), 2) for _ in range(0, 100)]
x0 = [1 for x in range(len(x))] # wszystkie 1
s0 = sum(x0) # suma jedynek
s1 = sum(x) # suma x-sów
@@ -44,6 +48,7 @@ t0 = sum(y) # suma igreków
t1 = sum([a*b for a, b in zip(x, y)]) # suma x * y
print_table(
[i for i in range(1, len(x)+1)],
x0,
x,
[round(a*a, 2) for a in x],
@@ -72,7 +77,7 @@ plt.ylabel('oś y')
plt.title('Aproksymacja - MNK')
plt.plot(x,y, 'o')
# plt.plot(x, [a0+a1*a for a in x]) # wzór z lekcji zastosowany
extended_x = np.linspace(min(x) - 2, max(x) + 5)
extended_x = np.linspace(min(x) - 50, max(x) + 50)
extended_y = a0 + a1 * extended_x
plt.plot(extended_x, extended_y)