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 018package org.apache.commons.math3.analysis.solvers; 019 020import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; 021import org.apache.commons.math3.util.FastMath; 022import 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 */ 032@Deprecated 033public class NewtonSolver extends AbstractDifferentiableUnivariateSolver { 034 /** Default absolute accuracy. */ 035 private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; 036 037 /** 038 * Construct a solver. 039 */ 040 public NewtonSolver() { 041 this(DEFAULT_ABSOLUTE_ACCURACY); 042 } 043 /** 044 * Construct a solver. 045 * 046 * @param absoluteAccuracy Absolute accuracy. 047 */ 048 public NewtonSolver(double absoluteAccuracy) { 049 super(absoluteAccuracy); 050 } 051 052 /** 053 * Find a zero near the midpoint of {@code min} and {@code max}. 054 * 055 * @param f Function to solve. 056 * @param min Lower bound for the interval. 057 * @param max Upper bound for the interval. 058 * @param maxEval Maximum number of evaluations. 059 * @return the value where the function is zero. 060 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException 061 * if the maximum evaluation count is exceeded. 062 * @throws org.apache.commons.math3.exception.NumberIsTooLargeException 063 * if {@code min >= max}. 064 */ 065 @Override 066 public double solve(int maxEval, final DifferentiableUnivariateFunction f, 067 final double min, final double max) 068 throws TooManyEvaluationsException { 069 return super.solve(maxEval, f, UnivariateSolverUtils.midpoint(min, max)); 070 } 071 072 /** 073 * {@inheritDoc} 074 */ 075 @Override 076 protected double doSolve() 077 throws TooManyEvaluationsException { 078 final double startValue = getStartValue(); 079 final double absoluteAccuracy = getAbsoluteAccuracy(); 080 081 double x0 = startValue; 082 double x1; 083 while (true) { 084 x1 = x0 - (computeObjectiveValue(x0) / computeDerivativeObjectiveValue(x0)); 085 if (FastMath.abs(x1 - x0) <= absoluteAccuracy) { 086 return x1; 087 } 088 089 x0 = x1; 090 } 091 } 092}