In [1]:
%matplotlib inline
In [2]:
import numpy as np
import matplotlib.pyplot as plt

Generate $x$ and $y$

In [45]:
n=100
x = np.arange(1,n+1,1)
x = x/n  
y = np.random.randn(n)*20+30
In [46]:
plt.plot(x,y)
Out[46]:
[<matplotlib.lines.Line2D at 0x126c7bb00>]

Truncated Vandermonde matrix

In [47]:
A = np.vander(x,increasing=True)[:,:5]
In [48]:
plt.imshow(A,aspect="auto")
Out[48]:
<matplotlib.image.AxesImage at 0x126cda2e8>

Compute the pseudo-inverse of $A$

In [49]:
A_p_inv = np.linalg.inv(A.T.dot(A)).dot(A.T)
In [50]:
plt.imshow(A_p_inv,aspect="auto")
Out[50]:
<matplotlib.image.AxesImage at 0x126d2b4a8>
In [51]:
A_p_inv.dot(A)
Out[51]:
array([[ 1.00000000e+00,  1.80232912e-12,  1.43778045e-12,
         1.19003418e-12,  1.04927872e-12],
       [-6.22479845e-12,  1.00000000e+00, -4.58078020e-12,
        -3.41793260e-12, -3.26383365e-12],
       [ 2.75246492e-12,  7.91899879e-12,  1.00000000e+00,
         2.41051623e-12,  3.37418982e-12],
       [ 3.44968498e-12, -6.02362604e-12, -3.16369153e-12,
         1.00000000e+00, -1.79944948e-12],
       [-4.65050221e-12,  6.55475674e-13, -2.19380070e-13,
        -1.54543045e-12,  1.00000000e+00]])
In [52]:
plt.imshow(A.dot(A_p_inv))
Out[52]:
<matplotlib.image.AxesImage at 0x126d886a0>

Compute $c$

In [53]:
c = A_p_inv.dot(y)
In [54]:
plt.plot(c)
Out[54]:
[<matplotlib.lines.Line2D at 0x1270f2b38>]
In [55]:
plt.plot(A.dot(c))
Out[55]:
[<matplotlib.lines.Line2D at 0x127156b70>]

Generate new values, compute $A$ and apply $c$

In [56]:
n=200
x_ = np.arange(1,n+1,1)
x_ = x_/n
x_ = x_[np.logical_and(x_>=x.min(), x_<=x.max())]
In [57]:
A_ = np.vander(x_,increasing=True)
A_ = A_[:,:len(c)]
In [58]:
plt.imshow(A_,aspect="auto")
Out[58]:
<matplotlib.image.AxesImage at 0x1271bc3c8>
In [59]:
plt.plot(x_,A_.dot(c))
plt.plot(x,y,"o")
Out[59]:
[<matplotlib.lines.Line2D at 0x127171358>]
In [ ]:
 
In [ ]: