Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
812 B
2
Indexable
Never
import numpy as np

path = ''

def interpolacja(x, y):
    n = len(x)
    A = np.zeros((n, n))
    for i in range(n):
        A[i, 0] = 1
        for j in range(1, n):
            A[i, j] = A[i, j-1] * x[i]
    c = np.linalg.solve(A, y)
    p = np.poly1d(c[::-1])
    
    #print
    p_str = f"{c[-1]:.2f} ∙ x^{n-1}"
    for i in range(n-2, -1, -1):
        p_str += f" + {c[i]:.2f} ∙ x^{i}"
    return p_str

x = []
y = []

path = input("Enter path: ")
data = ""
with open(path) as file:
    data = file.read().replace(',', '.')

with open(path, 'w') as out_file:
    out_file.write(data)

with open(path) as f:
    for line in f:
        n = line.split()[0]
        x.append(float(n))
        z = line.split()[1] 
        y.append(float(z))


print(interpolacja(x,y))