Skip to content

Commit 49d970b

Browse files
committed
Add slope interval newton method
1 parent 1412b1f commit 49d970b

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/IntervalRootFinding.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export
1818
derivative, jacobian, # reexport derivative from ForwardDiff
1919
Root, is_unique,
2020
roots, find_roots,
21-
bisect, newton1d, slope
21+
bisect, newton1d, slope,
22+
slope_newton1d
2223

2324
export isunique, root_status
2425

src/newton1d.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
2525
continue
2626
end
2727

28-
if 0 f′(X)
28+
if 0 ForwardDiff.derivative(f, X)
2929

3030
debug && println("0 ∉ f′(X)")
3131

@@ -161,7 +161,7 @@ function newton1d{T}(f::Function, f′::Function, x::Interval{T};
161161

162162

163163
a = f(interval(expansion_pt))
164-
b = f′(X)
164+
b = ForwardDiff.derivative(f, X)
165165

166166
if 0 < b.hi && 0 > b.lo && 0 a
167167
if a.hi < 0
@@ -204,3 +204,8 @@ and a `debug` boolean argument that prints out diagnostic information."""
204204

205205
newton1d{T}(f::Function, x::Interval{T}; args...) =
206206
newton1d(f, x->D(f,x), x; args...)
207+
208+
function slope_newton1d{T}(f::Function, x::Interval{T};
209+
reltol=eps(T), abstol=eps(T), debug=false, debugroot=false)
210+
newton1d(f, x->slope(f, x), x, reltol=reltol, abstol=abstol, debug=debug, debugroot=debugroot)
211+
end

test/newton1d.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,55 @@ three_halves_pi = 3*big_pi/2
8484

8585
end
8686
end
87+
88+
@testset "Testing slope newton1d" begin
89+
90+
f(x) = exp(x^2) - cos(x) #double root
91+
f1(x) = x^4 - 10x^3 + 35x^2 - 50x + 24 #four unique roots
92+
f2(x) = 4567x^2 - 9134x + 4567 #double root
93+
f3(x) = (x^2 - 2)^2 #two double roots
94+
f4(x) = sin(x) - x #triple root at 0
95+
f5(x) = (x^2 - 1)^4 * (x^2 - 2)^4 #two quadruple roots
96+
97+
rts1 = newton1d(sin, -5..5)
98+
rts2 = newton1d(f, -..∞)
99+
rts3 = newton1d(f1, -10..10)
100+
rts4 = newton1d(f2, -10..11)
101+
rts5 = newton1d(f3, -10..10)
102+
rts6 = newton1d(f4, -10..10)
103+
rts7 = newton1d(f5, -10..10)
104+
105+
@test length(rts1) == 3
106+
L = [ -pi_interval(Float64), 0..0, pi_interval(Float64)]
107+
for i = 1:length(rts1)
108+
@test L[i] in rts1[i].interval && :unique == rts1[i].status
109+
end
110+
111+
@test length(rts2) == 1
112+
@test (0..0) == rts2[1].interval && :unknown == rts2[1].status
113+
114+
@test length(rts3) == 4
115+
L = [1, 2, 3, 4]
116+
for i = 1:length(rts3)
117+
@test L[i] in rts3[i].interval && :unique == rts3[i].status
118+
end
119+
120+
@test length(rts4) == 1
121+
@test 1 in rts4[1].interval && :unknown == rts4[1].status
122+
123+
L1 = [-sqrt(2), sqrt(2)]
124+
for i = 1:length(rts5)
125+
@test L1[i] in rts5[i].interval && :unknown == rts5[i].status
126+
end end
127+
128+
129+
@test length(rts6) == 1
130+
@test 0 in rts6[1].interval && :unknown == rts6[1].status
131+
132+
@test length(rts7) == 4
133+
L = [-sqrt(2), -1, 1, sqrt(2)]
134+
for i = 1:length(rts7)
135+
@test L[i] in rts7[i].interval && :unknown == rts7[i].status
136+
end
137+
138+
end

0 commit comments

Comments
 (0)