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.solvers;
18  
19  import org.apache.commons.math.MathException;
20  import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
21  import org.apache.commons.math.analysis.QuinticFunction;
22  import org.apache.commons.math.analysis.SinFunction;
23  
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
29   */
30  public final class NewtonSolverTest extends TestCase {
31  
32      @Deprecated
33      public void testDeprecated() throws MathException {
34          DifferentiableUnivariateRealFunction f = new SinFunction();
35          double result;
36          
37          UnivariateRealSolver solver = new NewtonSolver(f);
38          result = solver.solve(3, 4);
39          assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
40  
41          result = solver.solve(1, 4);
42          assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
43          
44          assertEquals(result, solver.getResult(), 0);
45          assertTrue(solver.getIterationCount() > 0);
46      }
47  
48      /**
49      *
50      */
51     public void testSinZero() throws MathException {
52         DifferentiableUnivariateRealFunction f = new SinFunction();
53         double result;
54         
55         UnivariateRealSolver solver = new NewtonSolver();
56         result = solver.solve(f, 3, 4);
57         assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
58  
59         result = solver.solve(f, 1, 4);
60         assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
61         
62         assertEquals(result, solver.getResult(), 0);
63         assertTrue(solver.getIterationCount() > 0);
64     }
65  
66     /**
67       *
68       */
69      public void testQuinticZero() throws MathException {
70          DifferentiableUnivariateRealFunction f = new QuinticFunction();
71          double result;
72  
73          UnivariateRealSolver solver = new NewtonSolver();
74          result = solver.solve(f, -0.2, 0.2);
75          assertEquals(result, 0, solver.getAbsoluteAccuracy());
76  
77          result = solver.solve(f, -0.1, 0.3);
78          assertEquals(result, 0, solver.getAbsoluteAccuracy());
79  
80          result = solver.solve(f, -0.3, 0.45);
81          assertEquals(result, 0, solver.getAbsoluteAccuracy());
82  
83          result = solver.solve(f, 0.3, 0.7);
84          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
85  
86          result = solver.solve(f, 0.2, 0.6);
87          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
88  
89          result = solver.solve(f, 0.05, 0.95);
90          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
91  
92          result = solver.solve(f, 0.85, 1.25);
93          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
94  
95          result = solver.solve(f, 0.8, 1.2);
96          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
97  
98          result = solver.solve(f, 0.85, 1.75);
99          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
100 
101         result = solver.solve(f, 0.55, 1.45);
102         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
103 
104         result = solver.solve(f, 0.85, 5);
105         assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
106     }
107     
108 }