Java API By Example, From Geeks To Geeks.

# Java > Open Source Codes > org > apache > commons > math > analysis > NewtonSolver

 `1 /*2  * Copyright 2004 The Apache Software Foundation.3  *4  * Licensed under the Apache License, Version 2.0 (the "License");5  * you may not use this file except in compliance with the License.6  * You may obtain a copy of the License at7  *8  * http://www.apache.org/licenses/LICENSE-2.09  *10  * Unless required by applicable law or agreed to in writing, software11  * distributed under the License is distributed on an "AS IS" BASIS,12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13  * See the License for the specific language governing permissions and14  * limitations under the License.15  */16 17 package org.apache.commons.math.analysis;18 19 import org.apache.commons.math.ConvergenceException;20 import org.apache.commons.math.FunctionEvaluationException; 21 22 /**23  * Implements 24  * Newton's Method for finding zeros of real univariate functions. 25  *

26  * The function should be continuous but not necessarily smooth.27  *28  * @version \$Revision\$ \$Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) \$29  */30 public class NewtonSolver extends UnivariateRealSolverImpl {31     32     /** Serializable version identifier */33     static final long serialVersionUID = 2606474895443431607L;34     35     /** The first derivative of the target function. */36     private UnivariateRealFunction derivative;37     38     /**39      * Construct a solver for the given function.40      * @param f function to solve.41      */42     public NewtonSolver(DifferentiableUnivariateRealFunction f) {43         super(f, 100, 1E-6);44         derivative = f.derivative();45     }46 47     /**48      * Find a zero near the midpoint of min and max.49      * 50      * @param min the lower bound for the interval51      * @param max the upper bound for the interval52      * @return the value where the function is zero53      * @throws ConvergenceException if the maximum iteration count is exceeded 54      * @throws FunctionEvaluationException if an error occurs evaluating the55      * function or derivative56      * @throws IllegalArgumentException if min is not less than max57      */58     public double solve(double min, double max) throws ConvergenceException, 59         FunctionEvaluationException {60         return solve(min, max, UnivariateRealSolverUtils.midpoint(min, max));61     }62 63     /**64      * Find a zero near the value startValue.65      * 66      * @param min the lower bound for the interval (ignored).67      * @param max the upper bound for the interval (ignored).68      * @param startValue the start value to use.69      * @return the value where the function is zero70     * @throws ConvergenceException if the maximum iteration count is exceeded 71      * @throws FunctionEvaluationException if an error occurs evaluating the72      * function or derivative73      * @throws IllegalArgumentException if startValue is not between min and max74      */75     public double solve(double min, double max, double startValue)76         throws ConvergenceException, FunctionEvaluationException {77         78         clearResult();79         verifySequence(min, startValue, max);80 81         double x0 = startValue;82         double x1;83         84         int i = 0;85         while (i < maximalIterationCount) {86             x1 = x0 - (f.value(x0) / derivative.value(x0));87             if (Math.abs(x1 - x0) <= absoluteAccuracy) {88                 89                 setResult(x1, i);90                 return x1;91             }92             93             x0 = x1;94             ++i;95         }96         97         throw new ConvergenceException98             ("Maximum number of iterations exceeded " + i);99     }100 101 }102 ` Popular Tags