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.  *      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. import org.apache.commons.math4.legacy.exception.NoBracketingException;
  19. import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
  20. import org.apache.commons.math4.core.jdkmath.JdkMath;

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

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

  42.     /** Construct a solver with default accuracy (1e-6). */
  43.     public SecantSolver() {
  44.         super(DEFAULT_ABSOLUTE_ACCURACY);
  45.     }

  46.     /**
  47.      * Construct a solver.
  48.      *
  49.      * @param absoluteAccuracy absolute accuracy
  50.      */
  51.     public SecantSolver(final double absoluteAccuracy) {
  52.         super(absoluteAccuracy);
  53.     }

  54.     /**
  55.      * Construct a solver.
  56.      *
  57.      * @param relativeAccuracy relative accuracy
  58.      * @param absoluteAccuracy absolute accuracy
  59.      */
  60.     public SecantSolver(final double relativeAccuracy,
  61.                         final double absoluteAccuracy) {
  62.         super(relativeAccuracy, absoluteAccuracy);
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     protected final double doSolve()
  67.         throws TooManyEvaluationsException,
  68.                NoBracketingException {
  69.         // Get initial solution
  70.         double x0 = getMin();
  71.         double x1 = getMax();
  72.         double f0 = computeObjectiveValue(x0);
  73.         double f1 = computeObjectiveValue(x1);

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

  83.         // Verify bracketing of initial solution.
  84.         verifyBracketing(x0, x1);

  85.         // Get accuracies.
  86.         final double ftol = getFunctionValueAccuracy();
  87.         final double atol = getAbsoluteAccuracy();
  88.         final double rtol = getRelativeAccuracy();

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

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

  100.             // Update the bounds with the new approximation.
  101.             x0 = x1;
  102.             f0 = f1;
  103.             x1 = x;
  104.             f1 = fx;

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

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