SecantSolver.java

  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.  *      https://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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.analysis.solvers;

  22. import org.hipparchus.exception.MathIllegalArgumentException;
  23. import org.hipparchus.exception.MathIllegalStateException;
  24. import org.hipparchus.util.FastMath;

  25. /**
  26.  * Implements the <em>Secant</em> method for root-finding (approximating a
  27.  * zero of a univariate real function). The solution that is maintained is
  28.  * not bracketed, and as such convergence is not guaranteed.
  29.  *
  30.  * <p>Implementation based on the following article: M. Dowell and P. Jarratt,
  31.  * <em>A modified regula falsi method for computing the root of an
  32.  * equation</em>, BIT Numerical Mathematics, volume 11, number 2,
  33.  * pages 168-174, Springer, 1971.</p>
  34.  *
  35.  * <p>Note that since release 3.0 this class implements the actual
  36.  * <em>Secant</em> algorithm, and not a modified one. As such, the 3.0 version
  37.  * is not backwards compatible with previous versions. To use an algorithm
  38.  * similar to the pre-3.0 releases, use the
  39.  * {@link IllinoisSolver <em>Illinois</em>} algorithm or the
  40.  * {@link PegasusSolver <em>Pegasus</em>} algorithm.</p>
  41.  *
  42.  */
  43. public class SecantSolver extends AbstractUnivariateSolver {

  44.     /** Default absolute accuracy. */
  45.     protected static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6;

  46.     /** Construct a solver with default accuracy (1e-6). */
  47.     public SecantSolver() {
  48.         super(DEFAULT_ABSOLUTE_ACCURACY);
  49.     }

  50.     /**
  51.      * Construct a solver.
  52.      *
  53.      * @param absoluteAccuracy absolute accuracy
  54.      */
  55.     public SecantSolver(final double absoluteAccuracy) {
  56.         super(absoluteAccuracy);
  57.     }

  58.     /**
  59.      * Construct a solver.
  60.      *
  61.      * @param relativeAccuracy relative accuracy
  62.      * @param absoluteAccuracy absolute accuracy
  63.      */
  64.     public SecantSolver(final double relativeAccuracy,
  65.                         final double absoluteAccuracy) {
  66.         super(relativeAccuracy, absoluteAccuracy);
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     protected final double doSolve()
  71.         throws MathIllegalArgumentException, MathIllegalStateException {
  72.         // Get initial solution
  73.         double x0 = getMin();
  74.         double x1 = getMax();
  75.         double f0 = computeObjectiveValue(x0);
  76.         double f1 = computeObjectiveValue(x1);

  77.         // If one of the bounds is the exact root, return it. Since these are
  78.         // not under-approximations or over-approximations, we can return them
  79.         // regardless of the allowed solutions.
  80.         if (f0 == 0.0) {
  81.             return x0;
  82.         }
  83.         if (f1 == 0.0) {
  84.             return x1;
  85.         }

  86.         // Verify bracketing of initial solution.
  87.         verifyBracketing(x0, x1);

  88.         // Get accuracies.
  89.         final double ftol = getFunctionValueAccuracy();
  90.         final double atol = getAbsoluteAccuracy();
  91.         final double rtol = getRelativeAccuracy();

  92.         // Keep finding better approximations.
  93.         while (true) {
  94.             // Calculate the next approximation.
  95.             final double x = x1 - ((f1 * (x1 - x0)) / (f1 - f0));
  96.             final double fx = computeObjectiveValue(x);

  97.             // If the new approximation is the exact root, return it. Since
  98.             // this is not an under-approximation or an over-approximation,
  99.             // we can return it regardless of the allowed solutions.
  100.             if (fx == 0.0) {
  101.                 return x;
  102.             }

  103.             // Update the bounds with the new approximation.
  104.             x0 = x1;
  105.             f0 = f1;
  106.             x1 = x;
  107.             f1 = fx;

  108.             // If the function value of the last approximation is too small,
  109.             // given the function value accuracy, then we can't get closer to
  110.             // the root than we already are.
  111.             if (FastMath.abs(f1) <= ftol) {
  112.                 return x1;
  113.             }

  114.             // If the current interval is within the given accuracies, we
  115.             // are satisfied with the current approximation.
  116.             if (FastMath.abs(x1 - x0) < FastMath.max(rtol * FastMath.abs(x1), atol)) {
  117.                 return x1;
  118.             }
  119.         }
  120.     }

  121. }