Interpolations.jl icon indicating copy to clipboard operation
Interpolations.jl copied to clipboard

Periodic{OnGrid} is problematic

Open mkitti opened this issue 3 years ago • 1 comments

Report via Slack: https://julialang.slack.com/archives/C6A044SQH/p1655221621792669

I am having a problem with the Interpolations.jl package. Trying to do a periodic cubic spline interpolation results in the following:

julia> itp = interpolate([1,2,3,4,1], BSpline(Cubic(Periodic(OnGrid()))));
julia> Interpolations.gradient(itp, 1)
1-element StaticArrays.SVector{1, Float64} with indices SOneTo(1):
 1.0909090909090908
julia> Interpolations.gradient(itp, 5)
1-element StaticArrays.SVector{1, Float64} with indices SOneTo(1):
 -2.181818181818182
I would have expected itp'(1) = itp'(5) and itp''(1) = itp''(5).

Am I doing something wrong here? (edited)

mkitti avatar Jun 14 '22 16:06 mkitti

Consider using OnCell():

itp = extrapolate(interpolate([1,2,3,4], BSpline(Cubic(Periodic(OnCell())))), Periodic());
x = 1:0.1:10; plot(x, itp.(x));
plot!(1:10, itp.(1:10), marker = :o, line = nothing)

image

x = 1:0.1:10; plot(x, first.(gradient.(Ref(itp), x)));
plot!(1:10, first.(gradient.(Ref(itp),1:10)), marker = :o, line = nothing)

image

mkitti avatar Jun 14 '22 16:06 mkitti