Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Add more tests to Alefeld and fix bugs #58

Merged
merged 1 commit into from
Mar 31, 2023
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
8 changes: 6 additions & 2 deletions src/alefeld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
c = a - (b - a) / (f(b) - f(a)) * f(a)

fc = f(c)
if iszero(fc)
(a == c || b == c) &&
return SciMLBase.build_solution(prob, alg, c, fc;
retcode = ReturnCode.FloatingPointLimit,
left = a,
right = b)
iszero(fc) &&
return SciMLBase.build_solution(prob, alg, c, fc;
retcode = ReturnCode.Success,
left = a,
right = b)
end
a, b, d = _bracket(f, a, b, c)
e = zero(a) # Set e as 0 before iteration to avoid a non-value f(e)

Expand Down
25 changes: 25 additions & 0 deletions test/basictests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ for p in 1.1:0.1:100.0
@test ForwardDiff.derivative(g, p) ≈ 1 / (2 * sqrt(p))
end

f, tspan = (u, p) -> p[1] * u * u - p[2], (1.0, 100.0)
t = (p) -> [sqrt(p[2] / p[1])]
p = [0.9, 50.0]
g = function (p)
probN = IntervalNonlinearProblem{false}(f, tspan, p)
sol = solve(probN, Alefeld())
return [sol.u]
end

@test g(p) ≈ [sqrt(p[2] / p[1])]
@test ForwardDiff.jacobian(g, p) ≈ ForwardDiff.jacobian(t, p)

f, tspan = (u, p) -> p[1] * u * u - p[2], (1.0, 100.0)
t = (p) -> [sqrt(p[2] / p[1])]
p = [0.9, 50.0]
Expand Down Expand Up @@ -288,6 +300,7 @@ probB = IntervalNonlinearProblem(f, tspan)
sol = solve(probB, Falsi())
@test sol.left ≈ sqrt(2.0)

# Bisection
sol = solve(probB, Bisection())
@test sol.left ≈ sqrt(2.0)

Expand Down Expand Up @@ -315,6 +328,18 @@ probB = IntervalNonlinearProblem(f, tspan)
sol = solve(probB, Brent())
@test sol.left ≈ sqrt(2.0)

# Alefeld
sol = solve(probB, Alefeld())
@test sol.u ≈ sqrt(2.0)
tspan = (sqrt(2.0), 10.0)
probB = IntervalNonlinearProblem(f, tspan)
sol = solve(probB, Alefeld())
@test sol.u ≈ sqrt(2.0)
tspan = (0.0, sqrt(2.0))
probB = IntervalNonlinearProblem(f, tspan)
sol = solve(probB, Alefeld())
@test sol.u ≈ sqrt(2.0)

# Garuntee Tests for Bisection
f = function (u, p)
if u < 2.0
Expand Down