From 498867b9f2ea1da68c51682eee7bb5c627bc13e9 Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Tue, 3 Dec 2024 13:46:30 +0100 Subject: [PATCH 1/3] =?UTF-8?q?do=20doko=C5=84czenia?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rozniczkowanie/rozniczkowanie.py | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 rozniczkowanie/rozniczkowanie.py diff --git a/rozniczkowanie/rozniczkowanie.py b/rozniczkowanie/rozniczkowanie.py new file mode 100644 index 0000000..6a3d1af --- /dev/null +++ b/rozniczkowanie/rozniczkowanie.py @@ -0,0 +1,33 @@ + +# +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] + +# x = [0.6, 0.7, 0.8, 0.9, 1.0] +# y = [-0.51083, -0.35667, -0.22314, -0.10536, 0.0] + +def oblicz(x): + result = [] + for i in range(len(x) - 1): + arr = [round(y[i] - y[i - 1], 5) for i in range(len(y) - 1, 0, -1)] + + + +h = x[1] - x[0] + +d2f = [] +for i in range(len(df) -1, -0, -1): + d2f.append(round(df[i] - df[i-1], 5)) + +d3f = [] +for i in range(len(d2f) -1, -0, -1): + d3f.append(round(d2f[i] - d2f[i-1], 5)) + +print(d3f) + + + + + + +f = 1/h \ No newline at end of file From c1626d40083a8ca5e5debb1b230e5ec9f80c207f Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Tue, 3 Dec 2024 23:00:11 +0100 Subject: [PATCH 2/3] rozniczkowanie zrobione --- rozniczkowanie/rozniczkowanie.py | 78 ++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/rozniczkowanie/rozniczkowanie.py b/rozniczkowanie/rozniczkowanie.py index 6a3d1af..c0e69ac 100644 --- a/rozniczkowanie/rozniczkowanie.py +++ b/rozniczkowanie/rozniczkowanie.py @@ -1,33 +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] -def oblicz(x): +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 = [] - for i in range(len(x) - 1): - arr = [round(y[i] - y[i - 1], 5) for i in range(len(y) - 1, 0, -1)] + 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)}") - -h = x[1] - x[0] - -d2f = [] -for i in range(len(df) -1, -0, -1): - d2f.append(round(df[i] - df[i-1], 5)) - -d3f = [] -for i in range(len(d2f) -1, -0, -1): - d3f.append(round(d2f[i] - d2f[i-1], 5)) - -print(d3f) - - - - - - -f = 1/h \ No newline at end of file +result = oblicz(y) +columns = [x, y] + result +print_table(*columns) +wynik(result) \ No newline at end of file From 034d6883bfe455569dafc061affcb14068a96d98 Mon Sep 17 00:00:00 2001 From: hamx Date: Tue, 3 Dec 2024 23:01:46 +0100 Subject: [PATCH 3/3] little fix --- aproksymacja/main.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/aproksymacja/main.py b/aproksymacja/main.py index ce7d4a9..e5e8573 100644 --- a/aproksymacja/main.py +++ b/aproksymacja/main.py @@ -7,25 +7,20 @@ import random as rand def print_table(*columns, result): headers = ["Lp.", "x0", "x1", "x2", "x0y", "x1y"] - # Combine headers and columns to calculate max width for each column max_widths = [ max(len(str(cell)) for cell in [header] + list(column)) for header, column in zip(headers, columns) ] - # Function to format a row def format_row(row): return " | ".join(f"{str(cell).ljust(width)}" for cell, width in zip(row, max_widths)) - # Print header print(format_row(headers)) print("-" * (sum(max_widths) + 3 * (len(headers) - 1))) # Separator line - # Print rows for row in zip_longest(*columns, fillvalue=" "): print(format_row(row)) - # Print result at the bottom print("-" * (sum(max_widths) + 3 * (len(headers) - 1))) print(", ".join(result), end="\n\n") @@ -71,7 +66,7 @@ a0 = a0(a1) print(f"a0 = {round(a0, 2)}, a1 = {round(a1, 2)}") - +# rysowanie funkcji i punktów plt.xlabel('oś x') plt.ylabel('oś y') plt.title('Aproksymacja - MNK')