1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.analysis;
18  
19  import org.apache.commons.math.MathException;
20  import junit.framework.TestCase;
21  
22  /**
23   * Testcase for Ridders solver.
24   * <p>
25   * Ridders' method converges superlinearly, more specific, its rate of
26   * convergence is sqrt(2). Test runs show that for a default absolute
27   * accuracy of 1E-6, it generally takes less than 5 iterations for close
28   * initial bracket and 5 to 10 iterations for distant initial bracket
29   * to converge.
30   * 
31   * @version $Revision$ $Date$ 
32   */
33  public final class RiddersSolverTest extends TestCase {
34  
35      /**
36       * Test of solver for the sine function.
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       * Test of solver for the quintic function.
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       * Test of solver for the exponential function.
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      * Test of parameters for the solver.
112      */
113     public void testParameters() throws Exception {
114         UnivariateRealFunction f = new SinFunction();
115         UnivariateRealSolver solver = new RiddersSolver(f);
116 
117         try {
118             // bad interval
119             solver.solve(1, -1);
120             fail("Expecting IllegalArgumentException - bad interval");
121         } catch (IllegalArgumentException ex) {
122             // expected
123         }
124         try {
125             // no bracketing
126             solver.solve(2, 3);
127             fail("Expecting IllegalArgumentException - no bracketing");
128         } catch (IllegalArgumentException ex) {
129             // expected
130         }
131     }
132 }