@@ -3,6 +3,7 @@ using StaticArrays
33using BenchmarkTools
44using Test
55
6+ # SimpleNewtonRaphson
67function benchmark_scalar (f, u0)
78 probN = NonlinearProblem {false} (f, u0)
89 sol = (solve (probN, SimpleNewtonRaphson ()))
@@ -23,38 +24,49 @@ sol = benchmark_scalar(sf, csu0)
2324
2425@test (@ballocated benchmark_scalar (sf, csu0)) == 0
2526
27+ # Broyden
28+ function benchmark_scalar (f, u0)
29+ probN = NonlinearProblem {false} (f, u0)
30+ sol = (solve (probN, Broyden ()))
31+ end
32+
33+ sol = benchmark_scalar (sf, csu0)
34+ @test sol. retcode === ReturnCode. Success
35+ @test sol. u * sol. u - 2 < 1e-9
36+ @test (@ballocated benchmark_scalar (sf, csu0)) == 0
37+
2638# AD Tests
2739using ForwardDiff
2840
2941# Immutable
3042f, u0 = (u, p) -> u .* u .- p, @SVector [1.0 , 1.0 ]
3143
32- g = function (p)
33- probN = NonlinearProblem {false} (f, csu0, p)
34- sol = solve (probN, SimpleNewtonRaphson (), tol = 1e-9 )
35- return sol. u[end ]
36- end
44+ for alg in [SimpleNewtonRaphson (), Broyden ()]
45+ g = function (p)
46+ probN = NonlinearProblem {false} (f, csu0, p)
47+ sol = solve (probN, alg, tol = 1e-9 )
48+ return sol. u[end ]
49+ end
3750
38- for p in 1.0 : 0.1 : 100.0
39- @test g (p) ≈ sqrt (p)
40- @test ForwardDiff. derivative (g, p) ≈ 1 / (2 * sqrt (p))
51+ for p in 1.1 : 0.1 : 100.0
52+ @test g (p) ≈ sqrt (p)
53+ @test ForwardDiff. derivative (g, p) ≈ 1 / (2 * sqrt (p))
54+ end
4155end
4256
4357# Scalar
4458f, u0 = (u, p) -> u * u - p, 1.0
59+ for alg in [SimpleNewtonRaphson (), Broyden ()]
60+ g = function (p)
61+ probN = NonlinearProblem {false} (f, oftype (p, u0), p)
62+ sol = solve (probN, alg)
63+ return sol. u
64+ end
4565
46- # SimpleNewtonRaphson
47- g = function (p)
48- probN = NonlinearProblem {false} (f, oftype (p, u0), p)
49- sol = solve (probN, SimpleNewtonRaphson ())
50- return sol. u
51- end
52-
53- @test ForwardDiff. derivative (g, 1.0 ) ≈ 0.5
54-
55- for p in 1.1 : 0.1 : 100.0
56- @test g (p) ≈ sqrt (p)
57- @test ForwardDiff. derivative (g, p) ≈ 1 / (2 * sqrt (p))
66+ for p in 1.1 : 0.1 : 100.0
67+ @test g (p) ≈ sqrt (p)
68+ @test ForwardDiff. derivative (g, p) ≈ 1 / (2 * sqrt (p))
69+ end
5870end
5971
6072tspan = (1.0 , 20.0 )
@@ -77,24 +89,26 @@ for alg in [Bisection(), Falsi()]
7789 global g, p
7890 g = function (p)
7991 probN = IntervalNonlinearProblem {false} (f, tspan, p)
80- sol = solve (probN, Bisection () )
92+ sol = solve (probN, alg )
8193 return [sol. left]
8294 end
8395
8496 @test g (p) ≈ [sqrt (p[2 ] / p[1 ])]
8597 @test ForwardDiff. jacobian (g, p) ≈ ForwardDiff. jacobian (t, p)
8698end
8799
88- gnewton = function (p)
89- probN = NonlinearProblem {false} (f, 0.5 , p)
90- sol = solve (probN, SimpleNewtonRaphson ())
91- return [sol. u]
100+ for alg in [SimpleNewtonRaphson (), Broyden ()]
101+ global g, p
102+ g = function (p)
103+ probN = NonlinearProblem {false} (f, 0.5 , p)
104+ sol = solve (probN, alg)
105+ return [sol. u]
106+ end
107+ @test g (p) ≈ [sqrt (p[2 ] / p[1 ])]
108+ @test ForwardDiff. jacobian (g, p) ≈ ForwardDiff. jacobian (t, p)
92109end
93- @test gnewton (p) ≈ [sqrt (p[2 ] / p[1 ])]
94- @test ForwardDiff. jacobian (gnewton, p) ≈ ForwardDiff. jacobian (t, p)
95110
96111# Error Checks
97-
98112f, u0 = (u, p) -> u .* u .- 2.0 , @SVector [1.0 , 1.0 ]
99113probN = NonlinearProblem (f, u0)
100114
@@ -103,6 +117,9 @@ probN = NonlinearProblem(f, u0)
103117@test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u[end ] ≈ sqrt (2.0 )
104118@test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u[end ] ≈ sqrt (2.0 )
105119
120+ @test solve (probN, Broyden ()). u[end ] ≈ sqrt (2.0 )
121+ @test solve (probN, Broyden (); immutable = false ). u[end ] ≈ sqrt (2.0 )
122+
106123for u0 in [1.0 , [1 , 1.0 ]]
107124 local f, probN, sol
108125 f = (u, p) -> u .* u .- 2.0
@@ -112,6 +129,8 @@ for u0 in [1.0, [1, 1.0]]
112129 @test solve (probN, SimpleNewtonRaphson ()). u ≈ sol
113130 @test solve (probN, SimpleNewtonRaphson ()). u ≈ sol
114131 @test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u ≈ sol
132+
133+ @test solve (probN, Broyden ()). u ≈ sol
115134end
116135
117136# Bisection Tests
0 commit comments