Java API By Example, From Geeks To Geeks.

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

85     *
• lowerBound <= a < initial < b <= upperBound
• 86      *
• f(a) * f(b) < 0
• 87      *
88      * If f is continuous on [a,b], this means that a89      * and b bracket a root of f.90      *

91      * The algorithm starts by setting 92      * a := initial -1; b := initial +1, examines the value of the93      * function at a and b and keeps moving94      * the endpoints out by one unit each time through a loop that terminates 95      * when one of the following happens:

96      *
• f(a) * f(b) < 0 -- success!
• 97      *
• a = lower and b = upper 98      * -- ConvergenceException
• 99      *
• Integer.MAX_VALUE iterations elapse 100      * -- ConvergenceException
• 101      *
102      *

103      * Note: this method can take 104      * Integer.MAX_VALUE iterations to throw a 105      * ConvergenceException. Unless you are confident that there106      * is a root between lowerBound and upperBound107      * near initial, it is better to use 108      * {@link #bracket(UnivariateRealFunction, double, double, double, int)}, 109      * explicitly specifying the maximum number of iterations.110      *111      * @param function the function112      * @param initial initial midpoint of interval being expanded to113      * bracket a root114      * @param lowerBound lower bound (a is never lower than this value)115      * @param upperBound upper bound (b never is greater than this116      * value)117      * @return a two element array holding {a, b}118      * @throws ConvergenceException if a root can not be bracketted119      * @throws FunctionEvaluationException if an error occurs evaluating the120      * function121      * @throws IllegalArgumentException if function is null, maximumIterations122      * is not positive, or initial is not between lowerBound and upperBound123      */124     public static double[] bracket(UnivariateRealFunction function, 125             double initial, double lowerBound, double upperBound) 126     throws ConvergenceException, FunctionEvaluationException {127         return bracket( function, initial, lowerBound, upperBound,128             Integer.MAX_VALUE ) ;129     }130 131      /**132      * This method attempts to find two values a and b satisfying

133      *
• lowerBound <= a < initial < b <= upperBound
• 134      *
• f(a) * f(b) < 0
• 135      *
136      * If f is continuous on [a,b], this means that a137      * and b bracket a root of f.138      *

139      * The algorithm starts by setting 140      * a := initial -1; b := initial +1, examines the value of the141      * function at a and b and keeps moving142      * the endpoints out by one unit each time through a loop that terminates 143      * when one of the following happens:

144      *
• f(a) * f(b) < 0 -- success!
• 145      *
• a = lower and b = upper 146      * -- ConvergenceException
• 147      *
• maximumIterations iterations elapse 148      * -- ConvergenceException
149      * 150      * @param function the function151      * @param initial initial midpoint of interval being expanded to152      * bracket a root153      * @param lowerBound lower bound (a is never lower than this value)154      * @param upperBound upper bound (b never is greater than this155      * value)156      * @param maximumIterations maximum number of iterations to perform157      * @return a two element array holding {a, b}.158      * @throws ConvergenceException if the algorithm fails to find a and b159      * satisfying the desired conditions160      * @throws FunctionEvaluationException if an error occurs evaluating the 161      * function162      * @throws IllegalArgumentException if function is null, maximumIterations163      * is not positive, or initial is not between lowerBound and upperBound164      */165     public static double[] bracket(UnivariateRealFunction function,166             double initial, double lowerBound, double upperBound, 167             int maximumIterations) throws ConvergenceException, 168             FunctionEvaluationException {169         170         if (function == null) {171             throw new IllegalArgumentException ("function is null.");172         }173         if (maximumIterations <= 0) {174             throw new IllegalArgumentException 175             ("bad value for maximumIterations: " + maximumIterations);176         }177         if (initial < lowerBound || initial > upperBound || lowerBound >= upperBound) {178             throw new IllegalArgumentException 179             ("Invalid endpoint parameters: lowerBound=" + lowerBound + 180               " initial=" + initial + " upperBound=" + upperBound);181         }182         double a = initial;183         double b = initial;184         double fa;185         double fb;186         int numIterations = 0 ;187     188         do {189             a = Math.max(a - 1.0, lowerBound);190             b = Math.min(b + 1.0, upperBound);191             fa = function.value(a);192             193             fb = function.value(b);194             numIterations++ ;195         } while ((fa * fb > 0.0) && (numIterations < maximumIterations) && 196                 ((a > lowerBound) || (b < upperBound)));197    198         if (fa * fb >= 0.0 ) {199             throw new ConvergenceException200             ("Number of iterations= " + numIterations +201               " maximum iterations= " + maximumIterations +202               " initial= " + initial + " lowerBound=" + lowerBound +203               " upperBound=" + upperBound + " final a value=" + a +204               " final b value=" + b + " f(a)=" + fa + " f(b)=" + fb);205         }206         207         return new double[]{a, b};208     }209 210     /**211      * Compute the midpoint of two values.212      * 213      * @param a first value.214      * @param b second value.215      * @return the midpoint. 216      */217     public static double midpoint(double a, double b) {218         return (a + b) * .5;219     }220     221     /**222      * Checks to see if f is null, throwing IllegalArgumentException if so.223      * Also initializes factory if factory is null.224      * 225      * @param f input function226      * @throws IllegalArgumentException if f is null227      */228     private static void setup(UnivariateRealFunction f) {229        230         if (f == null) {231             throw new IllegalArgumentException ("function can not be null."); 232         }233         234         if (factory == null) {235             factory = UnivariateRealSolverFactory.newInstance();236         } 237     }238 }239 Popular Tags