-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrootfinder_test.go
89 lines (81 loc) · 2.14 KB
/
rootfinder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package rootfinder
import (
"math"
"testing"
)
var testCases = []struct {
precision int
maxIteration int
root float64
function Function
derivative Function
a float64
b float64
guess float64
x0 float64
x1 float64
}{
{
precision: 6,
maxIteration: 100,
root: 0.739085,
function: func(x float64) float64 { return x - math.Cos(x) },
derivative: func(x float64) float64 { return 1 + math.Sin(x) },
a: 0.0,
b: 1.0,
guess: 0.5,
x0: 0,
x1: 1,
},
{
precision: 6,
maxIteration: 100,
root: 3.162278,
function: func(x float64) float64 { return x*x - 10 },
derivative: func(x float64) float64 { return 2 * x },
a: 3,
b: 4,
guess: 3,
x0: 3,
x1: 4,
},
{
precision: 5,
maxIteration: 100,
root: 2.09455,
function: func(x float64) float64 { return x*x*x - 2*x - 5 },
derivative: func(x float64) float64 { return 3*x*x - 2 },
a: 2,
b: 3,
guess: 2,
x0: 2,
x1: 3,
},
}
func TestBisection(t *testing.T) {
for _, tcase := range testCases {
rf := New(tcase.precision, tcase.maxIteration, tcase.function, tcase.derivative)
b, i, _ := rf.Bisection(tcase.a, tcase.b)
if math.Abs(b-tcase.root) > math.Pow10(-tcase.precision) {
t.Errorf("expected: %v, got: %v , iter: %v", tcase.root, b, i)
}
}
}
func TestNewtonRaphson(t *testing.T) {
for _, tcase := range testCases {
rf := New(tcase.precision, tcase.maxIteration, tcase.function, tcase.derivative)
n, i, _ := rf.NewtonRaphson(tcase.guess)
if math.Abs(n-tcase.root) > math.Pow10(-tcase.precision) {
t.Errorf("expected: %v, got: %v , iter: %v", tcase.root, n, i)
}
}
}
func TestSecant(t *testing.T) {
for _, tcase := range testCases {
rf := New(tcase.precision, tcase.maxIteration, tcase.function, tcase.derivative)
s, i, _ := rf.Secant(tcase.x0, tcase.x1)
if math.Abs(s-tcase.root) > math.Pow10(-tcase.precision) {
t.Errorf("expected: %v, got: %v , iter: %v", tcase.root, s, i)
}
}
}