Fix: Correct dichotimie
This commit is contained in:
5
ex3.py
5
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
|
||||
|
||||
24
ex5.py
24
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)
|
||||
|
||||
Reference in New Issue
Block a user