@@ -3,6 +3,7 @@ using StaticArrays
3
3
using BenchmarkTools
4
4
using Test
5
5
6
+ # SimpleNewtonRaphson
6
7
function benchmark_scalar (f, u0)
7
8
probN = NonlinearProblem {false} (f, u0)
8
9
sol = (solve (probN, SimpleNewtonRaphson ()))
@@ -23,38 +24,49 @@ sol = benchmark_scalar(sf, csu0)
23
24
24
25
@test (@ballocated benchmark_scalar (sf, csu0)) == 0
25
26
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
+
26
38
# AD Tests
27
39
using ForwardDiff
28
40
29
41
# Immutable
30
42
f, u0 = (u, p) -> u .* u .- p, @SVector [1.0 , 1.0 ]
31
43
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
37
50
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
41
55
end
42
56
43
57
# Scalar
44
58
f, 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
45
65
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
58
70
end
59
71
60
72
tspan = (1.0 , 20.0 )
@@ -77,24 +89,26 @@ for alg in [Bisection(), Falsi()]
77
89
global g, p
78
90
g = function (p)
79
91
probN = IntervalNonlinearProblem {false} (f, tspan, p)
80
- sol = solve (probN, Bisection () )
92
+ sol = solve (probN, alg )
81
93
return [sol. left]
82
94
end
83
95
84
96
@test g (p) ≈ [sqrt (p[2 ] / p[1 ])]
85
97
@test ForwardDiff. jacobian (g, p) ≈ ForwardDiff. jacobian (t, p)
86
98
end
87
99
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)
92
109
end
93
- @test gnewton (p) ≈ [sqrt (p[2 ] / p[1 ])]
94
- @test ForwardDiff. jacobian (gnewton, p) ≈ ForwardDiff. jacobian (t, p)
95
110
96
111
# Error Checks
97
-
98
112
f, u0 = (u, p) -> u .* u .- 2.0 , @SVector [1.0 , 1.0 ]
99
113
probN = NonlinearProblem (f, u0)
100
114
@@ -103,6 +117,9 @@ probN = NonlinearProblem(f, u0)
103
117
@test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u[end ] ≈ sqrt (2.0 )
104
118
@test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u[end ] ≈ sqrt (2.0 )
105
119
120
+ @test solve (probN, Broyden ()). u[end ] ≈ sqrt (2.0 )
121
+ @test solve (probN, Broyden (); immutable = false ). u[end ] ≈ sqrt (2.0 )
122
+
106
123
for u0 in [1.0 , [1 , 1.0 ]]
107
124
local f, probN, sol
108
125
f = (u, p) -> u .* u .- 2.0
@@ -112,6 +129,8 @@ for u0 in [1.0, [1, 1.0]]
112
129
@test solve (probN, SimpleNewtonRaphson ()). u ≈ sol
113
130
@test solve (probN, SimpleNewtonRaphson ()). u ≈ sol
114
131
@test solve (probN, SimpleNewtonRaphson (; autodiff = false )). u ≈ sol
132
+
133
+ @test solve (probN, Broyden ()). u ≈ sol
115
134
end
116
135
117
136
# Bisection Tests
0 commit comments