import numpy as np
import matplotlib.pyplot as plt

def f(x, n):
    return x**n

def vertical_matrix(n, x):
    m = len(x)
    matrix = np.zeros((n+1, n+1))
    for i in range(0, n + 1):
        for j in range(0, n + 1):
            for k in range(0, m):
                matrix[i, j] = matrix[i, j] + f(x[k], i) * f(x[k], j)
    print(matrix)
    return matrix

def y_matrix(n, x, y):
    A = []
    m = len(x)
    for i in range(0, n + 1):
        p = 0
        for k in range(0, m):
            p = p + y[k] * f(x[k], i)
        A.append(p)
    print(A)
    return np.array(A)


n = 2
x_values = [-2, -1, 0, 1, 2]
y_values = [0, 1, 2, 1, 0]

x = np.linalg.solve(vertical_matrix(n, x_values), y_matrix(n, x_values, y_values))

print("解为：", x)