#!/usr/bin/env python # -*- coding: utf-8 -*- """ Piège de l'interpolation : fonction discontinue """ __author__ = "Dominique Lefebvre" __copyright__ = "Copyright 2020 - TangenteX.com" __version__ = "1.0" __date__ = "2 février 2020" __email__ = "dominique.lefebvre@tangentex.com" from scipy import linspace, sin from scipy.interpolate import interp1d from matplotlib.pylab import figure, plot, grid, title # fonction à interpoler def f(x): return (sin(x)/(1. - x)) # définition des pivots d'interpolation équirépartis a = -2.0 # borne inf de l'intervalle d'interpolation b = 3.0 # borne sup de l'intervalle d'interpolation # calcul du polynome de Lagrange avec des pivots équirépartis n = 10 # nombre de pivots d'interpolation x = linspace(a,b,n) y = f(x) # calcul de la fonction à interpoler X = linspace(min(x),max(x), 200) Y = f(X) # calcul de l fonction d'interpolation par spline cubique P = interp1d(x, y, kind = 'cubic') YL = P(X) # tracé des fonctions interpolée et d'interpolation fig1 = figure(figsize=(10,8)) grid() title(u"Exemple d'un piège d'interpolation",fontsize=14) # fonction interpolée plot(x,y,'ro') plot(X,Y,'r') # fonction d'interpolation plot(X,YL,'b')