Java Code Examples for org.apache.commons.math.analysis.UnivariateRealFunction#value()
The following examples show how to use
org.apache.commons.math.analysis.UnivariateRealFunction#value() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NPEfix_00152_s.java From coming with MIT License | 5 votes |
/** {@inheritDoc} */ public double solve(final UnivariateRealFunction f, double min, double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min,max); double m; double fm; double fmin; int i = 0; while (i < maximalIterationCount) { m = UnivariateRealSolverUtils.midpoint(min, max); fmin = f.value(min); fm = f.value(m); if (fm * fmin > 0.0) { // max and m bracket the root. min = m; } else { // min and m bracket the root. max = m; } if (Math.abs(max - min) <= absoluteAccuracy) { m = UnivariateRealSolverUtils.midpoint(min, max); setResult(m, i); return m; } ++i; } throw new MaxIterationsExceededException(maximalIterationCount); }
Example 2
Source File: JGenProg2017_0031_s.java From coming with MIT License | 5 votes |
/** {@inheritDoc} */ public double solve(final UnivariateRealFunction f, double min, double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min,max); double m; double fm; double fmin; int i = 0; while (i < maximalIterationCount) { m = UnivariateRealSolverUtils.midpoint(min, max); fmin = f.value(min); fm = f.value(m); if (fm * fmin > 0.0) { // max and m bracket the root. min = m; } else { // min and m bracket the root. max = m; } if (Math.abs(max - min) <= absoluteAccuracy) { m = UnivariateRealSolverUtils.midpoint(min, max); setResult(m, i); return m; } ++i; } throw new MaxIterationsExceededException(maximalIterationCount); }
Example 3
Source File: Math_70_BisectionSolver_s.java From coming with MIT License | 5 votes |
/** {@inheritDoc} */ public double solve(final UnivariateRealFunction f, double min, double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min,max); double m; double fm; double fmin; int i = 0; while (i < maximalIterationCount) { m = UnivariateRealSolverUtils.midpoint(min, max); fmin = f.value(min); fm = f.value(m); if (fm * fmin > 0.0) { // max and m bracket the root. min = m; } else { // min and m bracket the root. max = m; } if (Math.abs(max - min) <= absoluteAccuracy) { m = UnivariateRealSolverUtils.midpoint(min, max); setResult(m, i); return m; } ++i; } throw new MaxIterationsExceededException(maximalIterationCount); }
Example 4
Source File: Cardumen_00122_s.java From coming with MIT License | 5 votes |
/** {@inheritDoc} */ public double solve(final UnivariateRealFunction f, double min, double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min,max); double m; double fm; double fmin; int i = 0; while (i < maximalIterationCount) { m = UnivariateRealSolverUtils.midpoint(min, max); fmin = f.value(min); fm = f.value(m); if (fm * fmin > 0.0) { // max and m bracket the root. min = m; } else { // min and m bracket the root. max = m; } if (Math.abs(max - min) <= absoluteAccuracy) { m = UnivariateRealSolverUtils.midpoint(min, max); setResult(m, i); return m; } ++i; } throw new MaxIterationsExceededException(maximalIterationCount); }
Example 5
Source File: jKali_0017_t.java From coming with MIT License | 5 votes |
/** * This method attempts to find two values a and b satisfying <ul> * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li> * <li> <code> f(a) * f(b) <= 0 </code> </li> * </ul> * If f is continuous on <code>[a,b],</code> this means that <code>a</code> * and <code>b</code> bracket a root of f. * <p> * The algorithm starts by setting * <code>a := initial -1; b := initial +1,</code> examines the value of the * function at <code>a</code> and <code>b</code> and keeps moving * the endpoints out by one unit each time through a loop that terminates * when one of the following happens: <ul> * <li> <code> f(a) * f(b) <= 0 </code> -- success!</li> * <li> <code> a = lower </code> and <code> b = upper</code> * -- ConvergenceException </li> * <li> <code> maximumIterations</code> iterations elapse * -- ConvergenceException </li></ul></p> * * @param function the function * @param initial initial midpoint of interval being expanded to * bracket a root * @param lowerBound lower bound (a is never lower than this value) * @param upperBound upper bound (b never is greater than this * value) * @param maximumIterations maximum number of iterations to perform * @return a two element array holding {a, b}. * @throws ConvergenceException if the algorithm fails to find a and b * satisfying the desired conditions * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if function is null, maximumIterations * is not positive, or initial is not between lowerBound and upperBound */ public static double[] bracket(UnivariateRealFunction function, double initial, double lowerBound, double upperBound, int maximumIterations) throws ConvergenceException, FunctionEvaluationException { if (function == null) { throw MathRuntimeException.createIllegalArgumentException("function is null"); } if (maximumIterations <= 0) { throw MathRuntimeException.createIllegalArgumentException( "bad value for maximum iterations number: {0}", maximumIterations); } if (initial < lowerBound || initial > upperBound || lowerBound >= upperBound) { throw MathRuntimeException.createIllegalArgumentException( "invalid bracketing parameters: lower bound={0}, initial={1}, upper bound={2}", lowerBound, initial, upperBound); } double a = initial; double b = initial; double fa; double fb; int numIterations = 0 ; do { a = Math.max(a - 1.0, lowerBound); b = Math.min(b + 1.0, upperBound); fa = function.value(a); fb = function.value(b); numIterations++ ; } while ((fa * fb > 0.0) && (numIterations < maximumIterations) && ((a > lowerBound) || (b < upperBound))); if (fa * fb >= 0.0 ) { } return new double[]{a, b}; }
Example 6
Source File: NPEfix_00154_s.java From coming with MIT License | 5 votes |
/** {@inheritDoc} */ public double solve(final UnivariateRealFunction f, double min, double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min,max); double m; double fm; double fmin; int i = 0; while (i < maximalIterationCount) { m = UnivariateRealSolverUtils.midpoint(min, max); fmin = f.value(min); fm = f.value(m); if (fm * fmin > 0.0) { // max and m bracket the root. min = m; } else { // min and m bracket the root. max = m; } if (Math.abs(max - min) <= absoluteAccuracy) { m = UnivariateRealSolverUtils.midpoint(min, max); setResult(m, i); return m; } ++i; } throw new MaxIterationsExceededException(maximalIterationCount); }
Example 7
Source File: JGenProg2017_0067_s.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval. * <p> * Requires that the values of the function at the endpoints have opposite * signs. An <code>IllegalArgumentException</code> is thrown if this is not * the case.</p> * * @param f the function to solve * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @return the value where the function is zero * @throws MaxIterationsExceededException if the maximum iteration count is exceeded * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if min is not less than max or the * signs of the values of the function at the endpoints are not opposites */ public double solve(final UnivariateRealFunction f, final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min, max); double ret = Double.NaN; double yMin = f.value(min); double yMax = f.value(max); // Verify bracketing double sign = yMin * yMax; if (sign > 0) { // check if either value is close to a zero if (Math.abs(yMin) <= functionValueAccuracy) { setResult(min, 0); ret = min; } else if (Math.abs(yMax) <= functionValueAccuracy) { setResult(max, 0); ret = max; } else { // neither value is close to zero and min and max do not bracket root. throw MathRuntimeException.createIllegalArgumentException( NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } } else if (sign < 0){ // solve using only the first endpoint as initial guess ret = solve(f, min, yMin, max, yMax, min, yMin); } else { // either min or max is a root if (yMin == 0.0) { ret = min; } else { ret = max; } } return ret; }
Example 8
Source File: Math_72_BrentSolver_s.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval with an initial guess. * <p>Throws <code>IllegalArgumentException</code> if the values of the * function at the three points have the same sign (note that it is * allowed to have endpoints with the same sign if the initial point has * opposite sign function-wise).</p> * * @param f function to solve. * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @param initial the start value to use (must be set to min if no * initial point is known). * @return the value where the function is zero * @throws MaxIterationsExceededException the maximum iteration count * is exceeded * @throws FunctionEvaluationException if an error occurs evaluating * the function * @throws IllegalArgumentException if initial is not between min and max * (even if it <em>is</em> a root) */ public double solve(final UnivariateRealFunction f, final double min, final double max, final double initial) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifySequence(min, initial, max); // return the initial guess if it is good enough double yInitial = f.value(initial); if (Math.abs(yInitial) <= functionValueAccuracy) { setResult(initial, 0); return result; } // return the first endpoint if it is good enough double yMin = f.value(min); if (Math.abs(yMin) <= functionValueAccuracy) { setResult(yMin, 0); return result; } // reduce interval if min and initial bracket the root if (yInitial * yMin < 0) { return solve(f, min, yMin, initial, yInitial, min, yMin); } // return the second endpoint if it is good enough double yMax = f.value(max); if (Math.abs(yMax) <= functionValueAccuracy) { setResult(yMax, 0); return result; } // reduce interval if initial and max bracket the root if (yInitial * yMax < 0) { return solve(f, initial, yInitial, max, yMax, initial, yInitial); } if (yMin * yMax > 0) { throw MathRuntimeException.createIllegalArgumentException( NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } // full Brent algorithm starting with provided initial guess return solve(f, min, yMin, max, yMax, initial, yInitial); }
Example 9
Source File: JGenProg2017_0093_t.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval. * <p> * Requires that the values of the function at the endpoints have opposite * signs. An <code>IllegalArgumentException</code> is thrown if this is not * the case.</p> * * @param f the function to solve * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @return the value where the function is zero * @throws MaxIterationsExceededException if the maximum iteration count is exceeded * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if min is not less than max or the * signs of the values of the function at the endpoints are not opposites */ public double solve(final UnivariateRealFunction f, final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min, max); double ret = Double.NaN; double yMin = f.value(min); double yMax = f.value(max); // Verify bracketing double sign = yMin * yMax; if (sign > 0) { // check if either value is close to a zero if (Math.abs(yMin) <= functionValueAccuracy) { setResult(min, 0); ret = min; } else if (Math.abs(yMax) <= functionValueAccuracy) { setResult(max, 0); ret = max; } else { // neither value is close to zero and min and max do not bracket root. throw MathRuntimeException.createIllegalArgumentException( NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } } else if (sign < 0){ // solve using only the first endpoint as initial guess ret = solve(f, min, yMin, max, yMax, min, yMin); } else { // either min or max is a root if (yMin == 0.0) { ret = min; } else { ret = max; } } return ret; }
Example 10
Source File: Math_73_BrentSolver_s.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval with an initial guess. * <p>Throws <code>IllegalArgumentException</code> if the values of the * function at the three points have the same sign (note that it is * allowed to have endpoints with the same sign if the initial point has * opposite sign function-wise).</p> * * @param f function to solve. * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @param initial the start value to use (must be set to min if no * initial point is known). * @return the value where the function is zero * @throws MaxIterationsExceededException the maximum iteration count * is exceeded * @throws FunctionEvaluationException if an error occurs evaluating * the function * @throws IllegalArgumentException if initial is not between min and max * (even if it <em>is</em> a root) */ public double solve(final UnivariateRealFunction f, final double min, final double max, final double initial) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifySequence(min, initial, max); // return the initial guess if it is good enough double yInitial = f.value(initial); if (Math.abs(yInitial) <= functionValueAccuracy) { setResult(initial, 0); return result; } // return the first endpoint if it is good enough double yMin = f.value(min); if (Math.abs(yMin) <= functionValueAccuracy) { setResult(yMin, 0); return result; } // reduce interval if min and initial bracket the root if (yInitial * yMin < 0) { return solve(f, min, yMin, initial, yInitial, min, yMin); } // return the second endpoint if it is good enough double yMax = f.value(max); if (Math.abs(yMax) <= functionValueAccuracy) { setResult(yMax, 0); return result; } // reduce interval if initial and max bracket the root if (yInitial * yMax < 0) { return solve(f, initial, yInitial, max, yMax, initial, yInitial); } // full Brent algorithm starting with provided initial guess return solve(f, min, yMin, max, yMax, initial, yInitial); }
Example 11
Source File: JGenProg2015_009_t.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval with an initial guess. * <p>Throws <code>IllegalArgumentException</code> if the values of the * function at the three points have the same sign (note that it is * allowed to have endpoints with the same sign if the initial point has * opposite sign function-wise).</p> * * @param f function to solve. * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @param initial the start value to use (must be set to min if no * initial point is known). * @return the value where the function is zero * @throws MaxIterationsExceededException the maximum iteration count * is exceeded * @throws FunctionEvaluationException if an error occurs evaluating * the function * @throws IllegalArgumentException if initial is not between min and max * (even if it <em>is</em> a root) */ public double solve(final UnivariateRealFunction f, final double min, final double max, final double initial) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifySequence(min, initial, max); // return the initial guess if it is good enough double yInitial = f.value(initial); if (Math.abs(yInitial) <= functionValueAccuracy) { setResult(initial, 0); return result; } // return the first endpoint if it is good enough double yMin = f.value(min); if (Math.abs(yMin) <= functionValueAccuracy) { setResult(yMin, 0); return result; } // reduce interval if min and initial bracket the root if (yInitial * yMin < 0) { return solve(f, min, yMin, initial, yInitial, min, yMin); } // return the second endpoint if it is good enough double yMax = f.value(max); if (Math.abs(yMax) <= functionValueAccuracy) { setResult(yMax, 0); return result; } // reduce interval if initial and max bracket the root if (yInitial * yMax < 0) { return solve(f, min, max); } // full Brent algorithm starting with provided initial guess return solve(f, min, yMin, max, yMax, initial, yInitial); }
Example 12
Source File: Math_85_UnivariateRealSolverUtils_t.java From coming with MIT License | 4 votes |
/** * This method attempts to find two values a and b satisfying <ul> * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li> * <li> <code> f(a) * f(b) <= 0 </code> </li> * </ul> * If f is continuous on <code>[a,b],</code> this means that <code>a</code> * and <code>b</code> bracket a root of f. * <p> * The algorithm starts by setting * <code>a := initial -1; b := initial +1,</code> examines the value of the * function at <code>a</code> and <code>b</code> and keeps moving * the endpoints out by one unit each time through a loop that terminates * when one of the following happens: <ul> * <li> <code> f(a) * f(b) <= 0 </code> -- success!</li> * <li> <code> a = lower </code> and <code> b = upper</code> * -- ConvergenceException </li> * <li> <code> maximumIterations</code> iterations elapse * -- ConvergenceException </li></ul></p> * * @param function the function * @param initial initial midpoint of interval being expanded to * bracket a root * @param lowerBound lower bound (a is never lower than this value) * @param upperBound upper bound (b never is greater than this * value) * @param maximumIterations maximum number of iterations to perform * @return a two element array holding {a, b}. * @throws ConvergenceException if the algorithm fails to find a and b * satisfying the desired conditions * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if function is null, maximumIterations * is not positive, or initial is not between lowerBound and upperBound */ public static double[] bracket(UnivariateRealFunction function, double initial, double lowerBound, double upperBound, int maximumIterations) throws ConvergenceException, FunctionEvaluationException { if (function == null) { throw MathRuntimeException.createIllegalArgumentException("function is null"); } if (maximumIterations <= 0) { throw MathRuntimeException.createIllegalArgumentException( "bad value for maximum iterations number: {0}", maximumIterations); } if (initial < lowerBound || initial > upperBound || lowerBound >= upperBound) { throw MathRuntimeException.createIllegalArgumentException( "invalid bracketing parameters: lower bound={0}, initial={1}, upper bound={2}", lowerBound, initial, upperBound); } double a = initial; double b = initial; double fa; double fb; int numIterations = 0 ; do { a = Math.max(a - 1.0, lowerBound); b = Math.min(b + 1.0, upperBound); fa = function.value(a); fb = function.value(b); numIterations++ ; } while ((fa * fb > 0.0) && (numIterations < maximumIterations) && ((a > lowerBound) || (b < upperBound))); if (fa * fb > 0.0 ) { throw new ConvergenceException( "number of iterations={0}, maximum iterations={1}, " + "initial={2}, lower bound={3}, upper bound={4}, final a value={5}, " + "final b value={6}, f(a)={7}, f(b)={8}", numIterations, maximumIterations, initial, lowerBound, upperBound, a, b, fa, fb); } return new double[]{a, b}; }
Example 13
Source File: Math_73_BrentSolver_s.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval. * <p> * Requires that the values of the function at the endpoints have opposite * signs. An <code>IllegalArgumentException</code> is thrown if this is not * the case.</p> * * @param f the function to solve * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @return the value where the function is zero * @throws MaxIterationsExceededException if the maximum iteration count is exceeded * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if min is not less than max or the * signs of the values of the function at the endpoints are not opposites */ public double solve(final UnivariateRealFunction f, final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min, max); double ret = Double.NaN; double yMin = f.value(min); double yMax = f.value(max); // Verify bracketing double sign = yMin * yMax; if (sign > 0) { // check if either value is close to a zero if (Math.abs(yMin) <= functionValueAccuracy) { setResult(min, 0); ret = min; } else if (Math.abs(yMax) <= functionValueAccuracy) { setResult(max, 0); ret = max; } else { // neither value is close to zero and min and max do not bracket root. throw MathRuntimeException.createIllegalArgumentException( NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } } else if (sign < 0){ // solve using only the first endpoint as initial guess ret = solve(f, min, yMin, max, yMax, min, yMin); } else { // either min or max is a root if (yMin == 0.0) { ret = min; } else { ret = max; } } return ret; }
Example 14
Source File: 1_UnivariateRealSolverUtils.java From SimFix with GNU General Public License v2.0 | 4 votes |
/** * This method attempts to find two values a and b satisfying <ul> * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li> * <li> <code> f(a) * f(b) <= 0 </code> </li> * </ul> * If f is continuous on <code>[a,b],</code> this means that <code>a</code> * and <code>b</code> bracket a root of f. * <p> * The algorithm starts by setting * <code>a := initial -1; b := initial +1,</code> examines the value of the * function at <code>a</code> and <code>b</code> and keeps moving * the endpoints out by one unit each time through a loop that terminates * when one of the following happens: <ul> * <li> <code> f(a) * f(b) <= 0 </code> -- success!</li> * <li> <code> a = lower </code> and <code> b = upper</code> * -- ConvergenceException </li> * <li> <code> maximumIterations</code> iterations elapse * -- ConvergenceException </li></ul></p> * * @param function the function * @param initial initial midpoint of interval being expanded to * bracket a root * @param lowerBound lower bound (a is never lower than this value) * @param upperBound upper bound (b never is greater than this * value) * @param maximumIterations maximum number of iterations to perform * @return a two element array holding {a, b}. * @throws ConvergenceException if the algorithm fails to find a and b * satisfying the desired conditions * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if function is null, maximumIterations * is not positive, or initial is not between lowerBound and upperBound */ public static double[] bracket(UnivariateRealFunction function, double initial, double lowerBound, double upperBound, int maximumIterations) throws ConvergenceException, FunctionEvaluationException { if (function == null) { throw MathRuntimeException.createIllegalArgumentException("function is null"); } if (maximumIterations <= 0) { throw MathRuntimeException.createIllegalArgumentException( "bad value for maximum iterations number: {0}", maximumIterations); } if (initial < lowerBound || initial > upperBound || lowerBound >= upperBound) { throw MathRuntimeException.createIllegalArgumentException( "invalid bracketing parameters: lower bound={0}, initial={1}, upper bound={2}", lowerBound, initial, upperBound); } double a = initial; double b = initial; double fa; double fb; int numIterations = 0 ; do { a = Math.max(a - 1.0, lowerBound); b = Math.min(b + 1.0, upperBound); fa = function.value(a); fb = function.value(b); numIterations++ ; } while ((fa * fb > 0.0) && (numIterations < maximumIterations) && ((a > lowerBound) || (b < upperBound))); // start of generated patch if(fa*upperBound>=0.0){ throw new ConvergenceException("number of iterations={0}, maximum iterations={1}, "+"initial={2}, lower bound={3}, upper bound={4}, final a value={5}, ",numIterations,maximumIterations,initial,lowerBound,upperBound,a,b,fa,fb); } // end of generated patch /* start of original code if (fa * fb >= 0.0 ) { throw new ConvergenceException( "number of iterations={0}, maximum iterations={1}, " + "initial={2}, lower bound={3}, upper bound={4}, final a value={5}, " + "final b value={6}, f(a)={7}, f(b)={8}", numIterations, maximumIterations, initial, lowerBound, upperBound, a, b, fa, fb); } end of original code*/ return new double[]{a, b}; }
Example 15
Source File: Cardumen_0065_s.java From coming with MIT License | 4 votes |
/** * This method attempts to find two values a and b satisfying <ul> * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li> * <li> <code> f(a) * f(b) <= 0 </code> </li> * </ul> * If f is continuous on <code>[a,b],</code> this means that <code>a</code> * and <code>b</code> bracket a root of f. * <p> * The algorithm starts by setting * <code>a := initial -1; b := initial +1,</code> examines the value of the * function at <code>a</code> and <code>b</code> and keeps moving * the endpoints out by one unit each time through a loop that terminates * when one of the following happens: <ul> * <li> <code> f(a) * f(b) <= 0 </code> -- success!</li> * <li> <code> a = lower </code> and <code> b = upper</code> * -- ConvergenceException </li> * <li> <code> maximumIterations</code> iterations elapse * -- ConvergenceException </li></ul></p> * * @param function the function * @param initial initial midpoint of interval being expanded to * bracket a root * @param lowerBound lower bound (a is never lower than this value) * @param upperBound upper bound (b never is greater than this * value) * @param maximumIterations maximum number of iterations to perform * @return a two element array holding {a, b}. * @throws ConvergenceException if the algorithm fails to find a and b * satisfying the desired conditions * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if function is null, maximumIterations * is not positive, or initial is not between lowerBound and upperBound */ public static double[] bracket(UnivariateRealFunction function, double initial, double lowerBound, double upperBound, int maximumIterations) throws ConvergenceException, FunctionEvaluationException { if (function == null) { throw MathRuntimeException.createIllegalArgumentException("function is null"); } if (maximumIterations <= 0) { throw MathRuntimeException.createIllegalArgumentException( "bad value for maximum iterations number: {0}", maximumIterations); } if (initial < lowerBound || initial > upperBound || lowerBound >= upperBound) { throw MathRuntimeException.createIllegalArgumentException( "invalid bracketing parameters: lower bound={0}, initial={1}, upper bound={2}", lowerBound, initial, upperBound); } double a = initial; double b = initial; double fa; double fb; int numIterations = 0 ; do { a = Math.max(a - 1.0, lowerBound); b = Math.min(b + 1.0, upperBound); fa = function.value(a); fb = function.value(b); numIterations++ ; } while ((fa * fb > 0.0) && (numIterations < maximumIterations) && ((a > lowerBound) || (b < upperBound))); if (fa * fb >= 0.0 ) { throw new ConvergenceException( "number of iterations={0}, maximum iterations={1}, " + "initial={2}, lower bound={3}, upper bound={4}, final a value={5}, " + "final b value={6}, f(a)={7}, f(b)={8}", numIterations, maximumIterations, initial, lowerBound, upperBound, a, b, fa, fb); } return new double[]{a, b}; }
Example 16
Source File: JGenProg2017_0067_t.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval. * <p> * Requires that the values of the function at the endpoints have opposite * signs. An <code>IllegalArgumentException</code> is thrown if this is not * the case.</p> * * @param f the function to solve * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @return the value where the function is zero * @throws MaxIterationsExceededException if the maximum iteration count is exceeded * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if min is not less than max or the * signs of the values of the function at the endpoints are not opposites */ public double solve(final UnivariateRealFunction f, final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min, max); double ret = Double.NaN; double yMin = f.value(min); double yMax = f.value(max); // Verify bracketing double sign = yMin * yMax; if (sign > 0) { // check if either value is close to a zero if (Math.abs(yMin) <= functionValueAccuracy) { setResult(min, 0); ret = min; } else if (Math.abs(yMax) <= functionValueAccuracy) { setResult(max, 0); ret = max; } else { // neither value is close to zero and min and max do not bracket root. throw MathRuntimeException.createIllegalArgumentException( NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } } else if (sign < 0){ // solve using only the first endpoint as initial guess ret = solve(f, min, yMin, max, yMax, min, yMin); } else { // either min or max is a root if (yMin == 0.0) { ret = min; } else { ret = max; } } return ret; }
Example 17
Source File: JGenProg2017_0032_s.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval. * <p> * Requires that the values of the function at the endpoints have opposite * signs. An <code>IllegalArgumentException</code> is thrown if this is not * the case.</p> * * @param f the function to solve * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @return the value where the function is zero * @throws MaxIterationsExceededException if the maximum iteration count is exceeded * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if min is not less than max or the * signs of the values of the function at the endpoints are not opposites */ public double solve(final UnivariateRealFunction f, final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min, max); double ret = Double.NaN; double yMin = f.value(min); double yMax = f.value(max); // Verify bracketing double sign = yMin * yMax; if (sign > 0) { // check if either value is close to a zero if (Math.abs(yMin) <= functionValueAccuracy) { setResult(min, 0); ret = min; } else if (Math.abs(yMax) <= functionValueAccuracy) { setResult(max, 0); ret = max; } else { // neither value is close to zero and min and max do not bracket root. throw MathRuntimeException.createIllegalArgumentException( NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } } else if (sign < 0){ // solve using only the first endpoint as initial guess ret = solve(f, min, yMin, max, yMax, min, yMin); } else { // either min or max is a root if (yMin == 0.0) { ret = min; } else { ret = max; } } return ret; }
Example 18
Source File: arja10_ten_s.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval. * <p> * Requires that the values of the function at the endpoints have opposite * signs. An <code>IllegalArgumentException</code> is thrown if this is not * the case.</p> * * @param f the function to solve * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @return the value where the function is zero * @throws MaxIterationsExceededException if the maximum iteration count is exceeded * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if min is not less than max or the * signs of the values of the function at the endpoints are not opposites */ public double solve(final UnivariateRealFunction f, final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min, max); double ret = Double.NaN; double yMin = f.value(min); double yMax = f.value(max); // Verify bracketing double sign = yMin * yMax; if (sign > 0) { // check if either value is close to a zero if (Math.abs(yMin) <= functionValueAccuracy) { setResult(min, 0); ret = min; } else if (Math.abs(yMax) <= functionValueAccuracy) { setResult(max, 0); ret = max; } else { // neither value is close to zero and min and max do not bracket root. throw MathRuntimeException.createIllegalArgumentException( NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } } else if (sign < 0){ // solve using only the first endpoint as initial guess ret = solve(f, min, yMin, max, yMax, min, yMin); } else { // either min or max is a root if (yMin == 0.0) { ret = min; } else { ret = max; } } return ret; }
Example 19
Source File: Cardumen_0056_t.java From coming with MIT License | 4 votes |
/** * Find a zero in the given interval. * <p> * Requires that the values of the function at the endpoints have opposite * signs. An <code>IllegalArgumentException</code> is thrown if this is not * the case.</p> * * @param f the function to solve * @param min the lower bound for the interval. * @param max the upper bound for the interval. * @return the value where the function is zero * @throws MaxIterationsExceededException if the maximum iteration count is exceeded * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if min is not less than max or the * signs of the values of the function at the endpoints are not opposites */ public double solve(final UnivariateRealFunction f, final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException { clearResult(); verifyInterval(min, max); double ret = Double.NaN; double yMin = f.value(min); double yMax = f.value(max); // Verify bracketing double sign = yMin * yMax; if (sign > 0) { // check if either value is close to a zero if (Math.abs(yMin) <= functionValueAccuracy) { setResult(min, 0); ret = min; } else if (Math.abs(yMax) <= functionValueAccuracy) { setResult(max, 0); ret = max; } else { // neither value is close to zero and min and max do not bracket root. throw MathRuntimeException.createIllegalArgumentException( NON_BRACKETING_MESSAGE, min, max, yMin, yMax); } } else if (sign < 0){ // solve using only the first endpoint as initial guess ret = solve(f, min, yMin, max, yMax, min, yMin); } else { // either min or max is a root if (yMin == 0.0) { ret = min; } else { ret = max; } } return ret; }
Example 20
Source File: Cardumen_00184_t.java From coming with MIT License | 4 votes |
/** * This method attempts to find two values a and b satisfying <ul> * <li> <code> lowerBound <= a < initial < b <= upperBound</code> </li> * <li> <code> f(a) * f(b) <= 0 </code> </li> * </ul> * If f is continuous on <code>[a,b],</code> this means that <code>a</code> * and <code>b</code> bracket a root of f. * <p> * The algorithm starts by setting * <code>a := initial -1; b := initial +1,</code> examines the value of the * function at <code>a</code> and <code>b</code> and keeps moving * the endpoints out by one unit each time through a loop that terminates * when one of the following happens: <ul> * <li> <code> f(a) * f(b) <= 0 </code> -- success!</li> * <li> <code> a = lower </code> and <code> b = upper</code> * -- ConvergenceException </li> * <li> <code> maximumIterations</code> iterations elapse * -- ConvergenceException </li></ul></p> * * @param function the function * @param initial initial midpoint of interval being expanded to * bracket a root * @param lowerBound lower bound (a is never lower than this value) * @param upperBound upper bound (b never is greater than this * value) * @param maximumIterations maximum number of iterations to perform * @return a two element array holding {a, b}. * @throws ConvergenceException if the algorithm fails to find a and b * satisfying the desired conditions * @throws FunctionEvaluationException if an error occurs evaluating the * function * @throws IllegalArgumentException if function is null, maximumIterations * is not positive, or initial is not between lowerBound and upperBound */ public static double[] bracket(UnivariateRealFunction function, double initial, double lowerBound, double upperBound, int maximumIterations) throws ConvergenceException, FunctionEvaluationException { if (function == null) { throw MathRuntimeException.createIllegalArgumentException("function is null"); } if (maximumIterations <= 0) { throw MathRuntimeException.createIllegalArgumentException( "bad value for maximum iterations number: {0}", maximumIterations); } if (initial < lowerBound || initial > upperBound || lowerBound >= upperBound) { throw MathRuntimeException.createIllegalArgumentException( "invalid bracketing parameters: lower bound={0}, initial={1}, upper bound={2}", lowerBound, initial, upperBound); } double a = initial; double b = initial; double fa; double fb; int numIterations = 0 ; do { a = Math.max(a - 1.0, lowerBound); b = Math.min(b + 1.0, upperBound); fa = function.value(a); fb = function.value(b); numIterations++ ; } while ((fa * fb > 0.0) && (numIterations < maximumIterations) && ((a > lowerBound) || (b < upperBound))); if ((initial < lowerBound) || (initial > upperBound)) { throw new ConvergenceException( "number of iterations={0}, maximum iterations={1}, " + "initial={2}, lower bound={3}, upper bound={4}, final a value={5}, " + "final b value={6}, f(a)={7}, f(b)={8}", numIterations, maximumIterations, initial, lowerBound, upperBound, a, b, fa, fb); } return new double[]{a, b}; }