Consider the function $f(x)=|x|$ on $[-1,1]$. Given $n\neq0$, define $x_n^k=-1+\frac{2k}{n}$ for $0\leq k \leq n$. Let $g_n(x)$ be the unique polynomial of degree n which results by interpolating the data with $g_n(x_n^k)=f(x_n^k)$ for all $0\leq k \leq n$. Plot the sequence ${g_n(0.3)_{1\leq n \leq 20}}$
I understand how to find each $g_n$ separately by using the formula, but I'm not sure how to write code for this.
I don't have access to matlab right now (and so I cannot test the code), but here goes the corresponding Wolfram Mathematica code:
g[x_, n_] := InterpolatingPolynomial[ Table[{-1 + 2*k/n, Abs[-1 + 2*k/n]}, {k, 0, n}], x];
ListPlot[Table[g[0.3, n], {n, 1, 20}], PlotRange -> Full]