Created
March 2, 2022 01:08
-
-
Save Allespro/c9cb5042055af3a47dbfaf24476d3e2e to your computer and use it in GitHub Desktop.
Lagrange polynomial calc, first three
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Lagrange polynomial | |
''' | |
def L(X, x, y): | |
l1, l2, l3 = L1(X, x, y), L2(X, x, y), L3(X, x, y) | |
return l1, l2, l3, l1 - l3, l2 - l3 | |
def L1(X, x, y): | |
l = list() | |
l.append((X-x[1])/(x[0]-x[1])*y[0]) | |
l.append((X-x[0])/(x[1]-x[0])*y[1]) | |
return sum(map(float,l)) | |
def L2(X, x, y): | |
l = list() | |
l.append(((X-x[1])*(X-x[2]))/((x[0]-x[1])*(x[0]-x[2]))*y[0]) | |
l.append(((X-x[0])*(X-x[2]))/((x[1]-x[0])*(x[1]-x[2]))*y[1]) | |
l.append(((X-x[0])*(X-x[1]))/((x[2]-x[0])*(x[2]-x[1]))*y[2]) | |
return sum(l) | |
def L3(X, x, y): | |
l = list() | |
l.append(((X-x[1])*(X-x[2])*(X-x[3]))/((x[0]-x[1])*(x[0]-x[2])*(x[0]-x[3]))*y[0]) | |
l.append(((X-x[0])*(X-x[2])*(X-x[3]))/((x[1]-x[0])*(x[1]-x[2])*(x[1]-x[3]))*y[1]) | |
l.append(((X-x[0])*(X-x[1])*(X-x[3]))/((x[2]-x[0])*(x[2]-x[1])*(x[2]-x[3]))*y[2]) | |
l.append(((X-x[0])*(X-x[1])*(X-x[2]))/((x[3]-x[0])*(x[3]-x[1])*(x[3]-x[2]))*y[3]) | |
return sum(l) | |
X = 0.08 | |
x = [0.05, 0.1, 0.15, 0.2] | |
y = [-4.171, -4.133, -4.0845, -4.0240] | |
print(list(map(lambda x: '{0:.10f}'.format(x), L(X, x, y)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment