Skip to content

Commit

Permalink
Merge pull request #58 from huiyuxie/main
Browse files Browse the repository at this point in the history
Add more tests to Alefeld and fix bugs
  • Loading branch information
ChrisRackauckas authored Mar 31, 2023
2 parents 58ffb58 + 755fec8 commit 3e495be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/SimpleNonlinearSolve/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 lib/SimpleNonlinearSolve/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

0 comments on commit 3e495be

Please sign in to comment.