Compare commits

...

6 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
4 changed files with 122 additions and 0 deletions

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,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)