Skip to content

Commit cfcb7da

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

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,43 @@ 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(f3, -10..10)
101+
rts5 = slope_newton1d(f4, -10..10)
102+
103+
@test length(rts1) == 3
104+
L = [ -pi_interval(Float64), 0..0, pi_interval(Float64)]
105+
for i = 1:length(rts1)
106+
@test L[i] in rts1[i].interval && :unique == rts1[i].status
107+
end
108+
109+
@test length(rts2) == 1
110+
@test (0..0) == rts2[1].interval && :unknown == rts2[1].status
111+
112+
@test length(rts3) == 4
113+
L = [1, 2, 3, 4]
114+
for i = 1:length(rts3)
115+
@test L[i] in rts3[i].interval && :unique == rts3[i].status
116+
end
117+
118+
L1 = [-sqrt(2), sqrt(2)]
119+
for i = 1:length(rts4)
120+
@test L1[i] in rts4[i].interval && :unknown == rts4[i].status
121+
end
122+
123+
@test length(rts5) == 1
124+
@test 0 in rts5[1].interval && :unknown == rts5[1].status
125+
126+
end

0 commit comments

Comments
 (0)