bisekcja
This commit is contained in:
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()
|
||||||
Reference in New Issue
Block a user