From b04a54cad7d6e89fb8bfeb3db31ded48a41fddd7 Mon Sep 17 00:00:00 2001 From: Namu Date: Wed, 15 Oct 2025 17:31:57 +0200 Subject: [PATCH] Feat: Add dichotomie and Newton-Raphson --- ex5.py | 102 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/ex5.py b/ex5.py index 1417a14..4bb0dfc 100644 --- a/ex5.py +++ b/ex5.py @@ -1,56 +1,80 @@ +import matplotlib.pyplot as plt +import numpy as np -class Point: - def __init__(self, x: float, y: float): - self.x = x - self.y = y +dich_steps = [] +new_raph_steps = [] + +def f(x) -> float: + return np.exp(x) - 2 * np.cos(x) -def interpolation_lagrange(points: list[Point], x: float) -> float: - interpolation: float = 0. - n = len(points) - for i in range(n): - term: float = 1. # 0.0 will always give 0, it's a product ! So lets put 1 - # Lagrange L(x) - for k in range(n): - if k != i: - term *= ((x - points[k].x) / (points[i].x - points[k].x)) - # interpolation with a piece of Lagrange (y_i) - interpolation += term * points[i].y +def dichotomie(x_0, a, b, epsilon, n=0) -> float: + delta = f(a) * f(x_0) + x_1 = (a + b) / 2 - return interpolation + dich_steps.append(x_1) + + if np.abs(b - a) <= epsilon: + return x_1 + + if delta < 0: + return dichotomie(x_1, a, x_0, epsilon, n+1) + elif delta == 0: + return x_1 + else: + return dichotomie(x_1, x_0, b, epsilon, n+1) -def interpolation_newton(points: list[Point], x: float) -> float: - interpolation = 0. # alpha sum - n = len(points) - for i in range(n): - a_i = 1. - for k in range(n-1): - a_i *= (points[k+1].y - points[k].y) / (points[k+1].x - points[k].x) +def f_prime(x) -> float: + return 1 + np.sin(x) - return interpolation + +def newton_raphson(x_0, epsilon) -> float: + x_1 = x_0 - (f(x_0) / f_prime(x_0)) + new_raph_steps.append(x_1) + + if np.abs(x_1 - x_0) < epsilon: + return x_1 + elif np.abs(f(x_0)) < epsilon: + return x_1 + return newton_raphson(x_1, epsilon) def ex1() -> None: - p1 = Point(-1, 0) - p2 = Point(0, -1) - p3 = Point(1, 0) - p4 = Point(3, 70) + x = np.arange(0, 1, 0.001) + f = [np.exp(i) - 2 * np.cos(i) for i in x] + f1 = [np.exp(i) for i in x] + f2 = [2 * np.cos(i) for i in x] - points = [] - points.append(p1) - points.append(p2) - points.append(p3) - points.append(p4) + for i in range(len(x)): + if f1[i] == f2[i]: + print(f1[i]) + + plt.plot(x, f) + plt.plot(x, f1) + plt.plot(x, f2) + plt.grid() + plt.show() + + dichotomie_res = dichotomie(0, 0, 1, 10e-6) + print(dichotomie_res) + + plt.plot(dich_steps) + plt.title('Convergence de la dichotomie') + plt.show() + + newton_raphson_res = newton_raphson(0.1, 10e-6) + print(newton_raphson_res) + + plt.plot(new_raph_steps) + plt.title('Convergence de Newton-Raphson') + plt.show() """ - + La dichotomie à moins d'étape que NR, mais NR à l'air plus rapide, + comme Flash McQueen """ - racine_x_2 = interpolation_lagrange(points, 2.0) - racine_x_3 = interpolation_lagrange(points, 3.0) - print(f'racine X = 2 = {racine_x_2}, racine X = 3 = {racine_x_3}') - if __name__ == '__main__': - ex1() + ex1() \ No newline at end of file