@@ -9,18 +9,17 @@ algorithm 4.1 because, in certain sense, the second algorithm(4.2) is an optimal
99struct Alefeld <: AbstractBracketingAlgorithm end
1010
1111function SciMLBase. solve (prob:: IntervalNonlinearProblem ,
12- alg:: Alefeld , args... ; abstol = nothing ,
13- reltol = nothing ,
14- maxiters = 1000 , kwargs... )
15-
12+ alg:: Alefeld , args... ; abstol = nothing ,
13+ reltol = nothing ,
14+ maxiters = 1000 , kwargs... )
1615 f = Base. Fix2 (prob. f, prob. p)
1716 a, b = prob. tspan
1817 c = a - (b - a) / (f (b) - f (a)) * f (a)
19-
18+
2019 fc = f (c)
2120 if iszero (fc)
2221 return SciMLBase. build_solution (prob, alg, c, fc;
23- retcode = ReturnCode. Success,
22+ retcode = ReturnCode. Success,
2423 left = a,
2524 right = b)
2625 end
@@ -33,47 +32,47 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
3332 f₁, f₂, f₃, f₄ = f (a), f (b), f (d), f (e)
3433 if i == 2 || (f₁ == f₂ || f₁ == f₃ || f₁ == f₄ || f₂ == f₃ || f₂ == f₄ || f₃ == f₄)
3534 c = _newton_quadratic (f, a, b, d, 2 )
36- else
35+ else
3736 c = _ipzero (f, a, b, d, e)
3837 if (c - a) * (c - b) ≥ 0
3938 c = _newton_quadratic (f, a, b, d, 2 )
4039 end
41- end
40+ end
4241 ē, fc = d, f (c)
43- (a == c || b == c) &&
42+ (a == c || b == c) &&
4443 return SciMLBase. build_solution (prob, alg, c, fc;
4544 retcode = ReturnCode. FloatingPointLimit,
46- left = a,
47- right = b)
45+ left = a,
46+ right = b)
4847 iszero (fc) &&
4948 return SciMLBase. build_solution (prob, alg, c, fc;
50- retcode = ReturnCode. Success,
51- left = a,
52- right = b)
53- ā, b̄, d̄ = _bracket (f, a, b, c)
49+ retcode = ReturnCode. Success,
50+ left = a,
51+ right = b)
52+ ā, b̄, d̄ = _bracket (f, a, b, c)
5453
5554 # The second bracketing block
5655 f₁, f₂, f₃, f₄ = f (ā), f (b̄), f (d̄), f (ē)
5756 if f₁ == f₂ || f₁ == f₃ || f₁ == f₄ || f₂ == f₃ || f₂ == f₄ || f₃ == f₄
5857 c = _newton_quadratic (f, ā, b̄, d̄, 3 )
59- else
58+ else
6059 c = _ipzero (f, ā, b̄, d̄, ē)
6160 if (c - ā) * (c - b̄) ≥ 0
6261 c = _newton_quadratic (f, ā, b̄, d̄, 3 )
6362 end
6463 end
6564 fc = f (c)
66- (ā == c || b̄ == c) &&
65+ (ā == c || b̄ == c) &&
6766 return SciMLBase. build_solution (prob, alg, c, fc;
6867 retcode = ReturnCode. FloatingPointLimit,
69- left = ā,
68+ left = ā,
7069 right = b̄)
7170 iszero (fc) &&
7271 return SciMLBase. build_solution (prob, alg, c, fc;
73- retcode = ReturnCode. Success,
74- left = ā,
75- right = b̄)
76- ā, b̄, d̄ = _bracket (f, ā, b̄, c)
72+ retcode = ReturnCode. Success,
73+ left = ā,
74+ right = b̄)
75+ ā, b̄, d̄ = _bracket (f, ā, b̄, c)
7776
7877 # The third bracketing block
7978 if abs (f (ā)) < abs (f (b̄))
@@ -86,17 +85,17 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
8685 c = 0.5 * (ā + b̄)
8786 end
8887 fc = f (c)
89- (ā == c || b̄ == c) &&
88+ (ā == c || b̄ == c) &&
9089 return SciMLBase. build_solution (prob, alg, c, fc;
9190 retcode = ReturnCode. FloatingPointLimit,
92- left = ā,
91+ left = ā,
9392 right = b̄)
9493 iszero (fc) &&
9594 return SciMLBase. build_solution (prob, alg, c, fc;
96- retcode = ReturnCode. Success,
97- left = ā,
98- right = b̄)
99- ā, b̄, d = _bracket (f, ā, b̄, c)
95+ retcode = ReturnCode. Success,
96+ left = ā,
97+ right = b̄)
98+ ā, b̄, d = _bracket (f, ā, b̄, c)
10099
101100 # The last bracketing block
102101 if b̄ - ā < 0.5 * (b - a)
@@ -105,14 +104,14 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
105104 e = d
106105 c = 0.5 * (ā + b̄)
107106 fc = f (c)
108- (ā == c || b̄ == c) &&
107+ (ā == c || b̄ == c) &&
109108 return SciMLBase. build_solution (prob, alg, c, fc;
110109 retcode = ReturnCode. FloatingPointLimit,
111- left = ā,
110+ left = ā,
112111 right = b̄)
113112 iszero (fc) &&
114113 return SciMLBase. build_solution (prob, alg, c, fc;
115- retcode = ReturnCode. Success,
114+ retcode = ReturnCode. Success,
116115 left = ā,
117116 right = b̄)
118117 a, b, d = _bracket (f, ā, b̄, c)
@@ -133,43 +132,45 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
133132end
134133
135134# Define subrotine function bracket, check fc before bracket to return solution
136- function _bracket (f:: F , a, b, c) where F
135+ function _bracket (f:: F , a, b, c) where {F}
137136 if iszero (f (c))
138137 ā, b̄, d = a, b, c
139138 else
140- if f (a) * f (c) < 0
139+ if f (a) * f (c) < 0
141140 ā, b̄, d = a, c, b
142141 elseif f (b) * f (c) < 0
143142 ā, b̄, d = c, b, a
144143 end
145144 end
146145
147- return ā, b̄, d
146+ return ā, b̄, d
148147end
149148
150149# Define subrotine function newton quadratic, return the approximation of zero
151- function _newton_quadratic (f:: F , a, b, d, k) where F
152- A = ((f (d) - f (b)) / (d - b) - (f (b) - f (a)) / (b - a)) / (d - a)
150+ function _newton_quadratic (f:: F , a, b, d, k) where {F}
151+ A = ((f (d) - f (b)) / (d - b) - (f (b) - f (a)) / (b - a)) / (d - a)
153152 B = (f (b) - f (a)) / (b - a)
154153
155154 if iszero (A)
156155 return a - (1 / B) * f (a)
157156 elseif A * f (a) > 0
158- rᵢ₋₁ = a
159- else
157+ rᵢ₋₁ = a
158+ else
160159 rᵢ₋₁ = b
161- end
160+ end
162161
163162 for i in 1 : k
164- rᵢ = rᵢ₋₁ - (f (a) + B * (rᵢ₋₁ - a) + A * (rᵢ₋₁ - a) * (rᵢ₋₁ - b)) / (B + A * (2 * rᵢ₋₁ - a - b))
163+ rᵢ = rᵢ₋₁ -
164+ (f (a) + B * (rᵢ₋₁ - a) + A * (rᵢ₋₁ - a) * (rᵢ₋₁ - b)) /
165+ (B + A * (2 * rᵢ₋₁ - a - b))
165166 rᵢ₋₁ = rᵢ
166167 end
167168
168169 return rᵢ₋₁
169170end
170171
171172# Define subrotine function ipzero, also return the approximation of zero
172- function _ipzero (f:: F , a, b, c, d) where F
173+ function _ipzero (f:: F , a, b, c, d) where {F}
173174 Q₁₁ = (c - d) * f (c) / (f (d) - f (c))
174175 Q₂₁ = (b - c) * f (b) / (f (c) - f (b))
175176 Q₃₁ = (a - b) * f (a) / (f (b) - f (a))
@@ -181,4 +182,4 @@ function _ipzero(f::F, a, b, c, d) where F
181182 Q₃₃ = (D₃₂ - Q₂₂) * f (a) / (f (d) - f (a))
182183
183184 return a + Q₃₁ + Q₃₂ + Q₃₃
184- end
185+ end
0 commit comments