Poor conditioning in polynomial interpolation

Here are some points that we could consider to be observations of an unknown function on \([-1,1]\).

using FundamentalsNumericalComputation
n = 5
t = LinRange(-1,1,n+1)
y = @. t^2 + t + 0.05*sin(20*t)

scatter(t,y,label="data",leg=:top)

The polynomial interpolant, as computed using polyfit, looks very sensible. It’s the kind of function you’d take home to meet your parents.

p = fit(t,y,n)     # interpolating polynomial
plot!(p,-1,1,label="interpolant")

But now consider a different set of points generated in almost exactly the same way.

n = 18
t = LinRange(-1,1,n+1)
y = @. t^2 + t + 0.05*sin(20*t)

scatter(t,y,m=:o,l=nothing,label="data",leg=:top)

The points themselves are unremarkable. But take a look at what happens to the polynomial interpolant.

p = fit(t,y,n)
x = LinRange(-1,1,1000)  # use a lot of points
plot!(x,p.(x),label="interpolant")

Surely there must be functions that are more intuitively representative of those points!