@@ -7,16 +7,16 @@ and a `debug` boolean argument that prints out diagnostic information."""
7
7
function newton1d {T} (f:: Function , f′:: Function , x:: Interval{T} ;
8
8
reltol= eps (T), abstol= eps (T), debug= false , debugroot= false )
9
9
10
- L = Interval{T}[]
10
+ L = Interval{T}[] # Array to hold the intervals still to be processed
11
11
12
- R = Root{Interval{T}}[]
12
+ R = Root{Interval{T}}[] # Array to hold the `root` objects obtained
13
13
reps = reps1 = 0
14
14
15
- push! (L, x)
15
+ push! (L, x) # Initialize
16
16
initial_width = ∞
17
- X = emptyinterval (T)
18
- while ! isempty (L)
19
- X = pop! (L)
17
+ X = emptyinterval (T) # Initialize
18
+ while ! isempty (L) # Until all intervals have been processed
19
+ X = pop! (L) # Process next interval
20
20
21
21
debug && (print (" Current interval popped: " ); @show X)
22
22
@@ -32,10 +32,10 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
32
32
while true
33
33
34
34
m = mid (X)
35
- N = m - (f (interval (m)) / f′ (X))
35
+ N = m - (f (interval (m)) / f′ (X)) # Newton step
36
36
37
37
debug && (print (" Newton step1: " ); @show (X, X ∩ N))
38
- if X == X ∩ N
38
+ if X == X ∩ N # Checking if Newton step was redundant
39
39
reps1 += 1
40
40
if reps1 > 20
41
41
reps1 = 0
@@ -44,64 +44,64 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
44
44
end
45
45
X = X ∩ N
46
46
47
- if (isempty (X) || diam (X) == 0 )
47
+ if (isempty (X)) # No root in X
48
48
break
49
49
50
- elseif 0 ∈ f (interval ( prevfloat ( mid (X)), nextfloat ( mid (X))))
50
+ elseif 0 ∈ f (wideinterval ( mid (X))) # Root guaranteed to be in X
51
51
n = fa = fb = 0
52
52
root_exist = true
53
- while (n < 4 && (fa == 0 || fb == 0 ))
53
+ while (n < 4 && (fa == 0 || fb == 0 )) # Narrowing the interval further
54
54
if fa == 0
55
- if 0 ∈ f (interval ( prevfloat ( X. lo), nextfloat (X . lo) ))
55
+ if 0 ∈ f (wideinterval ( X. lo))
56
56
fa = 1
57
57
else
58
58
N = X. lo - (f (interval (X. lo)) / f′ (X))
59
59
X = X ∩ N
60
- if (isempty (X) || diam (X) == 0 )
60
+ if (isempty (X))
61
61
root_exist = false
62
62
break
63
63
end
64
64
end
65
65
end
66
66
if fb == 0
67
- if 0 ∈ f (interval ( prevfloat ( X. hi), nextfloat (X . hi) ))
67
+ if 0 ∈ f (wideinterval ( X. hi))
68
68
fb = 1
69
69
else
70
- if 0 ∈ f (interval ( prevfloat ( mid (X)), nextfloat ( mid (X) )))
70
+ if 0 ∈ f (wideinterval ( mid (X)))
71
71
N = X. hi - (f (interval (X. hi)) / f′ (X))
72
72
else
73
73
N = mid (X) - (f (interval (mid (X))) / f′ (X))
74
74
end
75
75
X = X ∩ N
76
- if (isempty (X) || diam (X) == 0 )
76
+ if (isempty (X))
77
77
root_exist = false
78
78
break
79
79
end
80
80
end
81
81
end
82
82
N = mid (X) - (f (interval (mid (X))) / f′ (X))
83
83
X = X ∩ N
84
- if (isempty (X) || diam (X) == 0 )
84
+ if (isempty (X))
85
85
root_exist = false
86
86
break
87
87
end
88
88
n += 1
89
89
end
90
90
if root_exist
91
91
push! (R, Root (X, :unique ))
92
- debugroot && @show " Root found" , X
92
+ debugroot && @show " Root found" , X # Storing determined unique root
93
93
end
94
94
95
95
break
96
96
end
97
97
end
98
98
99
- else
100
- if diam (X) == initial_width
99
+ else # 0 ∈ f′(X)
100
+ if diam (X) == initial_width # if no improvement occuring for a number of iterations
101
101
reps += 1
102
102
if reps > 10
103
103
push! (R, Root (X, :unknown ))
104
- debugroot && @show " Repititive root found" , X
104
+ debugroot && @show " Repeated root found" , X
105
105
reps = 0
106
106
continue
107
107
end
@@ -112,21 +112,21 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
112
112
expansion_pt = Inf
113
113
# expansion point for the newton step might be m, X.lo or X.hi according to some conditions
114
114
115
- if 0 ∈ f (interval ( prevfloat ( mid (X)), nextfloat ( mid (X))))
115
+ if 0 ∈ f (wideinterval ( mid (X))) # if root in X, narrow interval further
116
116
# 0 ∈ fⁱ(x)
117
117
118
118
debug && println (" 0 ∈ fⁱ(x)" )
119
119
120
- if 0 ∉ f (interval ( prevfloat ( X. lo), nextfloat (X . lo) ))
120
+ if 0 ∉ f (wideinterval ( X. lo))
121
121
expansion_pt = X. lo
122
122
123
- elseif 0 ∉ f (interval ( prevfloat ( X. hi), nextfloat (X . hi) ))
123
+ elseif 0 ∉ f (wideinterval ( X. hi))
124
124
expansion_pt = X. hi
125
125
126
126
else
127
127
x1 = mid (interval (X. lo, mid (X)))
128
128
x2 = mid (interval (mid (X), X. hi))
129
- if 0 ∉ f (interval ( prevfloat ( x1), nextfloat (x1))) || 0 ∉ f (interval ( prevfloat (x2), nextfloat (x2) ))
129
+ if 0 ∉ f (wideinterval ( x1)) || 0 ∉ f (wideinterval (x2 ))
130
130
push! (L, interval (X. lo, m))
131
131
push! (L, interval (m, X. hi))
132
132
continue
@@ -145,7 +145,7 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
145
145
146
146
debug && println (" 0 ∉ fⁱ(x)" )
147
147
148
- if (diam (X)/ mag (X)) < reltol && diam (f (X)) < abstol
148
+ if (diam (X)/ mag (X)) < reltol && diam (f (X)) < abstol # checking if X is still within tolerances
149
149
push! (R, Root (X, :unknown ))
150
150
151
151
debugroot && @show " Tolerance root found" , X
@@ -189,7 +189,7 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
189
189
end
190
190
end
191
191
192
- push! (L, X)
192
+ push! (L, X) # Pushing X into L to be processed again
193
193
end
194
194
end
195
195
0 commit comments