Skip to content

Instantly share code, notes, and snippets.

@okurka12
Created May 8, 2023 05:27
Show Gist options
  • Save okurka12/e680d24fbfa0497ff9223e4c257d1cbc to your computer and use it in GitHub Desktop.
Save okurka12/e680d24fbfa0497ff9223e4c257d1cbc to your computer and use it in GitHub Desktop.
overeni konvergence newtonovy numericke metody pro hledani korenu polynomu
from math import sin, cos
DEPTH = 9
def f(x: float) -> float:
"""funkce f"""
return sin(x) - x**2 + 4
def f_d(x: float) -> float:
"""derivace f"""
return cos(x) - 2*x
def recursive(depth: int, xn: float) -> float:
print(f"x_{DEPTH - depth} = {xn:.05f}, f({xn:.05f}) = {f(xn)}")
result = xn - f(xn)/f_d(xn)
if depth == 0:
return result
else:
return recursive(depth - 1, result)
return recursive()
#-------------------------------------------------------------------
recursive(DEPTH, -2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment