#!/usr/bin/env python # coding: utf-8 # # Gibbs oscillations # Let us try to approximate a discontinuous function using Chebyshev interpolation # $$ # f(x) = \begin{cases} # 1 & x < 0 \\ # 0 & x > 0 # \end{cases} # $$ # In[40]: get_ipython().run_line_magic('matplotlib', 'inline') get_ipython().run_line_magic('config', "InlineBackend.figure_format = 'svg'") import numpy as np import matplotlib.pyplot as plt xmin, xmax = -1.0, +1.0 fun = lambda x: (x < 0)*1.0 # We will interpolate this function at Chebyshev points. # In[43]: # Plot function on a finer grid xx = np.linspace(xmin,xmax,100); ye = fun(xx); plt.figure(figsize=(10,8)) plt.plot(xx,ye,'--') for N in range(10,15): theta = np.linspace(0,pi,N+1) x = np.cos(theta) y = fun(x); P = np.polyfit(x,y,N); yy = np.polyval(P,xx); plt.plot(xx,yy) # The interpolants are oscillatory and this phenomenon is known as Gibbs oscillations.