#keep
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
%matplotlib inline
def f(x):
return np.sin(4*x)
def df(x):
return 4*np.cos(4*x)
x = np.linspace(0, 1, 1000)
pt.plot(x, f(x))
degree = 2
nodes = np.linspace(0, 1, degree+1)
print(nodes)
V = np.array([
nodes**i
for i in range(degree+1)
]).T
print(V)
coeffs
:¶coeffs = la.solve(V, f(nodes))
interp = 0*x
for i in range(degree+1):
interp += coeffs[i] * x**i
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")