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

Commit 7219d9f

Browse files
committed
fix bugs
1 parent fa0395b commit 7219d9f

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/alefeld.jl

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
1616
f = Base.Fix2(prob.f, prob.p)
1717
a, b = prob.tspan
1818
c = a - (b - a) / (f(b) - f(a)) * f(a)
19-
@show c
2019

2120
fc = f(c)
2221
if iszero(fc)
@@ -26,7 +25,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
2625
right = b)
2726
end
2827
a, b, d = _bracket(f, a, b, c)
29-
e = 0 # Set e as 0 before iteration to avoid a non-value f(e)
28+
e = zero(a) # Set e as 0 before iteration to avoid a non-value f(e)
3029

3130
# Begin of algorithm iteration
3231
for i in 2:maxiters
@@ -40,7 +39,12 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
4039
c = _newton_quadratic(f, a, b, d, 2)
4140
end
4241
end
43-
ē, fc = d, f(c)
42+
ē, fc = d, f(c)
43+
(a == c || b == c) &&
44+
return SciMLBase.build_solution(prob, alg, c, fc;
45+
retcode = ReturnCode.FloatingPointLimit,
46+
left = a,
47+
right = b)
4448
iszero(fc) &&
4549
return SciMLBase.build_solution(prob, alg, c, fc;
4650
retcode = ReturnCode.Success,
@@ -59,11 +63,16 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
5963
end
6064
end
6165
fc = f(c)
66+
(ā == c ||== c) &&
67+
return SciMLBase.build_solution(prob, alg, c, fc;
68+
retcode = ReturnCode.FloatingPointLimit,
69+
left = ā,
70+
right = b̄)
6271
iszero(fc) &&
6372
return SciMLBase.build_solution(prob, alg, c, fc;
6473
retcode = ReturnCode.Success,
65-
left = a,
66-
right = b)
74+
left = ,
75+
right = )
6776
ā, b̄, d̄ = _bracket(f, ā, b̄, c)
6877

6978
# The third bracketing block
@@ -77,11 +86,16 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
7786
c = 0.5 * (ā + b̄)
7887
end
7988
fc = f(c)
89+
(ā == c ||== c) &&
90+
return SciMLBase.build_solution(prob, alg, c, fc;
91+
retcode = ReturnCode.FloatingPointLimit,
92+
left = ā,
93+
right = b̄)
8094
iszero(fc) &&
8195
return SciMLBase.build_solution(prob, alg, c, fc;
8296
retcode = ReturnCode.Success,
83-
left = a,
84-
right = b)
97+
left = ā,
98+
right = )
8599
ā, b̄, d = _bracket(f, ā, b̄, c)
86100

87101
# The last bracketing block
@@ -91,11 +105,16 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
91105
e = d
92106
c = 0.5 * (ā + b̄)
93107
fc = f(c)
108+
(ā == c ||== c) &&
109+
return SciMLBase.build_solution(prob, alg, c, fc;
110+
retcode = ReturnCode.FloatingPointLimit,
111+
left = ā,
112+
right = b̄)
94113
iszero(fc) &&
95-
return SciMLBase.build_solution(prob, alg, c, fc;
96-
retcode = ReturnCode.Success,
97-
left = a,
98-
right = b)
114+
return SciMLBase.build_solution(prob, alg, c, fc;
115+
retcode = ReturnCode.Success,
116+
left = ,
117+
right = )
99118
a, b, d = _bracket(f, ā, b̄, c)
100119
end
101120
end
@@ -142,7 +161,7 @@ function _newton_quadratic(f::F, a, b, d, k) where F
142161
end
143162

144163
for i in 1:k
145-
rᵢ = rᵢ₋₁ - B * rᵢ₋₁ / (B + A * (2 * rᵢ₋₁ - a - b))
164+
rᵢ = rᵢ₋₁ - (f(a) + B * (rᵢ₋₁ - a) + A * (rᵢ₋₁ - a) * (rᵢ₋₁ - b)) / (B + A * (2 * rᵢ₋₁ - a - b))
146165
rᵢ₋₁ = rᵢ
147166
end
148167

0 commit comments

Comments
 (0)