added lagrange polynom calculation

This commit is contained in:
Sven Vogel 2023-11-21 11:35:58 +01:00
parent 8adb0fcbe1
commit 61ad1fefef
2 changed files with 26 additions and 0 deletions

24
lagrange/__init__.py Normal file
View File

@ -0,0 +1,24 @@
def lagrange_polynom(points, j):
nom = ""
dem = 1
xj = points[j][0]
for u in range(len(points)):
if u == j:
continue
nom += f"(x - {points[u][0]})"
dem *= xj - points[u][0]
return (nom, dem)
def interpolate_lagrange(points):
n = len(points)
poly = ""
for i in range(n):
(lagrange, dem) = lagrange_polynom(points, i)
poly += f" + {points[i][1]*dem}*{lagrange}"
return poly
def test():
points = [[1,2], [3,5], [4,-1]]
print(f"polynom via lagrange: {interpolate_lagrange(points)}")

View File

@ -15,6 +15,7 @@
# Copyright (c) Sven Vogel # Copyright (c) Sven Vogel
import fixpoint_approximation import fixpoint_approximation
import lagrange
import linear_approximation import linear_approximation
import matrix import matrix
import newton_polynom import newton_polynom
@ -30,3 +31,4 @@ if __name__ == '__main__':
matrix.test() matrix.test()
taylor_sine_approx.test() taylor_sine_approx.test()
integral_approximation.test() integral_approximation.test()
lagrange.test()