1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.solvers;
23
24 import org.hipparchus.analysis.QuinticFunction;
25 import org.hipparchus.analysis.UnivariateFunction;
26 import org.hipparchus.analysis.function.Expm1;
27 import org.hipparchus.analysis.function.Sin;
28 import org.hipparchus.exception.MathIllegalArgumentException;
29 import org.hipparchus.util.FastMath;
30 import org.junit.Assert;
31 import org.junit.Test;
32
33
34
35
36
37
38
39
40
41
42
43 public final class RiddersSolverTest {
44
45
46
47 @Test
48 public void testSinFunction() {
49 UnivariateFunction f = new Sin();
50 UnivariateSolver solver = new RiddersSolver();
51 double min, max, expected, result, tolerance;
52
53 min = 3.0; max = 4.0; expected = FastMath.PI;
54 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
55 FastMath.abs(expected * solver.getRelativeAccuracy()));
56 result = solver.solve(100, f, min, max);
57 Assert.assertEquals(expected, result, tolerance);
58
59 min = -1.0; max = 1.5; expected = 0.0;
60 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
61 FastMath.abs(expected * solver.getRelativeAccuracy()));
62 result = solver.solve(100, f, min, max);
63 Assert.assertEquals(expected, result, tolerance);
64 }
65
66
67
68
69 @Test
70 public void testQuinticFunction() {
71 UnivariateFunction f = new QuinticFunction();
72 UnivariateSolver solver = new RiddersSolver();
73 double min, max, expected, result, tolerance;
74
75 min = -0.4; max = 0.2; expected = 0.0;
76 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
77 FastMath.abs(expected * solver.getRelativeAccuracy()));
78 result = solver.solve(100, f, min, max);
79 Assert.assertEquals(expected, result, tolerance);
80
81 min = 0.75; max = 1.5; expected = 1.0;
82 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
83 FastMath.abs(expected * solver.getRelativeAccuracy()));
84 result = solver.solve(100, f, min, max);
85 Assert.assertEquals(expected, result, tolerance);
86
87 min = -0.9; max = -0.2; expected = -0.5;
88 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
89 FastMath.abs(expected * solver.getRelativeAccuracy()));
90 result = solver.solve(100, f, min, max);
91 Assert.assertEquals(expected, result, tolerance);
92 }
93
94
95
96
97 @Test
98 public void testExpm1Function() {
99 UnivariateFunction f = new Expm1();
100 UnivariateSolver solver = new RiddersSolver();
101 double min, max, expected, result, tolerance;
102
103 min = -1.0; max = 2.0; expected = 0.0;
104 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
105 FastMath.abs(expected * solver.getRelativeAccuracy()));
106 result = solver.solve(100, f, min, max);
107 Assert.assertEquals(expected, result, tolerance);
108
109 min = -20.0; max = 10.0; expected = 0.0;
110 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
111 FastMath.abs(expected * solver.getRelativeAccuracy()));
112 result = solver.solve(100, f, min, max);
113 Assert.assertEquals(expected, result, tolerance);
114
115 min = -50.0; max = 100.0; expected = 0.0;
116 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
117 FastMath.abs(expected * solver.getRelativeAccuracy()));
118 result = solver.solve(100, f, min, max);
119 Assert.assertEquals(expected, result, tolerance);
120 }
121
122
123
124
125 @Test
126 public void testParameters() {
127 UnivariateFunction f = new Sin();
128 UnivariateSolver solver = new RiddersSolver();
129
130 try {
131
132 solver.solve(100, f, 1, -1);
133 Assert.fail("Expecting MathIllegalArgumentException - bad interval");
134 } catch (MathIllegalArgumentException ex) {
135
136 }
137 try {
138
139 solver.solve(100, f, 2, 3);
140 Assert.fail("Expecting MathIllegalArgumentException - no bracketing");
141 } catch (MathIllegalArgumentException ex) {
142
143 }
144 }
145 }