Compare commits

...

13 Commits

Author SHA1 Message Date
fef143b824 bisekcja 2025-01-13 21:41:40 +01:00
2c23dba6e6 rungego-kutty dodane 2024-12-27 21:38:06 +01:00
9f3aed89f6 Dziwnie to nazwałem 2024-12-13 19:27:26 +01:00
d6e4e7dfb1 gaussa 2024-12-13 19:22:13 +01:00
33fb3e96b8 Merge branch 'main' of https://progit.zikor.pl/hamx/MO_pracadomowa 2024-12-05 22:26:41 +01:00
daf350c8ad Zmiana nazwy pliku 2024-12-05 22:26:34 +01:00
034d6883bf little fix 2024-12-03 23:01:46 +01:00
c1626d4008 rozniczkowanie zrobione 2024-12-03 23:00:11 +01:00
498867b9f2 do dokończenia 2024-12-03 13:46:30 +01:00
0f8b66f831 Generowanie x i y
Dodano na potrzeby punktu 7. w instrukcji
2024-12-02 22:26:57 +01:00
df9bc002e1 Działa aproksymacja 2024-12-02 21:51:59 +01:00
ce9b3f4a40 init aproksymacja 2024-11-16 22:24:43 +01:00
fc1666062c Delete editor.xml 2024-11-13 23:23:34 +01:00
6 changed files with 274 additions and 103 deletions

103
.idea/editor.xml generated
View File

@@ -1,103 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BackendCodeEditorSettings">
<option name="/Default/Housekeeping/GlobalSettingsUpgraded/IsUpgraded/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppClangFormat/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/EditorConfig/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_BINARY_EXPRESSIONS_CHAIN/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_CALLS_CHAIN/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXPRESSION/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_FOR_STMT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTIPLE_DECLARATION/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_TERNARY/@EntryValue" value="ALIGN_ALL" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_CLASS_DEFINITION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue" value="2" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_CODE/@EntryValue" value="2" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_USER_LINEBREAKS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CASE_FROM_SWITCH/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_COMMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INT_ALIGN_EQ/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SIMPLE_BLOCK_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_ARGS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_PARAMS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_SEMICOLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_SEMICOLON/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_UNARY_OPERATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_ARRAY_ACCESS_BRACKETS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_METHOD_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPECIAL_ELSE_IF_TREATMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_BINARY_OPSIGN/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_TERNARY_OPSIGNS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TYPE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/OTHER_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CASE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DECLARATION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DEFINITION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_WHILE_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_ELSE_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_CATCH_ON_NEW_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_INDENTATION/@EntryValue" value="All" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_ARGUMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXTENDS_LIST/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_PARAMETER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_ARGUMENT/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_PARAMETER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_DECLARATIONS/@EntryValue" value="0" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_ACCESS_SPECIFIERS_FROM_CLASS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CLASS_MEMBERS_FROM_ACCESS_SPECIFIERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/LINE_BREAK_AFTER_COLON_IN_MEMBER_INITIALIZER_LISTS/@EntryValue" value="ON_SINGLE_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/MEMBER_INITIALIZER_LIST_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_NAMESPACE_DEFINITIONS_ON_SAME_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_COLON/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_DATA_MEMBER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_DATA_MEMBERS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_METHOD/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_PTR_IN_NESTED_DECLARATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_DATA_MEMBER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_DATA_MEMBERS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_METHOD/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_DATA_MEMBER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_DATA_MEMBERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_METHOD/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_DATA_MEMBER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_DATA_MEMBERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_METHOD/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BETWEEN_CLOSING_ANGLE_BRACKETS_IN_TEMPLATE_ARGS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_DECLARATION_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_BLOCKS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_RPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_RPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_ARGUMENTS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_PARAMETERS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BREAK_TEMPLATE_DECLARATION/@EntryValue" value="LINE_BREAK" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/FREE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INVOCABLE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INITIALIZER_BRACES/@EntryValue" value="END_OF_LINE_NO_SPACE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_STYLE/@EntryValue" value="Space" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_SIZE/@EntryValue" value="4" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CONTINUOUS_LINE_INDENT/@EntryValue" value="Double" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TAB_WIDTH/@EntryValue" value="4" type="int" />
</component>
</project>

View File

