Precept 10: Barycentric interpolation, Runge's phenomenon and Gibb's phenomenon

In this precept, we experiment with polynomial interpolants.

Recall the formulas for Barycentric interpolation of points $\{x_i, y_i\}_{i=0}^{n}$:

$$ w_k = \frac{1}{\prod_{i=0 \dots n, i\neq k} (x_k - x_i)} $$$$ p_n(x) = \frac{\sum_{k=0}^n \frac{w_k y_k}{(x-x_k)}}{\sum_{k=0}^{n} \frac{w_k}{(x - x_k)}} $$

for $x \neq x_i$.

The uniform and Chebysev points (of the second kind) on $[-1, 1]$ are defined as :

$$ x_i = -1 + 2 \frac{i}{n} $$$$ x_i = \cos \left( \frac{i\pi}{n} \right) $$

for $i = 0, \dots n$

Implement a polynomial interpolant of the following functions using Barycentric interpolation. Check that the point to evaluate is not of of the nodes, up to $10^{-15}$. If it is, return the corresponding value $y_i$.

Test it on uniform points and the function $f_1(x) = \sin(2\pi x)$ below.

Test your code on:

At uniform, Chebyshev and random point. You can get random points on $[-1,1]$ by running pts = np.random.rand(n)*2 -1.

Visualize the functions are their interpolant for n = 5, 10, 30.

Then, make a convergence plot of the max error as n increases from n = 1 to n = 50. To do this, approximate and plot the error between the interpolant and the function $\max_x |f(x) - p_n(x)| $. You can approximate the max by taking the max over a fine uniform grid with $n = 10^4$ points (see code above).

Do the interpolant converge for $f_1(x)$ ? What about $f_2(x)$?

Dos the interpolant at random points behave like the uniform interpolant or like the Chebyshev interpolant?

Test it on:

Plot the the Chebyshev interpolant of $f_3$ for different values of n. How does the oscillation near $0$ change with $n$? Make a convergence plot for n = 5 to 500 in increment of 10.

This tendency of polynomial interpolant to oscillate near discontinuities is called Gibb's phenomenon.