Lagrange_interp_poly_coeffs.py

import numpy as np

def Lagrange_interp_poly_coeffs(degree,f,a,b):

        np1 = degree+1          #  Define the polynomial degree and end points of the interval

        x = np.linspace(a,b,np1)        #  Construct the abscissas

        y = f(x)        #  Evaluate the function at the abscissas

        A = np.empty((np1,np1)) #  Set up the Vandermonde system
        A[:,0] = 1
        for j in range(1,np1):
                A[:,j] = A[:,j-1]*x
        #print(A)

        return np.linalg.solve(A,y)             #  Solve the system


if __name__ == '__main__':

        from runge import runge
        print( np.round(Lagrange_interp_poly_coeffs(10,runge,-5,5),5) )