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