import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
x = np.linspace(0, 1, 200)
Now plot the monomial basis on the interval [0,1] up to $x^9$.
n = 10
for i in range(n):
pt.plot(x, x**i)
pt.vlines(np.linspace(0, 1, n), 0, 1, alpha=0.5, linestyle="--")
n = 5
x = np.linspace(0, 1, n)
V = np.zeros((n, n))
for i in range(n):
V[:, i] = x**i
la.cond(V)
Is there really a practical impact to this? Let's find out by plotting the error in an interpolant:
n = 20
def f(x):
return np.sin(2*np.pi*x)
x = np.linspace(0, 1, n)
V = np.zeros((n, n))
for i in range(n):
V[:, i] = x**i
coeffs = la.solve(V, f(x))
many_x = np.linspace(0, 1, 5000)
interp = 0
for i in range(n):
interp += coeffs[i]*many_x**i
pt.plot(many_x, f(many_x))
pt.semilogy(many_x, np.abs(interp - f(many_x)))
Observations?
# Those fuzzy bits down there are the floating point error.
# In practice, results can be *much* better than condition number suggests--even "most of the time".
# Condition number bounds are *worst-case*.