This commit is contained in:
2024-12-05 22:26:41 +01:00
2 changed files with 66 additions and 6 deletions

View File

@@ -7,25 +7,20 @@ import random as rand
def print_table(*columns, result): def print_table(*columns, result):
headers = ["Lp.", "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 = [ max_widths = [
max(len(str(cell)) for cell in [header] + list(column)) max(len(str(cell)) for cell in [header] + list(column))
for header, column in zip(headers, columns) for header, column in zip(headers, columns)
] ]
# Function to format a row
def format_row(row): def format_row(row):
return " | ".join(f"{str(cell).ljust(width)}" for cell, width in zip(row, max_widths)) return " | ".join(f"{str(cell).ljust(width)}" for cell, width in zip(row, max_widths))
# Print header
print(format_row(headers)) print(format_row(headers))
print("-" * (sum(max_widths) + 3 * (len(headers) - 1))) # Separator line print("-" * (sum(max_widths) + 3 * (len(headers) - 1))) # Separator line
# Print rows
for row in zip_longest(*columns, fillvalue=" "): for row in zip_longest(*columns, fillvalue=" "):
print(format_row(row)) print(format_row(row))
# Print result at the bottom
print("-" * (sum(max_widths) + 3 * (len(headers) - 1))) print("-" * (sum(max_widths) + 3 * (len(headers) - 1)))
print(", ".join(result), end="\n\n") print(", ".join(result), end="\n\n")
@@ -71,7 +66,7 @@ a0 = a0(a1)
print(f"a0 = {round(a0, 2)}, a1 = {round(a1, 2)}") print(f"a0 = {round(a0, 2)}, a1 = {round(a1, 2)}")
# rysowanie funkcji i punktów
plt.xlabel('oś x') plt.xlabel('oś x')
plt.ylabel('oś y') plt.ylabel('oś y')
plt.title('Aproksymacja - MNK') plt.title('Aproksymacja - MNK')

View File

@@ -0,0 +1,65 @@
import math
from itertools import zip_longest
x = [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3]
y = [20, 22.222222, 8, 4.081633, 2.469136, 1.652893, 0.83665]
# # z instrukcji, tylko do testu
# x = [0.6, 0.7, 0.8, 0.9, 1.0]
# y = [-0.51083, -0.35667, -0.22314, -0.10536, 0.0]
h = math.ceil(x[1] - x[0])
def print_table(*columns):
def generate_headers(n):
headers = ["x", "f(x)"]
superscripts = ["\u2070", "\u00B9", "\u00B2", "\u00B3", "\u2074", "\u2075", "\u2076", "\u2077", "\u2078", "\u2079"]
for i in range(1, n):
exp = ''.join(superscripts[int(digit)] for digit in str(i))
headers.append(f"{exp}f(x)")
return headers
headers = generate_headers(len(columns))
max_widths = [
max(len(str(cell)) for cell in [header] + list(column))
for header, column in zip(headers, columns)
]
def format_row(row):
return " | ".join(f"{str(cell).ljust(width)}" for cell, width in zip(row, max_widths))
print(format_row(headers))
print("-" * (sum(max_widths) + 3 * (len(headers) - 1))) # Separator line
for row in zip_longest(*columns, fillvalue=" "):
print(format_row(row))
print("-" * (sum(max_widths) + 3 * (len(headers) - 1)))
def oblicz(array):
result = []
current = array
for _ in range(len(array) - 1):
arr = [round(current[j] - current[j - 1], 5) for j in range(len(current) - 1, 0, -1)]
arr.reverse()
result.append(arr)
current = arr
return result
def wynik(array):
temp = []
suma = 0
for x in array:
temp.append(x[-1])
for x in temp:
if temp.index(x) == 0:
suma += x
elif temp.index(x) >= 1:
tik = 1/(temp.index(x) + 1)
suma += x*tik
print(f"f⁽¹⁾(x) = {round(suma*(1/h),5)}")
result = oblicz(y)
columns = [x, y] + result
print_table(*columns)
wynik(result)