From b559ade3e9bb6143205abf0ec423bf407600f6a5 Mon Sep 17 00:00:00 2001 From: Namu Date: Sun, 19 Oct 2025 21:03:35 +0200 Subject: [PATCH] Fix: Correct dichotimie --- ex3.py | 5 +---- ex5.py | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/ex3.py b/ex3.py index e3fe251..7f81c10 100644 --- a/ex3.py +++ b/ex3.py @@ -72,11 +72,8 @@ def relaxation(A, b, omega=1.0, epsilon=1e-6, max_iter=100_000): # Pré-calculer (D - ωL)^(-1) une seule fois inv_D_omega_L = np.linalg.inv(D - omega * L) - if omega == 1: - return gauss_seidel(A, b) - for _ in range(max_iter): - x_new = inv_D_omega_L @ ((1 - omega) * D @ x + omega * (U @ x + b)) + x_new = inv_D_omega_L @ (omega * (b - U @ x) - omega * L @ x + (1 - omega) * D @ x) if np.linalg.norm(x_new - x, ord=2) < epsilon: return x_new x = x_new diff --git a/ex5.py b/ex5.py index 8e83bdc..0fba8f7 100644 --- a/ex5.py +++ b/ex5.py @@ -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)