diff --git a/bisekcja/bisekcja.py b/bisekcja/bisekcja.py new file mode 100644 index 0000000..1494b21 --- /dev/null +++ b/bisekcja/bisekcja.py @@ -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() \ No newline at end of file