import matplotlib.pyplot as plt import numpy as np from math import * def coeffs(X,Y): n = len(X)-1 diff_div=[[0 for _ in xrange(n+1)] for _ in xrange(n+1)] for i in xrange(n+1): diff_div[i][0] = Y[i] for j in xrange(1,n+1): for i in xrange(n-j+1): r = float(diff_div[i+1][j-1]-diff_div[i][j-1])/(X[i+j]-X[i]) diff_div[i][j] = r print diff_div return diff_div[0] print "TP 2.1 ............" def forloop(list): r = 1 for x in list: r *= x return r def construit_pol(X,Y): d = coeffs(X,Y) n = len(X)-1 return lambda x : sum([d[i]*forloop([x - X[j] for j in xrange(i)]) for i in xrange(n+1)]) """ print "tp 2.1....." X = [10,25,60] Y = [2.3, 8, 24.6] p = construit_pol(X,Y) Yp =[p(x) for x in X] print p(15) print p(40) print p(100) x=np.linspace(10,100,100) plt.plot(x,p(x)) plt.ylabel('temps d\'execution') plt.xlabel("taille des donnees") plt.show() """ print "tp 2.2....." X1 = [-1+0.25*i for i in xrange(9)] Y1 = [e**x for x in X1] p1 = construit_pol(X1,Y1) X2 = [cos(pi/2*(float(2*i+1)/9)) for i in xrange(9)] Y2 = [e**x for x in X2] p2 = construit_pol(X2,Y2) e1 = lambda x : abs(p1(x) - e**x) e2 = lambda x : abs(p2(x) - e**x) x=np.linspace(-1,1,1000) plt.plot(x,e1(x)) plt.plot(x,e2(x)) plt.ylabel('exp') plt.xlabel("x") plt.show()