@@ -0,0 +1,87 @@
from itertools import zip_longest
import matplotlib.pyplot as plt
import numpy as np
import random as rand
def print_table(*columns, result):
headers = ["Lp.", "x0", "x1", "x2", "x0y", "x1y"]
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)))
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, 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
s2 = sum([a*a for a in x]) # suma x-sów do kwadratu
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],
y,
[round(a*b, 2) for a, b in zip(x, y)],
result = [f"s0={round(s0, 2)}", f"s1={round(s1, 2)}", f"s2={round(s2, 2)}", f"t0={round(t0, 2)}", f"t1={round(t1, 2)}"]
)
def a0(a1):
return (t0 - s1 * a1) / s0
a1 = (t1 * s0 - s1 * t0) / (s2 * s0 - s1**2)
a0 = a0(a1)
# wynik
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')
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) - 50, max(x) + 50)
extended_y = a0 + a1 * extended_x
plt.plot(extended_x, extended_y)
plt.xlim(min(x)-1, max(x)+1)
plt.ylim(min(y)-2, max(y)+1)
plt.show()

45
bisekcja/bisekcja.py Normal file
View File

@@ -0,0 +1,45 @@
from math import fabs
import matplotlib.pyplot as plt
import numpy as np
def f(x):
left_side = 2*np.cos(x + (np.pi/6)) + x**2
right_side = 4 * x - 3
return left_side - right_side
def oblicz(a,b):
x1,x2 = a,b
x = 0
epsi = 0.01
while(fabs(f(x)) > epsi):
x = (x1 + x2) / 2
y = f(x)
y1 = f(x1)
if y*y1 < 0:
x2 = x
elif y*y1 > 0:
x1 = x
return x
a,b = -2,7
print(f"Pierwiastek: {round(oblicz(a,b), 4)}")
x = np.linspace(-15, 15, 1000)
y = f(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y, label=r"$f(x) = 2\cos(x + \frac{\pi}{6}) + x^2 - (4x - 3)$", color="blue")
plt.axhline(0, color="black", linestyle="--", linewidth=0.8) # Add x-axis
plt.axvline(0, color="black", linestyle="--", linewidth=0.8) # Add y-
plt.axvline(a, color="red", linestyle="-", linewidth=2, label="a = {:.2f}".format(a))
plt.axvline(b, color="green", linestyle="-", linewidth=2, label="b = {:.2f}".format(b))
plt.title("Bisekcja")
plt.xlabel("x")
plt.ylabel("f(x)")
plt.legend()
plt.grid()
plt.show()

44
gaussa/gaussa.py Normal file
View File

@@ -0,0 +1,44 @@
def gaussa(a, b):
n = len(b)
for s in range(n - 1):
for i in range(s + 1, n):
factor = a[i][s] / a[s][s]
for j in range(s, n):
a[i][j] -= factor * a[s][j]
b[i] -= factor * b[s]
x = [0] * n
for i in range(n - 1, -1, -1):
x[i] = (b[i] - sum(a[i][j] * x[j] for j in range(i + 1, n))) / a[i][i]
return x
A = [
[4.88, -2.91, 1.34],
[1.34, 5.3, -3.31],
[2.18, -3.49, 6.7]
]
B = [7.86, -2.28, 4.47]
solutions = gaussa(A, B)
print("Macierz A:")
for x in A:
for a in x:
print(round(a, 2), end=" ")
print()
print()
print("Macierz B:")
for b in B:
print(round(b, 2), end=" ")
print("\n")
print("Wynik: ")
i = 1
for x in solutions:
print(f"x{i} = {round(x, 2)}")
i += 1

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)

View File

@@ -0,0 +1,33 @@
import numpy as np
x0 = 1.7
xk = 2.7
h = 0.1
y0 = 5.3
def f(x, y):
return x**2 + np.cos(y / np.pi)
def runge_kutta_4(f, x0, y0, h, xk):
n = int((xk - x0) / h) + 1
x = [x0 + i * h for i in range(n)]
y = [0] * n
y[0] = y0
print("Tabela wyników:")
print(f"{'i':<5}{'x':<10}{'y':<15}{'k1':<10}{'k2':<10}{'k3':<10}{'k4':<10}{'Δy':<10}")
for i in range(1, n):
k1 = h * f(x[i-1], y[i-1])
k2 = h * f(x[i-1] + h/2, y[i-1] + k1/2)
k3 = h * f(x[i-1] + h/2, y[i-1] + k2/2)
k4 = h * f(x[i-1] + h, y[i-1] + k3)
Δy = (k1 + 2*k2 + 2*k3 + k4) / 6
y[i] = y[i-1] + Δy
print(f"{i-1:<5}{x[i-1]:<10.2f}{y[i-1]:<15.5f}{k1:<10.5f}{k2:<10.5f}{k3:<10.5f}{k4:<10.5f}{Δy:<10.5f}")
print(f"{n-1:<5}{x[-1]:<10.2f}{y[-1]:<15.5f}")
return x, y
x, y = runge_kutta_4(f, x0, y0, h, xk)