Skip to content

Instantly share code, notes, and snippets.

@undrcrxwn
Created November 1, 2023 15:31
Show Gist options
  • Save undrcrxwn/caed49abae35cc8087e957ef9490b93f to your computer and use it in GitHub Desktop.
Save undrcrxwn/caed49abae35cc8087e957ef9490b93f to your computer and use it in GitHub Desktop.
import math
def f(x):
return math.exp(-2 * x) - 3 * x ** 3
a = 0.4976
b = 0.49761
eps = 1e-12
k = 0
print("a = ", a)
print("b = ", b)
print("eps = ", eps)
k = 1
x = (a+b)/2;
while (abs(b-a) >= eps) and (k<=1000):
x = (a+b)/2;
if f(x) == 0:
print("Найден точный корень. X = ", x)
print("Количество итераций k = ",k)
print("Проверка f(x) = ", f(x))
break
elif f(a)*f(x) > 0:
a = x
else:
b = x
k += 1
print("x = ", x)
print("Количество итераций k = ",k)
print("Проверка f(x) = ", f(x))
if k>=1000:
print("Заданная точность не достигнута, превышен предел итераций")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment