Skip to content

Commit

Permalink
Updating Brent
Browse files Browse the repository at this point in the history
  • Loading branch information
Deltadahl committed Jan 30, 2023
1 parent 8e57137 commit c9ae379
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/SimpleNonlinearSolve/src/brent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
f = Base.Fix2(prob.f, prob.p)
a, b = prob.tspan
fa, fb = f(a), f(b)
ϵ = eps(convert(typeof(fa), 1.0))

if iszero(fa)
return SciMLBase.build_solution(prob, alg, a, fa;
Expand Down Expand Up @@ -46,8 +47,8 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
if (s < min((3 * a + b) / 4, b) || s > max((3 * a + b) / 4, b)) ||
(cond && abs(s - b) abs(b - c) / 2) ||
(!cond && abs(s - b) abs(c - d) / 2) ||
(cond && abs(b - c) eps(a)) ||
(!cond && abs(c - d) eps(a))
(cond && abs(b - c) ϵ) ||
(!cond && abs(c - d) ϵ)
# Bisection method
s = (a + b) / 2
(s == a || s == b) &&
Expand All @@ -60,8 +61,12 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
end
fs = f(s)
if iszero(fs)
a = b
if b < a
a = b
fa = fb
end
b = s
fb = fs
break
end
if fa * fs < 0
Expand Down Expand Up @@ -103,7 +108,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
end
i += 1
end

return SciMLBase.build_solution(prob, alg, a, fa; retcode = ReturnCode.MaxIters,
left = a, right = b)
end

0 comments on commit c9ae379

Please sign in to comment.