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  
18  package org.apache.commons.math4.legacy.analysis.solvers;
19  
20  import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
21  import org.apache.commons.math4.legacy.exception.ConvergenceException;
22  import org.apache.commons.math4.core.jdkmath.JdkMath;
23  import org.junit.Test;
24  import org.junit.Assert;
25  
26  /**
27   * Test case for {@link RegulaFalsiSolver Regula Falsi} solver.
28   *
29   */
30  public final class RegulaFalsiSolverTest extends BaseSecantSolverAbstractTest {
31      /** {@inheritDoc} */
32      @Override
33      protected UnivariateSolver getSolver() {
34          return new RegulaFalsiSolver();
35      }
36  
37      /** {@inheritDoc} */
38      @Override
39      protected int[] getQuinticEvalCounts() {
40          // While the Regula Falsi method guarantees convergence, convergence
41          // may be extremely slow. The last test case does not converge within
42          // even a million iterations. As such, it was disabled.
43          return new int[] {3, 7, 8, 19, 18, 11, 67, 55, 288, 151, -1};
44      }
45  
46      @Test(expected=ConvergenceException.class)
47      public void testIssue631() {
48          final UnivariateFunction f = new UnivariateFunction() {
49                  /** {@inheritDoc} */
50                  @Override
51                  public double value(double x) {
52                      return JdkMath.exp(x) - JdkMath.pow(Math.PI, 3.0);
53                  }
54              };
55  
56          final UnivariateSolver solver = new RegulaFalsiSolver();
57          final double root = solver.solve(3624, f, 1, 10);
58          Assert.assertEquals(3.4341896575482003, root, 1e-15);
59      }
60  }