Feat: Ajoute la méthode des moindres carrés

This commit is contained in:
Namu
2026-01-07 16:27:49 +01:00
parent 51fd37f80b
commit 10f3b4c20d

26
tp6.py
View File

@@ -74,10 +74,6 @@ def P_newton(x_i: list[float], y_i: list[float], x: float) -> float:
return p return p
def interpolation_moindre_carree(points: list[Point], x: float) -> float:
interpolation = 0.
pass
def exercice1() -> None: def exercice1() -> None:
points = [Point(-1,0), Point(0,-1), Point(1,0), Point(3,70)] points = [Point(-1,0), Point(0,-1), Point(1,0), Point(3,70)]
@@ -126,17 +122,33 @@ def exercice2() -> None:
plt.show() plt.show()
def moindre_carre(points: list[Point]) -> np.ndarray[np.float64]:
# Construction de la matrice A et du vecteur y
T = np.array([p.x for p in points])
Y = np.array([p.y for p in points])
A = np.column_stack((T**2, T, np.ones(len(T))))
# Équations normales
X = np.linalg.inv(A.T @ A) @ A.T @ Y
return X
def exercice3() -> None: def exercice3() -> None:
t = [1.5, 3.5, 4.5, 11, 12, 13] t = [1.5, 3.5, 4.5, 11, 12, 13]
h = [80, 160, 184, 184, 149, 116] h = [80, 160, 184, 184, 149, 116]
points = [Point(t[i], h[i]) for i in range(len(t))]
# fonction de calcul de h # fonction de calcul de h
f_h = lambda a, b, c, t: a * (t ** 2) + b * t + c f_h = lambda a, b, c, t: a * (t ** 2) + b * t + c
print(f'{f_h(1,2,3,t[0])}') a, b, c = moindre_carre(points)
print(a,b,c)
if __name__ == '__main__': if __name__ == '__main__':
#exercice1() #exercice1()
exercice2() #exercice2()
#exercice3() exercice3()