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.jupiter.api.Test;
31
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.junit.jupiter.api.Assertions.fail;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 final class MullerSolver2Test {
48
49
50
51 @Test
52 void testSinFunction() {
53 UnivariateFunction f = new Sin();
54 UnivariateSolver solver = new MullerSolver2();
55 double min, max, expected, result, tolerance;
56
57 min = 3.0; max = 4.0; expected = FastMath.PI;
58 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
59 FastMath.abs(expected * solver.getRelativeAccuracy()));
60 result = solver.solve(100, f, min, max);
61 assertEquals(expected, result, tolerance);
62
63 min = -1.0; max = 1.5; expected = 0.0;
64 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
65 FastMath.abs(expected * solver.getRelativeAccuracy()));
66 result = solver.solve(100, f, min, max);
67 assertEquals(expected, result, tolerance);
68 }
69
70
71
72
73 @Test
74 void testQuinticFunction() {
75 UnivariateFunction f = new QuinticFunction();
76 UnivariateSolver solver = new MullerSolver2();
77 double min, max, expected, result, tolerance;
78
79 min = -0.4; max = 0.2; expected = 0.0;
80 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
81 FastMath.abs(expected * solver.getRelativeAccuracy()));
82 result = solver.solve(100, f, min, max);
83 assertEquals(expected, result, tolerance);
84
85 min = 0.75; max = 1.5; expected = 1.0;
86 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
87 FastMath.abs(expected * solver.getRelativeAccuracy()));
88 result = solver.solve(100, f, min, max);
89 assertEquals(expected, result, tolerance);
90
91 min = -0.9; max = -0.2; expected = -0.5;
92 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
93 FastMath.abs(expected * solver.getRelativeAccuracy()));
94 result = solver.solve(100, f, min, max);
95 assertEquals(expected, result, tolerance);
96 }
97
98
99
100
101
102
103 @Test
104 void testExpm1Function() {
105 UnivariateFunction f = new Expm1();
106 UnivariateSolver solver = new MullerSolver2();
107 double min, max, expected, result, tolerance;
108
109 min = -1.0; max = 2.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 assertEquals(expected, result, tolerance);
114
115 min = -20.0; max = 10.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 assertEquals(expected, result, tolerance);
120
121 min = -50.0; max = 100.0; expected = 0.0;
122 tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
123 FastMath.abs(expected * solver.getRelativeAccuracy()));
124 result = solver.solve(100, f, min, max);
125 assertEquals(expected, result, tolerance);
126 }
127
128
129
130
131 @Test
132 void testParameters() {
133 UnivariateFunction f = new Sin();
134 UnivariateSolver solver = new MullerSolver2();
135
136 try {
137
138 solver.solve(100, f, 1, -1);
139 fail("Expecting MathIllegalArgumentException - bad interval");
140 } catch (MathIllegalArgumentException ex) {
141
142 }
143 try {
144
145 solver.solve(100, f, 2, 3);
146 fail("Expecting MathIllegalArgumentException - no bracketing");
147 } catch (MathIllegalArgumentException ex) {
148
149 }
150 }
151 }