1 16 package org.apache.commons.math.analysis; 17 18 import org.apache.commons.math.FunctionEvaluationException; 19 import org.apache.commons.math.ConvergenceException; 20 21 26 public class UnivariateRealSolverUtils { 27 30 private UnivariateRealSolverUtils() { 31 super(); 32 } 33 34 35 private static UnivariateRealSolverFactory factory = null; 36 37 51 public static double solve(UnivariateRealFunction f, double x0, double x1) 52 throws ConvergenceException, FunctionEvaluationException { 53 setup(f); 54 return factory.newDefaultSolver(f).solve(x0, x1); 55 } 56 57 73 public static double solve(UnivariateRealFunction f, double x0, double x1, 74 double absoluteAccuracy) throws ConvergenceException, 75 FunctionEvaluationException { 76 77 setup(f); 78 UnivariateRealSolver solver = factory.newDefaultSolver(f); 79 solver.setAbsoluteAccuracy(absoluteAccuracy); 80 return solver.solve(x0, x1); 81 } 82 83 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 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 ConvergenceException 200 ("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 217 public static double midpoint(double a, double b) { 218 return (a + b) * .5; 219 } 220 221 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 |