Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CubicSpline's natural boundary conditions #235

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/interpolation_caches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ function QuadraticSpline(u::uType, t; extrapolate = false) where {uType <: Abstr
end

"""
QuadraticSpline(u, t; extrapolate = false)
CubicSpline(u, t; extrapolate = false)

It is a spline interpolation using piecewise cubic polynomials between each pair of data points. Its first and second derivative is also continuous.
Extrapolation extends the last cubic polynomial on each side.
Second derivative on both ends are zero, which are also called "natural" boundary conditions. Extrapolation extends the last cubic polynomial on each side.

## Arguments

Expand Down Expand Up @@ -303,9 +303,9 @@ function CubicSpline(u::uType,
u, t = munge_data(u, t)
n = length(t) - 1
h = vcat(0, map(k -> t[k + 1] - t[k], 1:(length(t) - 1)), 0)
dl = h[2:(n + 1)]
dl = vcat(h[2:n], zero(eltype(h)))
d_tmp = 2 .* (h[1:(n + 1)] .+ h[2:(n + 2)])
du = h[2:(n + 1)]
du = vcat(zero(eltype(h)), h[3:(n + 1)])
tA = Tridiagonal(dl, d_tmp, du)

# zero for element type of d, which we don't know yet
Expand All @@ -324,9 +324,9 @@ function CubicSpline(u::uType, t; extrapolate = false) where {uType <: AbstractV
u, t = munge_data(u, t)
n = length(t) - 1
h = vcat(0, map(k -> t[k + 1] - t[k], 1:(length(t) - 1)), 0)
dl = h[2:(n + 1)]
dl = vcat(h[2:n], zero(eltype(h)))
d_tmp = 2 .* (h[1:(n + 1)] .+ h[2:(n + 2)])
du = h[2:(n + 1)]
du = vcat(zero(eltype(h)), h[3:(n + 1)])
tA = Tridiagonal(dl, d_tmp, du)
d_ = map(
i -> i == 1 || i == n + 1 ? zeros(eltype(t), size(u[1])) :
Expand Down
8 changes: 4 additions & 4 deletions test/interpolation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ end
A = CubicSpline(u, t; extrapolate = true)

# Solution
P₁ = x -> 1 + 1.5x + x^2 + 0.5x^3 # for x ∈ [-1.0, 0.0]
P₂ = x -> 1 + 1.5x + x^2 - 0.5x^3 # for x ∈ [0.0, 1.0]
P₁ = x -> 1 + 1.5x + 0.75 * x^2 + 0.25 * x^3 # for x ∈ [-1.0, 0.0]
P₂ = x -> 1 + 1.5x + 0.75 * x^2 - 0.25 * x^3 # for x ∈ [0.0, 1.0]

for (_t, _u) in zip(t, u)
@test A(_t) == _u
Expand Down Expand Up @@ -534,8 +534,8 @@ end
u = [0.0, 1.0, 3.0]
t = [-1.0, 0.0, 1.0]
A = CubicSpline(u, t; extrapolate = true)
@test A(-2.0) ≈ -2.0
@test A(2.0) ≈ 4.0
@test A(-2.0) ≈ -1.0
@test A(2.0) ≈ 5.0
A = CubicSpline(u, t)
@test_throws DataInterpolations.ExtrapolationError A(-2.0)
@test_throws DataInterpolations.ExtrapolationError A(2.0)
Expand Down
Loading