Compare commits
6 Commits
034d6883bf
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| fef143b824 | |||
| 2c23dba6e6 | |||
| 9f3aed89f6 | |||
| d6e4e7dfb1 | |||
| 33fb3e96b8 | |||
| daf350c8ad |
45
bisekcja/bisekcja.py
Normal file
45
bisekcja/bisekcja.py
Normal 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
44
gaussa/gaussa.py
Normal 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
|
||||
33
rungego-kutty/rungego-kutty.py
Normal file
33
rungego-kutty/rungego-kutty.py
Normal 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)
|
||||
Reference in New Issue
Block a user