Feat: Add tp5
This commit is contained in:
31
ex5.py
Normal file
31
ex5.py
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
class Point:
|
||||
def __init__(self, x: float, y: float):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def interpolation_lagrange(points: list[Point], x: float) -> float:
|
||||
interpolation: float = 0.
|
||||
n = len(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))
|
||||
# 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
|
||||
Reference in New Issue
Block a user