Skip to content

Instantly share code, notes, and snippets.

@dadyarri
Created December 14, 2021 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadyarri/2f90bee721616670d9a273c42b044f61 to your computer and use it in GitHub Desktop.
Save dadyarri/2f90bee721616670d9a273c42b044f61 to your computer and use it in GitHub Desktop.
import math
def func(x): # Подставьте вашу функцию
return ...
def main():
a = 5.0 # Границы
b = 25.0
tolerance = 0.00001 # Точность
iterations = ... # Число итераций
i = 0
while i <= iterations:
c = (a + b) / 2.0
if (func(a) * func(c)) < 0:
b = c
else:
a = c
i += 1
print(f"Итерация: {i - 1}, Границы {(a, b)}. Корень: {c}, Значение функции {func(c)}")
if abs((a - b) / 2.0) < tolerance:
print(f'Корень после {i - 1} итерации - {c}')
else:
print('Нельзя достичь указанной точности в число итераций')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment