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