# coding: utf-8 # # Interpolation with Generalized Vandermonde Matrices # In[1]: import numpy as np import numpy.linalg as la import matplotlib.pyplot as pt # In[54]: if True: def f(x): return np.exp(1.5*x) def df(x): return 1.5*np.exp(1.5*x) else: def f(x): return np.sin(4*x) def df(x): return 4*np.cos(4*x) # In[55]: x = np.linspace(0, 1, 1000) pt.plot(x, f(x)) # Fix some parameters: # In[80]: degree = 2 h = 1 nodes = np.linspace(0, h, degree+1) nodes # Build the Vandermonde matrix: # In[81]: V = np.array([ nodes**i for i in range(degree+1) ]).T # In[82]: V # Now find the interpolation coefficients as `coeffs`: # In[83]: coeffs = la.solve(V, f(nodes)) # In[84]: interp = 0*x for i in range(degree+1): interp += coeffs[i] * x**i # In[85]: pt.plot(x, f(x), "--", color="gray", label="$f$") pt.plot(x, interp, color="red", label="Interpolant") pt.plot(nodes, f(nodes), "o") pt.legend(loc="best") # Now evaluate the derivative as `interp_deriv`: # In[86]: interp_deriv = 0*x for i in range(1, degree+1): interp_deriv += coeffs[i] * i * x**(i-1) # In[87]: pt.plot(x, interp_deriv, color="red", label="Interpolant") pt.plot(x, df(x), "--", color="gray", label="Derivative") pt.legend(loc="best") # In[38]: