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

Commit 98eb885

Browse files
committed
Updating Brent
1 parent 74e257b commit 98eb885

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/brent.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
1212
f = Base.Fix2(prob.f, prob.p)
1313
a, b = prob.tspan
1414
fa, fb = f(a), f(b)
15+
ϵ = eps(convert(typeof(fa), 1.0))
1516

1617
if iszero(fa)
1718
return SciMLBase.build_solution(prob, alg, a, fa;
@@ -46,8 +47,8 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
4647
if (s < min((3 * a + b) / 4, b) || s > max((3 * a + b) / 4, b)) ||
4748
(cond && abs(s - b) abs(b - c) / 2) ||
4849
(!cond && abs(s - b) abs(c - d) / 2) ||
49-
(cond && abs(b - c) eps(a)) ||
50-
(!cond && abs(c - d) eps(a))
50+
(cond && abs(b - c) ϵ) ||
51+
(!cond && abs(c - d) ϵ)
5152
# Bisection method
5253
s = (a + b) / 2
5354
(s == a || s == b) &&
@@ -60,8 +61,12 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
6061
end
6162
fs = f(s)
6263
if iszero(fs)
63-
a = b
64+
if b < a
65+
a = b
66+
fa = fb
67+
end
6468
b = s
69+
fb = fs
6570
break
6671
end
6772
if fa * fs < 0
@@ -103,7 +108,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Brent, args...;
103108
end
104109
i += 1
105110
end
106-
111+
107112
return SciMLBase.build_solution(prob, alg, a, fa; retcode = ReturnCode.MaxIters,
108113
left = a, right = b)
109114
end

0 commit comments

Comments
 (0)