@@ -7,16 +7,16 @@ and a `debug` boolean argument that prints out diagnostic information."""
77function newton1d {T} (f:: Function , f′:: Function , x:: Interval{T} ;
88 reltol= eps (T), abstol= eps (T), debug= false , debugroot= false )
99
10- L = Interval{T}[]
10+ L = Interval{T}[] # Array to hold the intervals still to be processed
1111
12- R = Root{Interval{T}}[]
12+ R = Root{Interval{T}}[] # Array to hold the `root` objects obtained
1313 reps = reps1 = 0
1414
15- push! (L, x)
15+ push! (L, x) # Initialize
1616 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
2020
2121 debug && (print (" Current interval popped: " ); @show X)
2222
@@ -32,10 +32,10 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
3232 while true
3333
3434 m = mid (X)
35- N = m - (f (interval (m)) / f′ (X))
35+ N = m - (f (interval (m)) / f′ (X)) # Newton step
3636
3737 debug && (print (" Newton step1: " ); @show (X, X ∩ N))
38- if X == X ∩ N
38+ if X == X ∩ N # Checking if Newton step was redundant
3939 reps1 += 1
4040 if reps1 > 20
4141 reps1 = 0
@@ -44,64 +44,64 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
4444 end
4545 X = X ∩ N
4646
47- if (isempty (X) || diam (X) == 0 )
47+ if (isempty (X)) # No root in X
4848 break
4949
50- elseif 0 ∈ f (interval ( prevfloat ( mid (X)), nextfloat ( mid (X))))
50+ elseif 0 ∈ f (wideinterval ( mid (X))) # Root guaranteed to be in X
5151 n = fa = fb = 0
5252 root_exist = true
53- while (n < 4 && (fa == 0 || fb == 0 ))
53+ while (n < 4 && (fa == 0 || fb == 0 )) # Narrowing the interval further
5454 if fa == 0
55- if 0 ∈ f (interval ( prevfloat ( X. lo), nextfloat (X . lo) ))
55+ if 0 ∈ f (wideinterval ( X. lo))
5656 fa = 1
5757 else
5858 N = X. lo - (f (interval (X. lo)) / f′ (X))
5959 X = X ∩ N
60- if (isempty (X) || diam (X) == 0 )
60+ if (isempty (X))
6161 root_exist = false
6262 break
6363 end
6464 end
6565 end
6666 if fb == 0
67- if 0 ∈ f (interval ( prevfloat ( X. hi), nextfloat (X . hi) ))
67+ if 0 ∈ f (wideinterval ( X. hi))
6868 fb = 1
6969 else
70- if 0 ∈ f (interval ( prevfloat ( mid (X)), nextfloat ( mid (X) )))
70+ if 0 ∈ f (wideinterval ( mid (X)))
7171 N = X. hi - (f (interval (X. hi)) / f′ (X))
7272 else
7373 N = mid (X) - (f (interval (mid (X))) / f′ (X))
7474 end
7575 X = X ∩ N
76- if (isempty (X) || diam (X) == 0 )
76+ if (isempty (X))
7777 root_exist = false
7878 break
7979 end
8080 end
8181 end
8282 N = mid (X) - (f (interval (mid (X))) / f′ (X))
8383 X = X ∩ N
84- if (isempty (X) || diam (X) == 0 )
84+ if (isempty (X))
8585 root_exist = false
8686 break
8787 end
8888 n += 1
8989 end
9090 if root_exist
9191 push! (R, Root (X, :unique ))
92- debugroot && @show " Root found" , X
92+ debugroot && @show " Root found" , X # Storing determined unique root
9393 end
9494
9595 break
9696 end
9797 end
9898
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
101101 reps += 1
102102 if reps > 10
103103 push! (R, Root (X, :unknown ))
104- debugroot && @show " Repititive root found" , X
104+ debugroot && @show " Repeated root found" , X
105105 reps = 0
106106 continue
107107 end
@@ -112,21 +112,21 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
112112 expansion_pt = Inf
113113 # expansion point for the newton step might be m, X.lo or X.hi according to some conditions
114114
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
116116 # 0 ∈ fⁱ(x)
117117
118118 debug && println (" 0 ∈ fⁱ(x)" )
119119
120- if 0 ∉ f (interval ( prevfloat ( X. lo), nextfloat (X . lo) ))
120+ if 0 ∉ f (wideinterval ( X. lo))
121121 expansion_pt = X. lo
122122
123- elseif 0 ∉ f (interval ( prevfloat ( X. hi), nextfloat (X . hi) ))
123+ elseif 0 ∉ f (wideinterval ( X. hi))
124124 expansion_pt = X. hi
125125
126126 else
127127 x1 = mid (interval (X. lo, mid (X)))
128128 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 ))
130130 push! (L, interval (X. lo, m))
131131 push! (L, interval (m, X. hi))
132132 continue
@@ -145,7 +145,7 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
145145
146146 debug && println (" 0 ∉ fⁱ(x)" )
147147
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
149149 push! (R, Root (X, :unknown ))
150150
151151 debugroot && @show " Tolerance root found" , X
@@ -189,7 +189,7 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
189189 end
190190 end
191191
192- push! (L, X)
192+ push! (L, X) # Pushing X into L to be processed again
193193 end
194194 end
195195
0 commit comments