Feat: Ajoute l'exercice 2 avec les polynômes de Newton
This commit is contained in:
81
tp6.py
81
tp6.py
@@ -36,30 +36,49 @@ def interpolation_lagrange(points: list[Point], x: float, show_polynome: bool=Fa
|
|||||||
return interpolation
|
return interpolation
|
||||||
|
|
||||||
|
|
||||||
def interpolation_newton(points: list[Point], x: float) -> float:
|
def calc_coef(x: list[float],y: list[float]) -> float:
|
||||||
interpolation = 0. # alpha sum
|
"""
|
||||||
n = len(points)
|
Renvois f[x0,x1,...,xk] (diff divisé de Newton)
|
||||||
for i in range(n):
|
avec la relation récursive :
|
||||||
a_i = 1.
|
f[x0...xk] = (f[x1...xk] - f[x0..x{k-1}] / (xk - x0)
|
||||||
for k in range(n-1):
|
(calcul alpha)
|
||||||
a_i *= (points[k+1].y - points[k].y) / (points[k+1].x - points[k].x)
|
: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:
|
def interpolation_moindre_carree(points: list[Point], x: float) -> float:
|
||||||
interpolation = 0.
|
interpolation = 0.
|
||||||
|
|
||||||
pass
|
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:
|
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)]
|
||||||
|
|
||||||
@@ -68,16 +87,40 @@ def exercice1() -> None:
|
|||||||
print(f"X2={inter_x2}, X3={inter_x3}")
|
print(f"X2={inter_x2}, X3={inter_x3}")
|
||||||
|
|
||||||
x = np.linspace(0, 4, 300)
|
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
|
# extrapolation
|
||||||
plt.plot(x, y)
|
plt.plot(x, y_lagrange)
|
||||||
plt.title('fonction f(x) ex 1')
|
plt.title('fonction f(x) ex 1')
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
def exercice2() -> None:
|
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__':
|
if __name__ == '__main__':
|
||||||
exercice1()
|
exercice1()
|
||||||
|
#exercice2()
|
||||||
|
|||||||
Reference in New Issue
Block a user