# -*- coding: utf-8 -*- """ Created on Fri Dec 12 17:51:56 2014 @author: grivet Listing 4.2 : Construction du polynôme de Newton """ import numpy as np import matplotlib.pyplot as plt npiv = 4 npt = 200 xpiv = np.array([-0.5,0.5,1.5,2.0]) fpiv = np.exp(xpiv) x = np.linspace(-2,3,200) t = np.zeros(4); a = np.zeros(4) neff = int(input("ordre d'interpolation (<= 3): ")) #calcul des différences divisées for i in range(0,neff+1): t[i] = fpiv[i] for j in range(i-1,-1,-1): t[j] = (t[j+1]-t[j])/(xpiv[i]-xpiv[j]) a[i] = t[0] #calcul du polynôme pol = a[neff] for i in range(neff-1,-1,-1): pol = pol*(x-xpiv[i])+a[i] plt.plot (xpiv,fpiv,'ok') plt.plot(x,np.exp(x),'--k') plt.plot(x,pol,'-r') plt.title("interpolation de Newton pour la fonction $e^x$") plt.xlabel("$x$")