import numpy as np
import matplotlib.pyplot as plt

def interpol(X,Y):
    n = len(X)
    def pol(x):
        res = 0
        for i in range(n):
            Li = 1
            for j in range(n):
                if j != i:
                    Li = Li * (x-X[j])/(X[i]-X[j])
            res = res + Y[i]*Li
        return res
    return pol

# X = [1,2,3,4.5,5,6]
# n = len(X)
# Y = [x**(n) for x in X]
#
# f = interpol(X,Y)
#
# T = np.linspace(0,5)
# plt.plot(T,f(T))
# plt.show()

def approx(f,a,b,N):
    X = [a + k*(b-a)/N for k in range(N+1)]
    Y = [f(x) for x in X]
    pol = interpol(X,Y)
    T = np.linspace(a,b,200)
    plt.plot(T,f(T),'r',label = 'Vraie Fonction')
    plt.plot(T,pol(T),'b',label = 'Interpolation')
    plt.legend()
    plt.show()


def fR(x):
    return 1/(1+25*x**2)

#Tester np.sin
#Tester fR entre -1 et 1