Fix: Correct dichotimie

This commit is contained in:
Namu
2025-10-19 21:03:35 +02:00
parent 128e07d420
commit b559ade3e9
2 changed files with 14 additions and 15 deletions

24
ex5.py
View File

@@ -1,29 +1,31 @@
import matplotlib.pyplot as plt
import numpy as np
dich_steps = []
new_raph_steps = []
dich_steps = []
def f(x) -> float:
return np.exp(x) - 2 * np.cos(x)
def dichotomie(x_0, a, b, epsilon, n=0) -> float:
delta = f(a) * f(x_0)
x_1 = (a + b) / 2
def dichotomie(a, b, epsilon=1e-6, n=0, steps=None) -> float:
if steps is None:
steps = []
dich_steps.append(x_1)
x = (a + b) / 2
delta = f(a) * f(x)
dich_steps.append(x) # si on veut afficher les étapes
if np.abs(b - a) <= epsilon:
return x_1
return x
if delta < 0:
return dichotomie(x_1, a, x_0, epsilon, n+1)
return dichotomie(a, x, epsilon, n+1)
elif delta == 0:
return x_1
return x
else:
return dichotomie(x_1, x_0, b, epsilon, n+1)
return dichotomie(x, b, epsilon, n+1)
def f_prime(x) -> float:
@@ -72,7 +74,7 @@ def ex1() -> None:
plt.grid()
plt.show()
dichotomie_res = dichotomie(0, 0, 1, 10e-6)
dichotomie_res = dichotomie(0, 1, 10e-6)
print(dichotomie_res)
plt.plot(dich_steps)