@@ -9,14 +9,13 @@ algorithm 4.1 because, in certain sense, the second algorithm(4.2) is an optimal
9
9
struct Alefeld <: AbstractBracketingAlgorithm end
10
10
11
11
function 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... )
16
15
f = Base. Fix2 (prob. f, prob. p)
17
16
a, b = prob. tspan
18
17
c = a - (b - a) / (f (b) - f (a)) * f (a)
19
-
18
+
20
19
fc = f (c)
21
20
(a == c || b == c) &&
22
21
return SciMLBase. build_solution (prob, alg, c, fc;
@@ -25,7 +24,7 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
25
24
right = b)
26
25
iszero (fc) &&
27
26
return SciMLBase. build_solution (prob, alg, c, fc;
28
- retcode = ReturnCode. Success,
27
+ retcode = ReturnCode. Success,
29
28
left = a,
30
29
right = b)
31
30
a, b, d = _bracket (f, a, b, c)
@@ -37,47 +36,47 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
37
36
f₁, f₂, f₃, f₄ = f (a), f (b), f (d), f (e)
38
37
if i == 2 || (f₁ == f₂ || f₁ == f₃ || f₁ == f₄ || f₂ == f₃ || f₂ == f₄ || f₃ == f₄)
39
38
c = _newton_quadratic (f, a, b, d, 2 )
40
- else
39
+ else
41
40
c = _ipzero (f, a, b, d, e)
42
41
if (c - a) * (c - b) ≥ 0
43
42
c = _newton_quadratic (f, a, b, d, 2 )
44
43
end
45
- end
44
+ end
46
45
ē, fc = d, f (c)
47
- (a == c || b == c) &&
46
+ (a == c || b == c) &&
48
47
return SciMLBase. build_solution (prob, alg, c, fc;
49
48
retcode = ReturnCode. FloatingPointLimit,
50
- left = a,
51
- right = b)
49
+ left = a,
50
+ right = b)
52
51
iszero (fc) &&
53
52
return SciMLBase. build_solution (prob, alg, c, fc;
54
- retcode = ReturnCode. Success,
55
- left = a,
56
- right = b)
57
- ā, b̄, d̄ = _bracket (f, a, b, c)
53
+ retcode = ReturnCode. Success,
54
+ left = a,
55
+ right = b)
56
+ ā, b̄, d̄ = _bracket (f, a, b, c)
58
57
59
58
# The second bracketing block
60
59
f₁, f₂, f₃, f₄ = f (ā), f (b̄), f (d̄), f (ē)
61
60
if f₁ == f₂ || f₁ == f₃ || f₁ == f₄ || f₂ == f₃ || f₂ == f₄ || f₃ == f₄
62
61
c = _newton_quadratic (f, ā, b̄, d̄, 3 )
63
- else
62
+ else
64
63
c = _ipzero (f, ā, b̄, d̄, ē)
65
64
if (c - ā) * (c - b̄) ≥ 0
66
65
c = _newton_quadratic (f, ā, b̄, d̄, 3 )
67
66
end
68
67
end
69
68
fc = f (c)
70
- (ā == c || b̄ == c) &&
69
+ (ā == c || b̄ == c) &&
71
70
return SciMLBase. build_solution (prob, alg, c, fc;
72
71
retcode = ReturnCode. FloatingPointLimit,
73
- left = ā,
72
+ left = ā,
74
73
right = b̄)
75
74
iszero (fc) &&
76
75
return SciMLBase. build_solution (prob, alg, c, fc;
77
- retcode = ReturnCode. Success,
78
- left = ā,
79
- right = b̄)
80
- ā, b̄, d̄ = _bracket (f, ā, b̄, c)
76
+ retcode = ReturnCode. Success,
77
+ left = ā,
78
+ right = b̄)
79
+ ā, b̄, d̄ = _bracket (f, ā, b̄, c)
81
80
82
81
# The third bracketing block
83
82
if abs (f (ā)) < abs (f (b̄))
@@ -90,17 +89,17 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
90
89
c = 0.5 * (ā + b̄)
91
90
end
92
91
fc = f (c)
93
- (ā == c || b̄ == c) &&
92
+ (ā == c || b̄ == c) &&
94
93
return SciMLBase. build_solution (prob, alg, c, fc;
95
94
retcode = ReturnCode. FloatingPointLimit,
96
- left = ā,
95
+ left = ā,
97
96
right = b̄)
98
97
iszero (fc) &&
99
98
return SciMLBase. build_solution (prob, alg, c, fc;
100
- retcode = ReturnCode. Success,
101
- left = ā,
102
- right = b̄)
103
- ā, b̄, d = _bracket (f, ā, b̄, c)
99
+ retcode = ReturnCode. Success,
100
+ left = ā,
101
+ right = b̄)
102
+ ā, b̄, d = _bracket (f, ā, b̄, c)
104
103
105
104
# The last bracketing block
106
105
if b̄ - ā < 0.5 * (b - a)
@@ -109,14 +108,14 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
109
108
e = d
110
109
c = 0.5 * (ā + b̄)
111
110
fc = f (c)
112
- (ā == c || b̄ == c) &&
111
+ (ā == c || b̄ == c) &&
113
112
return SciMLBase. build_solution (prob, alg, c, fc;
114
113
retcode = ReturnCode. FloatingPointLimit,
115
- left = ā,
114
+ left = ā,
116
115
right = b̄)
117
116
iszero (fc) &&
118
117
return SciMLBase. build_solution (prob, alg, c, fc;
119
- retcode = ReturnCode. Success,
118
+ retcode = ReturnCode. Success,
120
119
left = ā,
121
120
right = b̄)
122
121
a, b, d = _bracket (f, ā, b̄, c)
@@ -137,43 +136,45 @@ function SciMLBase.solve(prob::IntervalNonlinearProblem,
137
136
end
138
137
139
138
# Define subrotine function bracket, check fc before bracket to return solution
140
- function _bracket (f:: F , a, b, c) where F
139
+ function _bracket (f:: F , a, b, c) where {F}
141
140
if iszero (f (c))
142
141
ā, b̄, d = a, b, c
143
142
else
144
- if f (a) * f (c) < 0
143
+ if f (a) * f (c) < 0
145
144
ā, b̄, d = a, c, b
146
145
elseif f (b) * f (c) < 0
147
146
ā, b̄, d = c, b, a
148
147
end
149
148
end
150
149
151
- return ā, b̄, d
150
+ return ā, b̄, d
152
151
end
153
152
154
153
# Define subrotine function newton quadratic, return the approximation of zero
155
- function _newton_quadratic (f:: F , a, b, d, k) where F
156
- A = ((f (d) - f (b)) / (d - b) - (f (b) - f (a)) / (b - a)) / (d - a)
154
+ function _newton_quadratic (f:: F , a, b, d, k) where {F}
155
+ A = ((f (d) - f (b)) / (d - b) - (f (b) - f (a)) / (b - a)) / (d - a)
157
156
B = (f (b) - f (a)) / (b - a)
158
157
159
158
if iszero (A)
160
159
return a - (1 / B) * f (a)
161
160
elseif A * f (a) > 0
162
- rᵢ₋₁ = a
163
- else
161
+ rᵢ₋₁ = a
162
+ else
164
163
rᵢ₋₁ = b
165
- end
164
+ end
166
165
167
166
for i in 1 : k
168
- rᵢ = rᵢ₋₁ - (f (a) + B * (rᵢ₋₁ - a) + A * (rᵢ₋₁ - a) * (rᵢ₋₁ - b)) / (B + A * (2 * rᵢ₋₁ - a - b))
167
+ rᵢ = rᵢ₋₁ -
168
+ (f (a) + B * (rᵢ₋₁ - a) + A * (rᵢ₋₁ - a) * (rᵢ₋₁ - b)) /
169
+ (B + A * (2 * rᵢ₋₁ - a - b))
169
170
rᵢ₋₁ = rᵢ
170
171
end
171
172
172
173
return rᵢ₋₁
173
174
end
174
175
175
176
# Define subrotine function ipzero, also return the approximation of zero
176
- function _ipzero (f:: F , a, b, c, d) where F
177
+ function _ipzero (f:: F , a, b, c, d) where {F}
177
178
Q₁₁ = (c - d) * f (c) / (f (d) - f (c))
178
179
Q₂₁ = (b - c) * f (b) / (f (c) - f (b))
179
180
Q₃₁ = (a - b) * f (a) / (f (b) - f (a))
@@ -185,4 +186,4 @@ function _ipzero(f::F, a, b, c, d) where F
185
186
Q₃₃ = (D₃₂ - Q₂₂) * f (a) / (f (d) - f (a))
186
187
187
188
return a + Q₃₁ + Q₃₂ + Q₃₃
188
- end
189
+ end
0 commit comments