From 8a44fd242502dd71cf550d88115c0e9df17e9360 Mon Sep 17 00:00:00 2001 From: Namu Date: Mon, 5 Jan 2026 11:30:56 +0100 Subject: [PATCH] Feat: Ajoute l'exercice 1 de l'interpolation & extrapolation --- ei_blanc.py => p1/ei_blanc.py | 0 ex1.py => p1/ex1.py | 0 ex2.py => p1/ex2.py | 0 ex3.py => p1/ex3.py | 0 ex4.py => p1/ex4.py | 0 ex5.py => p1/ex5.py | 0 tp6.py | 83 +++++++++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+) rename ei_blanc.py => p1/ei_blanc.py (100%) rename ex1.py => p1/ex1.py (100%) rename ex2.py => p1/ex2.py (100%) rename ex3.py => p1/ex3.py (100%) rename ex4.py => p1/ex4.py (100%) rename ex5.py => p1/ex5.py (100%) create mode 100644 tp6.py diff --git a/ei_blanc.py b/p1/ei_blanc.py similarity index 100% rename from ei_blanc.py rename to p1/ei_blanc.py diff --git a/ex1.py b/p1/ex1.py similarity index 100% rename from ex1.py rename to p1/ex1.py diff --git a/ex2.py b/p1/ex2.py similarity index 100% rename from ex2.py rename to p1/ex2.py diff --git a/ex3.py b/p1/ex3.py similarity index 100% rename from ex3.py rename to p1/ex3.py diff --git a/ex4.py b/p1/ex4.py similarity index 100% rename from ex4.py rename to p1/ex4.py diff --git a/ex5.py b/p1/ex5.py similarity index 100% rename from ex5.py rename to p1/ex5.py diff --git a/tp6.py b/tp6.py new file mode 100644 index 0000000..0524a12 --- /dev/null +++ b/tp6.py @@ -0,0 +1,83 @@ +from dataclasses import dataclass +import matplotlib.pyplot as plt +import numpy as np + + +@dataclass +class Point: + x: float + y: float + + +def interpolation_lagrange(points: list[Point], x: float, show_polynome: bool=False) -> float: + """ + + :param points: Les points de la courbe représentant une fonction + :param x: le x en entrée de la fonction où on veut interpoler + :return: L'interpolation + """ + + interpolation: float = 0. + n = len(points) # nombre de 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)) + + # erreur ici, voir le chat + if show_polynome: + print(f'P(x) = (({x} - {points[k].x}) / ({points[i].x} - {points[k].x}))') + + # interpolation with a piece of Lagrange (y_i) + interpolation += term * points[i].y + + 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) + + return interpolation + + +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)] + + inter_x2 = interpolation_lagrange(points, 2, True) + inter_x3 = interpolation_lagrange(points, 3, True) + 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))] + + # extrapolation + plt.plot(x, y) + plt.title('fonction f(x) ex 1') + plt.show() + + +def exercice2() -> None: + pass + +if __name__ == '__main__': + exercice1()