Skip to content

Commit 288cd6d

Browse files
committed
Add slope interval newton method
1 parent 64f7339 commit 288cd6d

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/IntervalRootFinding.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export
2222
gauss_seidel_interval, gauss_seidel_interval!,
2323
gauss_seidel_contractor, gauss_seidel_contractor!,
2424
gauss_elimination_interval, gauss_elimination_interval!,
25-
slope
25+
slope, slope_newton1d
2626

2727
export isunique, root_status
2828

src/newton1d.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = slope_newton1d(sin, -5..5)
98+
rts2 = slope_newton1d(f, -..∞)
99+
rts3 = slope_newton1d(f1, -10..10)
100+
rts4 = slope_newton1d(f2, -10..11)
101+
rts5 = slope_newton1d(f3, -10..10)
102+
rts6 = slope_newton1d(f4, -10..10)
103+
rts7 = slope_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
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)