View Javadoc
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.math4.legacy.analysis.solvers;
18  
19  import org.apache.commons.math4.legacy.analysis.QuinticFunction;
20  import org.apache.commons.math4.legacy.analysis.differentiation.UnivariateDifferentiableFunction;
21  import org.apache.commons.math4.legacy.analysis.function.Sin;
22  import org.apache.commons.math4.core.jdkmath.JdkMath;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  
27  /**
28   */
29  public final class NewtonRaphsonSolverTest {
30      /**
31       *
32       */
33      @Test
34      public void testSinZero() {
35          UnivariateDifferentiableFunction f = new Sin();
36          double result;
37  
38          NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
39          result = solver.solve(100, f, 3, 4);
40          Assert.assertEquals(result, JdkMath.PI, solver.getAbsoluteAccuracy());
41  
42          result = solver.solve(100, f, 1, 4);
43          Assert.assertEquals(result, JdkMath.PI, solver.getAbsoluteAccuracy());
44  
45          Assert.assertTrue(solver.getEvaluations() > 0);
46      }
47  
48      /**
49       *
50       */
51      @Test
52      public void testQuinticZero() {
53          final UnivariateDifferentiableFunction f = new QuinticFunction();
54          double result;
55  
56          NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
57          result = solver.solve(100, f, -0.2, 0.2);
58          Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
59  
60          result = solver.solve(100, f, -0.1, 0.3);
61          Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
62  
63          result = solver.solve(100, f, -0.3, 0.45);
64          Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());
65  
66          result = solver.solve(100, f, 0.3, 0.7);
67          Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
68  
69          result = solver.solve(100, f, 0.2, 0.6);
70          Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
71  
72          result = solver.solve(100, f, 0.05, 0.95);
73          Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
74  
75          result = solver.solve(100, f, 0.85, 1.25);
76          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
77  
78          result = solver.solve(100, f, 0.8, 1.2);
79          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
80  
81          result = solver.solve(100, f, 0.85, 1.75);
82          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
83  
84          result = solver.solve(100, f, 0.55, 1.45);
85          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
86  
87          result = solver.solve(100, f, 0.85, 5);
88          Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
89      }
90  }