#!/usr/bin/env python # -*- coding: utf-8 -*- """ Fonction interp1d : différentes méthodes d'interpolation """ __author__ = "Dominique Lefebvre" __copyright__ = "Copyright 2020 - TangenteX.com" __version__ = "1.0" __date__ = "3 février 2020" __email__ = "dominique.lefebvre@tangentex.com" from scipy import linspace, array from scipy.interpolate import interp1d from matplotlib.pylab import figure, plot, grid, title, axis # définition des pivots d'interpolation x = array([2.0,3.0,4.0,5.0,6.0,7.0,8.0]) y = array([1.0,4.0,6.0,7.0,2.0,4.5,3.5]) X = linspace(min(x),max(x), 200) # calcul du polynôme d'interpolation linéaire et spline d'ordre 0,1,2 et 3 PL = interp1d(x, y, kind = 'linear') YL = PL(X) P0 = interp1d(x, y, kind = 'zero') Y0 = P0(X) P1 = interp1d(x, y, kind = 'slinear') Y1 = P1(X) P2 = interp1d(x, y, kind = 'quadratic') Y2 = P2(X) P3 = interp1d(x, y, kind = 'cubic') Y3 = P3(X) # tracé des courbes d'interpolation fig1 = figure(figsize=(10,8)) axis([2.0,8.0,1.0,9.0]) grid() title(u'Interpolation par interp1d',fontsize=14) # fonction interpolée plot(x,y,'ro') plot(X,YL,'blue') #plot(X,Y0,'r') plot(X,Y1,'g') #plot(X,Y2,'y') #plot(X,Y3,'black')