Skip to content

Commit 41be74a

Browse files
Merge pull request #1162 from dcourteville/itpfix
Ensure internal ITP bounds are in proper order
2 parents 100f97b + be30788 commit 41be74a

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/internal_itp.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T, T}}, alg::I
3030
args...;
3131
maxiters = 1000, kwargs...) where {IP, T}
3232
f = Base.Fix2(prob.f, prob.p)
33-
left, right = prob.tspan # a and b
33+
left, right = minmax(prob.tspan...) # a and b
3434
fl, fr = f(left), f(right)
3535
ϵ = eps(T)
3636
if iszero(fl)
@@ -40,7 +40,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T, T}}, alg::I
4040
return SciMLBase.build_solution(prob, alg, right, fr;
4141
retcode = ReturnCode.ExactSolutionRight, left, right)
4242
end
43-
span = abs(right - left)
43+
span = right - left
4444
k1 = T(alg.scaled_k1) / span
4545
n0 = T(alg.n0)
4646
n_h = exponent(span / (2 * ϵ))
@@ -49,7 +49,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem{IP, Tuple{T, T}}, alg::I
4949

5050
i = 1
5151
while i maxiters
52-
span = abs(right - left)
52+
span = right - left
5353
mid = (left + right) / 2
5454
r = ϵ_s - (span / 2)
5555

test/internal_rootfinder.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ for Rootfinder in (InternalITP,)
3636
@test abs.(solve(inp3, rf).u) sqrt.(p)
3737
@test abs.(solve(inp4, rf).u) sqrt.(p)
3838
end
39+
40+
# https://github.com/SciML/DifferentialEquations.jl/issues/1087
41+
# Test with normal and reversed tspan.
42+
# The expected zero is -acos(p), but there is another zero at acos(p) just outside tspan
43+
f(u, p) = cos(u) - p
44+
p = 0.9
45+
inp1 = IntervalNonlinearProblem(f, (-1.1 * acos(p), 0.9 * acos(p)), p)
46+
inp2 = IntervalNonlinearProblem(f, (0.9 * acos(p), -1.1 * acos(p)), p)
47+
@test solve(inp1, rf).u -acos(p)
48+
@test solve(inp2, rf).u -acos(p)
3949
end

0 commit comments

Comments
 (0)