NewtonRaphsonSolver.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.analysis.differentiation.DerivativeStructure;
  23. import org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction;
  24. import org.hipparchus.exception.MathIllegalStateException;
  25. import org.hipparchus.util.FastMath;

  26. /**
  27.  * Implements <a href="http://mathworld.wolfram.com/NewtonsMethod.html">
  28.  * Newton's Method</a> for finding zeros of real univariate differentiable
  29.  * functions.
  30.  *
  31.  */
  32. public class NewtonRaphsonSolver extends AbstractUnivariateDifferentiableSolver {
  33.     /** Default absolute accuracy. */
  34.     private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6;

  35.     /**
  36.      * Construct a solver.
  37.      */
  38.     public NewtonRaphsonSolver() {
  39.         this(DEFAULT_ABSOLUTE_ACCURACY);
  40.     }
  41.     /**
  42.      * Construct a solver.
  43.      *
  44.      * @param absoluteAccuracy Absolute accuracy.
  45.      */
  46.     public NewtonRaphsonSolver(double absoluteAccuracy) {
  47.         super(absoluteAccuracy);
  48.     }

  49.     /**
  50.      * Find a zero near the midpoint of {@code min} and {@code max}.
  51.      *
  52.      * @param f Function to solve.
  53.      * @param min Lower bound for the interval.
  54.      * @param max Upper bound for the interval.
  55.      * @param maxEval Maximum number of evaluations.
  56.      * @return the value where the function is zero.
  57.      * @throws org.hipparchus.exception.MathIllegalStateException
  58.      * if the maximum evaluation count is exceeded.
  59.      * @throws org.hipparchus.exception.MathIllegalArgumentException
  60.      * if {@code min >= max}.
  61.      */
  62.     @Override
  63.     public double solve(int maxEval, final UnivariateDifferentiableFunction f,
  64.                         final double min, final double max)
  65.         throws MathIllegalStateException {
  66.         return super.solve(maxEval, f, UnivariateSolverUtils.midpoint(min, max));
  67.     }

  68.     /**
  69.      * {@inheritDoc}
  70.      */
  71.     @Override
  72.     protected double doSolve()
  73.         throws MathIllegalStateException {
  74.         final double startValue = getStartValue();
  75.         final double absoluteAccuracy = getAbsoluteAccuracy();

  76.         double x0 = startValue;
  77.         double x1;
  78.         while (true) {
  79.             final DerivativeStructure y0 = computeObjectiveValueAndDerivative(x0);
  80.             x1 = x0 - (y0.getValue() / y0.getPartialDerivative(1));
  81.             if (FastMath.abs(x1 - x0) <= absoluteAccuracy) {
  82.                 return x1;
  83.             }

  84.             x0 = x1;
  85.         }
  86.     }
  87. }