Skip to content

Instantly share code, notes, and snippets.

@undrcrxwn
Created October 26, 2023 17:15
Show Gist options
  • Save undrcrxwn/0b4ee07e31ba4dcd6a6eaeca29c8f600 to your computer and use it in GitHub Desktop.
Save undrcrxwn/0b4ee07e31ba4dcd6a6eaeca29c8f600 to your computer and use it in GitHub Desktop.
import math
def fix(x):
return math.tan(x) + x - 1
def fix_derivative(x):
return 1 / (math.cos(x) ** 2) + 1
def newton_method(f, df, initial_guess, eps, max_iterations=100):
x = initial_guess
iterations = 0
while iterations < max_iterations:
x_new = x - f(x) / df(x)
if abs(x_new - x) < eps:
return x_new, iterations
x = x_new
iterations += 1
return None, iterations
k = 1
x = 0.47972
eps = 1e-12
root, iterations = newton_method(fix, fix_derivative, x, eps)
if root is not None:
print(f"Приближенное значение корня: {root}")
print(f"Количество итераций: {iterations}")
else:
print("Метод Ньютона не сошелся к корню.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment