Fix: Correct dichotimie
This commit is contained in:
12
ex5.py
12
ex5.py
@@ -4,22 +4,22 @@ import numpy as np
|
||||
new_raph_steps = []
|
||||
dich_steps = []
|
||||
|
||||
|
||||
def f(x) -> float:
|
||||
return np.exp(x) - 2 * np.cos(x)
|
||||
|
||||
|
||||
def dichotomie(a, b, epsilon=1e-6, n=0) -> float:
|
||||
x = (a + b) / 2
|
||||
|
||||
delta = f(a) * f(x)
|
||||
alpha = f(a) * f(x)
|
||||
dich_steps.append(x) # si on veut afficher les étapes
|
||||
|
||||
if np.abs(b - a) <= epsilon:
|
||||
return x
|
||||
|
||||
if delta < 0:
|
||||
if alpha < 0:
|
||||
return dichotomie(a, x, epsilon, n+1)
|
||||
elif delta == 0:
|
||||
elif alpha == 0:
|
||||
return x
|
||||
else:
|
||||
return dichotomie(x, b, epsilon, n+1)
|
||||
@@ -46,13 +46,13 @@ def newton_raphson(x_0, epsilon, max_iter=1_000) -> float | None:
|
||||
if np.abs(fx) < epsilon:
|
||||
return x
|
||||
elif fpx == 0:
|
||||
return None
|
||||
raise RuntimeError("Dérivée nulle : échec de Newton-Raphson.")
|
||||
x = x - (fx / fpx)
|
||||
new_raph_steps.append(x)
|
||||
if np.abs(x - x_prev) < epsilon:
|
||||
return x
|
||||
|
||||
return None
|
||||
raise RuntimeError(f"Newton-Raphson n'a pas convergé en {max_iter} itérations.")
|
||||
|
||||
|
||||
def ex1() -> None:
|
||||
|
||||
Reference in New Issue
Block a user