Skip to content

Commit 6257f20

Browse files
committed
Add debug mode and bug fixes
1 parent 8500a99 commit 6257f20

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/newton1d.jl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Optional keyword arguments give the tolerances `reltol` and `abstol`.
55
and a `debug` boolean argument that prints out diagnostic information."""
66

77
function newton1d{T}(f::Function, f′::Function, x::Interval{T};
8-
reltol=eps(T), abstol=eps(T), debug=false)
8+
reltol=10eps(T), abstol=eps(T), debug=false)
99

1010
L = Interval{T}[]
1111

@@ -15,34 +15,50 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
1515

1616
while !isempty(L)
1717
X = pop!(L)
18+
19+
debug && (print("Current interval popped: "); @show X)
20+
1821
m = mid(X)
1922
if (isempty(X))
2023
continue
2124
end
2225

2326
if 0 f′(X)
27+
28+
debug && println("0 ∉ f′(X)")
29+
2430
while true
2531
m = mid(X)
2632
N = m - (f(Interval(m)) / f′(X))
33+
34+
debug && (print("Newton step: "); @show (X, X N))
35+
2736
X = X N
2837

2938
if isempty(X)
3039
break
3140

3241
elseif 0 f(Interval(prevfloat(m), nextfloat(m)))
3342
push!(R, Root(X, :unique))
43+
44+
debug && @show "Root found", X
45+
3446
break
3547
end
3648
end
3749

3850
else
3951
# 0 ∈ f'(X)
52+
53+
debug && println("0 ∈ f'(X)")
54+
4055
expansion_pt = Inf
4156
# expansion point for the newton step might be m, X.lo or X.hi according to some conditions
4257

4358
if 0 f(Interval(mid(X)))
4459
# 0 ∈ fⁱ(x)
45-
# Step 7
60+
61+
debug && println("0 ∈ fⁱ(x)")
4662

4763
if 0 f(Interval(X.lo))
4864
expansion_pt = X.lo
@@ -59,16 +75,24 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
5975
continue
6076

6177
else
62-
push!(R, Root(X, :unique))
78+
push!(R, Root(X, :unknown))
79+
80+
debug && @show "Multiple root found", X
81+
6382
continue
6483
end
6584
end
6685

6786
else
6887
# 0 ∉ fⁱ(x)
6988

89+
debug && println("0 ∉ fⁱ(x)")
90+
7091
if (diam(X)/mag(X)) < reltol && diam(f(X)) < abstol
7192
push!(R, Root(X, :unknown))
93+
94+
debug && @show "Tolerance root found", X
95+
7296
continue
7397
end
7498
end
@@ -98,6 +122,9 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
98122

99123
else
100124
N = expansion_pt - (f(Interval(expansion_pt))/f′(X))
125+
126+
debug && (print("Newton step: "); @show (X, X N))
127+
101128
X = X N
102129
m = mid(X)
103130

0 commit comments

Comments
 (0)