Last time¶
Interpolation using polynomials
Structure of generalized Vandermonde matrices
Conditioning of interpolation and Vandermonde matrices
Today (Read Sec 9.3: Stability of polynomial interpolation)¶
Choice of points and choice of basis
Runge phenomenon as ill conditioning
Stability and Chebyshev polynomials
Intro to piecewise methods
using LinearAlgebra
using Plots
default(linewidth=4, legendfontsize=12)
function vander(x, k=nothing)
if isnothing(k)
k = length(x)
end
m = length(x)
V = ones(m, k)
for j in 2:k
V[:, j] = V[:, j-1] .* x
end
V
end
function vander_newton(x, abscissa=nothing)
if isnothing(abscissa)
abscissa = x
end
n = length(abscissa)
A = ones(length(x), n)
for i in 2:n
A[:,i] = A[:,i-1] .* (x .- abscissa[i-1])
end
A
end
CosRange(a, b, n) = (a + b)/2 .+ (b - a)/2 * cos.(LinRange(-pi, 0, n))
CosRange (generic function with 1 method)A well-conditioned polynomial basis¶
function vander_legendre(x, k=nothing)
if isnothing(k)
k = length(x) # Square by default
end
m = length(x)
Q = ones(m, k)
Q[:, 2] = x
for n in 1:k-2
Q[:, n+2] = ((2*n + 1) * x .* Q[:, n+1] - n * Q[:, n]) / (n + 1)
end
Q
endvander_legendre (generic function with 2 methods)vcond(mat, points, nmax) = [cond(mat(points(-1, 1, n))) for n in 2:nmax]
plot([vcond(vander_legendre, CosRange, 20)], yscale=:log10)Loading...
A different set of points¶
CosRange(a, b, n) = (a + b)/2 .+ (b - a)/2 * cos.(LinRange(-pi, 0, n))
plot([vcond(vander, LinRange, 20)], yscale=:log10, size=(1000, 600))
plot!([vcond(vander, CosRange, 20)], yscale=:log10)
plot!([vcond(vander_legendre, LinRange, 20)], yscale=:log10)
plot!([vcond(vander_legendre, CosRange, 20)], yscale=:log10)Loading...
What’s wrong with ill-conditioning?¶
runge(x) = 1 / (1 + 10*x^2)
#runge(x) = sin(3x)
x = LinRange(-1, 1, 20) # try CosRange
y = runge.(x)
plot(runge, xlims=(-1, 1))
scatter!(x, y)Loading...
ourvander = vander_legendre # try vander_legendre
p = ourvander(x) \ y;
scatter(x, y)
s = LinRange(-1, 1, 100)
plot!(s, runge.(s))
plot!(s, ourvander(s, length(x)) * p)Loading...
svdvals(ourvander(s, length(x)) / ourvander(x))20-element Vector{Float64}:
2.8563807370813312
2.8414668608815536
2.820236386668483
2.7829719729254876
2.7474280450222
2.684651346430287
2.6367813664121758
2.545083971162912
2.486134057068077
2.3619485533713123
2.2916184164905107
2.132070881880068
2.0460305389351454
1.8547347714535936
1.734750748839897
1.5501994693431667
1.348199818311659
1.2519073527674076
0.9462546371465373
0.9419175178348709cond(ourvander(x))8.502660254099553Worst vector¶
A = ourvander(s, length(x)) / ourvander(x)
U, S, V = svd(A)
plot(x, V[:, 1:1])Loading...
plot(s, U[:,1], title=S[1])Loading...
Chebyshev polynomials¶
Define
This turns out to be a polynomial, but it’s not obvious why. Recall
Let and check
Adding these together produces
which yields a convenient recurrence:
function vander_chebyshev(x, n=nothing)
if isnothing(n)
n = length(x) # Square by default
end
m = length(x)
T = ones(m, n)
if n > 1
T[:, 2] = x
end
for k in 3:n
T[:, k] = 2 * x .* T[:,k-1] - T[:, k-2]
end
T
endvander_chebyshev (generic function with 2 methods)x = LinRange(-1, 1, 50)
plot(x, vander_chebyshev(x, 5), title="Chebyshev polynomials")Loading...
Chebyshev polynomials are well-conditioned¶
plot_vcond(mat, points) = plot!([
cond(mat(points(-1, 1, n)))
for n in 2:30], label="$mat/$points", marker=:auto, yscale=:log10)
plot(title="Vandermonde condition numbers", legend=:topleft, size=(1000, 600))
plot_vcond(vander, LinRange)
plot_vcond(vander, CosRange)
plot_vcond(vander_chebyshev, LinRange)
plot_vcond(vander_chebyshev, CosRange)Loading...
Lagrange interpolating polynomials revisited¶
Let’s re-examine the Lagrange polynomials as we vary the points.
x = CosRange(-1, 1, 11) # CosRange?
s = LinRange(-1, 1, 100)
A = vander_chebyshev(s, length(x)) /
vander_chebyshev(x)
plot(s, A[:,1:5], size=(1000, 600))
scatter!(x[1:5], ones(5), color=:black, label=nothing)
scatter!(x, zero(x), color=:black, label=nothing)Loading...
y = abs.(x)
plot(s, [abs.(s) A * y])Loading...
Are there artifacts?
Piecewise interpolation¶
function interp_nearest(x, s)
A = zeros(length(s), length(x))
for (i, t) in enumerate(s)
loc = nothing
dist = Inf
for (j, u) in enumerate(x)
if abs(t - u) < dist
loc = j
dist = abs(t - u)
end
end
A[i, loc] = 1
end
A
end
interp_nearest(LinRange(-1, 1, 3), LinRange(0, 1, 4))4×3 Matrix{Float64}:
0.0 1.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
0.0 0.0 1.0A = interp_nearest(x, s)
plot(s, A[:, 1:5])
scatter!(x, ones(length(x)))Loading...
I don’t see any visual artifacts¶
x = LinRange(-1, 1, 20)
A = interp_nearest(x, s)
cond(A)1.414213562373095plot(s, A * runge.(x))
plot!(s, runge.(s))
scatter!(x, runge.(x))Loading...
function interp_chebyshev(x, xx)
vander_chebyshev(xx, length(x)) * inv(vander_chebyshev(x))
end
function interp_monomial(x, xx)
vander(xx, length(x)) * inv(vander(x))
end
function interp_error(ieval, x, xx, test)
"""Compute norm of interpolation error for function test
using method interp_and_eval from points x to points xx.
"""
A = ieval(x, xx)
y = test.(x)
yy = test.(xx)
norm(A * y - yy, Inf)
end
function plot_convergence(ievals, ptspaces; xscale=:log10, yscale=:log10, maxpts=40)
"""Plot convergence rates for an interpolation scheme applied
to a set of tests.
"""
xx = LinRange(-1, 1, 100)
ns = 2:maxpts
fig = plot(title="Convergence",
xlabel="Number of points",
ylabel="Interpolation error",
xscale=xscale,
yscale=yscale,
legend=:bottomleft,
size=(1000, 700))
for ieval in ievals
for ptspace in ptspaces
for test in [runge]
errors = [interp_error(ieval, ptspace(-1, 1, n), xx, test)
for n in ns]
plot!(ns, errors, marker=:circle, label="$ieval/$ptspace")
end
end
end
for k in [1, 2, 3]
plot!(ns, ns .^ (-1.0*k), color=:black, label="\$n^{-$k}\$")
end
fig
end
plot_convergence (generic function with 1 method)So maybe it’s not that accurate?¶
plot_convergence([interp_monomial, interp_chebyshev, interp_nearest], [LinRange, CosRange])Loading...