Feat: Ajoute la méthode des moindres carrés
This commit is contained in:
26
tp6.py
26
tp6.py
@@ -74,10 +74,6 @@ def P_newton(x_i: list[float], y_i: list[float], x: float) -> float:
|
||||
|
||||
return p
|
||||
|
||||
def interpolation_moindre_carree(points: list[Point], x: float) -> float:
|
||||
interpolation = 0.
|
||||
|
||||
pass
|
||||
|
||||
def exercice1() -> None:
|
||||
points = [Point(-1,0), Point(0,-1), Point(1,0), Point(3,70)]
|
||||
@@ -126,17 +122,33 @@ def exercice2() -> None:
|
||||
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:
|
||||
t = [1.5, 3.5, 4.5, 11, 12, 13]
|
||||
h = [80, 160, 184, 184, 149, 116]
|
||||
|
||||
points = [Point(t[i], h[i]) for i in range(len(t))]
|
||||
|
||||
# fonction de calcul de h
|
||||
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__':
|
||||
#exercice1()
|
||||
exercice2()
|
||||
#exercice3()
|
||||
#exercice2()
|
||||
exercice3()
|
||||
|
||||
Reference in New Issue
Block a user