001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math3.analysis.solvers;
019
020 import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
021 import org.apache.commons.math3.util.FastMath;
022 import org.apache.commons.math3.exception.TooManyEvaluationsException;
023
024 /**
025 * Implements <a href="http://mathworld.wolfram.com/NewtonsMethod.html">
026 * Newton's Method</a> for finding zeros of real univariate functions.
027 * <p>
028 * The function should be continuous but not necessarily smooth.</p>
029 *
030 * @deprecated as of 3.1, replaced by {@link NewtonRaphsonSolver}
031 * @version $Id: NewtonSolver.java 1395937 2012-10-09 10:04:36Z luc $
032 */
033 @Deprecated
034 public class NewtonSolver extends AbstractDifferentiableUnivariateSolver {
035 /** Default absolute accuracy. */
036 private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6;
037
038 /**
039 * Construct a solver.
040 */
041 public NewtonSolver() {
042 this(DEFAULT_ABSOLUTE_ACCURACY);
043 }
044 /**
045 * Construct a solver.
046 *
047 * @param absoluteAccuracy Absolute accuracy.
048 */
049 public NewtonSolver(double absoluteAccuracy) {
050 super(absoluteAccuracy);
051 }
052
053 /**
054 * Find a zero near the midpoint of {@code min} and {@code max}.
055 *
056 * @param f Function to solve.
057 * @param min Lower bound for the interval.
058 * @param max Upper bound for the interval.
059 * @param maxEval Maximum number of evaluations.
060 * @return the value where the function is zero.
061 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
062 * if the maximum evaluation count is exceeded.
063 * @throws org.apache.commons.math3.exception.NumberIsTooLargeException
064 * if {@code min >= max}.
065 */
066 @Override
067 public double solve(int maxEval, final DifferentiableUnivariateFunction f,
068 final double min, final double max)
069 throws TooManyEvaluationsException {
070 return super.solve(maxEval, f, UnivariateSolverUtils.midpoint(min, max));
071 }
072
073 /**
074 * {@inheritDoc}
075 */
076 @Override
077 protected double doSolve()
078 throws TooManyEvaluationsException {
079 final double startValue = getStartValue();
080 final double absoluteAccuracy = getAbsoluteAccuracy();
081
082 double x0 = startValue;
083 double x1;
084 while (true) {
085 x1 = x0 - (computeObjectiveValue(x0) / computeDerivativeObjectiveValue(x0));
086 if (FastMath.abs(x1 - x0) <= absoluteAccuracy) {
087 return x1;
088 }
089
090 x0 = x1;
091 }
092 }
093 }