From 6df3d06ff047f6af9744776a80adff52af0772a5 Mon Sep 17 00:00:00 2001 From: Namu Date: Wed, 7 Jan 2026 14:21:47 +0100 Subject: [PATCH] =?UTF-8?q?Feat:=20Ajoute=20l'exercice=202=20avec=20les=20?= =?UTF-8?q?polyn=C3=B4mes=20de=20Newton?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tp6.py | 81 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 19 deletions(-) diff --git a/tp6.py b/tp6.py index 0524a12..d126dd4 100644 --- a/tp6.py +++ b/tp6.py @@ -36,30 +36,49 @@ def interpolation_lagrange(points: list[Point], x: float, show_polynome: bool=Fa return interpolation -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 calc_coef(x: list[float],y: list[float]) -> float: + """ + Renvois f[x0,x1,...,xk] (diff divisé de Newton) + avec la relation récursive : + f[x0...xk] = (f[x1...xk] - f[x0..x{k-1}] / (xk - x0) + (calcul alpha) + :param x: + :param y: + :return: + """ + if len(x) == 1: + return y[0] + elif len(x) == 2: + return (y[1]-y[0]) / (x[1] - x[0]) + return (calc_coef(x[1:], y[1:]) - calc_coef(x[:-1], y[:-1])) / (x[-1] - x[0]) - return interpolation +def P_newton(x_i: list[float], y_i: list[float], x: float) -> float: + """ + Calcul le polynôme de Newton + + P_n(x) = y0 + sum_{k=1..n} a_k * prod_{j=0..k-1}(x - x_j), + avec a_k = f[x0,...,xk] calculé via calc_coef() + :param x_i: ensemble des x + :param y_i: ensemble des y + :param x: là ou on veut interpolé + :return: + """ + p = y_i[0] + e = 1. + + for k in range(1, len(x_i)): + e *= (x - x_i[k-1]) + a_k = calc_coef(x_i[:k+1], y_i[:k+1]) + p += a_k * e + + return p def interpolation_moindre_carree(points: list[Point], x: float) -> float: interpolation = 0. pass - -def test() -> None: - points = [Point(0, 1), Point(1, 3), Point(2, 2)] - x = 1.5 - result = interpolation_lagrange(points, x) - print(f"P({x}) = {result}") # Doit afficher 2.875 - - def exercice1() -> None: points = [Point(-1,0), Point(0,-1), Point(1,0), Point(3,70)] @@ -68,16 +87,40 @@ def exercice1() -> None: print(f"X2={inter_x2}, X3={inter_x3}") x = np.linspace(0, 4, 300) - y = [interpolation_lagrange(points, x[i]) for i in range(len(x))] + y_lagrange = [interpolation_lagrange(points, x[i]) for i in range(len(x))] + + y_newton = [] + + for i in range(4): + y_newton.append([P_newton([point.x for point in points], [point.y for point in points], i)]) + + print(y_lagrange) + print(y_newton) # extrapolation - plt.plot(x, y) + plt.plot(x, y_lagrange) plt.title('fonction f(x) ex 1') plt.show() def exercice2() -> None: - pass + x_i = [0, np.pi / 4, np.pi / 2, (3 * np.pi) / 4, np.pi] + + x = [i for i in range(len(x_i))] + + f = lambda x : np.sin(x) + + results = [] + + for i in x: + res = P_newton(x_i, [f(x_i[i]) for i in range(len(x_i))], i) + print(res) + results.append(res) + + plt.plot(results) + plt.show() + if __name__ == '__main__': exercice1() + #exercice2()