This repository has been archived on 2025-01-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
MO_pracadomowa/rozniczkowanie/rozniczkowanie.py

65 lines
1.9 KiB
Python

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)