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 29 public class BisectionSolver extends UnivariateRealSolverImpl { 30 31 32 static final long serialVersionUID = 7137520585963699578L; 33 34 39 public BisectionSolver(UnivariateRealFunction f) { 40 super(f, 100, 1E-6); 41 } 42 43 55 public double solve(double min, double max, double initial) 56 throws ConvergenceException, FunctionEvaluationException { 57 58 return solve(min, max); 59 } 60 61 72 public double solve(double min, double max) throws ConvergenceException, 73 FunctionEvaluationException { 74 75 clearResult(); 76 verifyInterval(min,max); 77 double m; 78 double fm; 79 double fmin; 80 81 int i = 0; 82 while (i < maximalIterationCount) { 83 m = UnivariateRealSolverUtils.midpoint(min, max); 84 fmin = f.value(min); 85 fm = f.value(m); 86 87 if (fm * fmin > 0.0) { 88 min = m; 90 fmin = fm; 91 } else { 92 max = m; 94 } 95 96 if (Math.abs(max - min) <= absoluteAccuracy) { 97 m = UnivariateRealSolverUtils.midpoint(min, max); 98 setResult(m, i); 99 return m; 100 } 101 ++i; 102 } 103 104 throw new ConvergenceException 105 ("Maximum number of iterations exceeded: " + maximalIterationCount); 106 } 107 } 108 | Popular Tags |