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

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

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

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

  65.     /**
  66.      * {@inheritDoc}
  67.      */
  68.     @Override
  69.     protected double doSolve()
  70.         throws TooManyEvaluationsException {
  71.         final double startValue = getStartValue();
  72.         final double absoluteAccuracy = getAbsoluteAccuracy();

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

  81.             x0 = x1;
  82.         }
  83.     }
  84. }