%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import numpy as np
import matplotlib.pyplot as plt
xmin, xmax = 0.0, 1.0
n = 11
x = np.linspace(xmin, xmax, n)
y = 1.0*x
plt.plot(x,y,'-')
y += 0.1*(2*np.random.rand(n) - 1)
plt.plot(x,y,'o')
plt.legend(('True function','Noisy data'));
Let us try to fit a polynomial of degree $n-1$ so that it interpolates all the data
deg = n-1
P = np.polyfit(x,y,deg)
xg = np.linspace(xmin, xmax,100)
yg = np.polyval(P, xg)
plt.plot(x,y,'o',xg,yg,x,x)
plt.legend(('Data','Polynomial','True'))
plt.title('Degree = '+str(deg));
This is not a good approximation. We also see Runge-type effect at the end points. Suppose we knew that our function is indeed linear, we can fit a degree one polynomial to the data.
deg = 1
P = np.polyfit(x,y,deg)
xg = np.linspace(xmin, xmax,100)
yg = np.polyval(P, xg)
plt.plot(x,y,'o',xg,yg,x,x)
plt.legend(('Data','Polynomial','True'))
plt.title('Degree = '+str(deg));
xmin, xmax = -1.0, +1.0
x = np.linspace(xmin, xmax, 11)
y = 0*x
y[3] = 0.1
y[4] = 0.1
y[5] = 0.1
deg = 10
P = np.polyfit(x,y,deg)
xg = np.linspace(xmin, xmax,100)
yg = np.polyval(P, xg)
plt.plot(x,y,'o',xg,yg)
plt.title('Degree = '+str(deg));
deg = 5
P = np.polyfit(x,y,deg)
xg = np.linspace(xmin, xmax,100)
yg = np.polyval(P, xg)
plt.plot(x,y,'o',xg,yg)
plt.title('Degree = '+str(deg));