List of usage examples for org.apache.commons.math.analysis.solvers BrentSolver BrentSolver
public BrentSolver(double absoluteAccuracy)
From source file:geogebra.kernel.AlgoRootNewton.java
final double calcRoot(Function fun, double start) { double root = Double.NaN; if (rootFinderBrent == null) rootFinderBrent = new BrentSolver(Kernel.STANDARD_PRECISION); // try Brent method with borders close to start value try {/*from www .ja v a 2 s .c o m*/ double step = (kernel.getXmax() - kernel.getXmin()) / 10; root = rootFinderBrent.solve(MAX_ITERATIONS, new RealRootAdapter(fun), start - step, start + step, start); if (checkRoot(fun, root)) { //System.out.println("1. Brent worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } // try Brent method on valid interval around start double[] borders = getDomain(fun, start); try { root = rootFinderBrent.solve(MAX_ITERATIONS, new RealRootAdapter(fun), borders[0], borders[1], start); if (checkRoot(fun, root)) { //System.out.println("2. Brent worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } // try Newton's method RealRootDerivFunction derivFun = fun.getRealRootDerivFunction(); if (derivFun != null) { // check if fun(start) is defined double eval = fun.evaluate(start); if (Double.isNaN(eval) || Double.isInfinite(eval)) { // shift left border slightly right borders[0] = 0.9 * borders[0] + 0.1 * borders[1]; start = (borders[0] + borders[1]) / 2; } if (rootFinderNewton == null) { rootFinderNewton = new NewtonSolver(); } try { root = rootFinderNewton.solve(MAX_ITERATIONS, new RealRootDerivAdapter(derivFun), borders[0], borders[1], start); if (checkRoot(fun, root)) { //System.out.println("Newton worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } } // neither Brent nor Newton worked return Double.NaN; }
From source file:geogebra.common.kernel.algos.AlgoRootNewton.java
public final double calcRoot(Function fun, double start) { double root = Double.NaN; if (rootFinderBrent == null) rootFinderBrent = new BrentSolver(Kernel.STANDARD_PRECISION); // try Brent method with borders close to start value try {//from w ww . j a v a2 s . c o m // arbitrary (used to depend on screen width) double step = 1; root = rootFinderBrent.solve(MAX_ITERATIONS, new RealRootAdapter(fun), start - step, start + step, start); if (checkRoot(fun, root)) { // System.out.println("1. Brent worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } // try Brent method on valid interval around start double[] borders = getDomain(fun, start); try { root = rootFinderBrent.solve(MAX_ITERATIONS, new RealRootAdapter(fun), borders[0], borders[1], start); if (checkRoot(fun, root)) { // System.out.println("2. Brent worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } // try Newton's method RealRootDerivFunction derivFun = fun.getRealRootDerivFunction(); if (derivFun != null) { // check if fun(start) is defined double eval = fun.evaluate(start); if (Double.isNaN(eval) || Double.isInfinite(eval)) { // shift left border slightly right borders[0] = 0.9 * borders[0] + 0.1 * borders[1]; start = (borders[0] + borders[1]) / 2; } if (rootFinderNewton == null) { rootFinderNewton = new NewtonSolver(); } try { root = rootFinderNewton.solve(MAX_ITERATIONS, new RealRootDerivAdapter(derivFun), borders[0], borders[1], start); if (checkRoot(fun, root)) { // System.out.println("Newton worked: " + root); return root; } } catch (Exception e) { root = Double.NaN; } } // neither Brent nor Newton worked return Double.NaN; }
From source file:uk.ac.diamond.scisoft.analysis.fitting.EllipseFitterTest.java
@Test public void testOrthoDist() { AngleDerivativeFunction angleDerivative = new AngleDerivativeFunction(); BrentSolver solver = new BrentSolver(BrentSolver.DEFAULT_ABSOLUTE_ACCURACY); double a = 10.2; double b = 3.1; final double twopi = 2 * Math.PI; double alpha = twopi * 10. / 360; // 0 to 2 pi angleDerivative.setRadii(a, b);// w ww.j a va2s.com angleDerivative.setAngle(alpha); // final double ca = 0; // final double cb = b-0.5; final double Xc = -5.; // Math.cos(alpha)*ca + Math.sin(alpha)*cb; final double Yc = 5.5; //Math.sin(alpha)*ca + Math.cos(alpha)*cb; angleDerivative.setCoordinate(Xc, Yc); try { // find quadrant to use double p = Math.atan2(Yc, Xc); if (p < 0) p += twopi; p -= alpha; final double end; final double halfpi = 0.5 * Math.PI; p /= halfpi; end = Math.ceil(p) * halfpi; final double angle = solver.solve(BrentSolver.DEFAULT_MAXIMUM_ITERATIONS, angleDerivative, end - halfpi, end); // final double cos = Math.cos(angle); // final double sin = Math.sin(angle); Assert.assertEquals("Angle found is not close enough", 1.930, angle, 0.001); // double dx = a*cos + Xc; // double dy = b*sin + Yc; // // System.out.println("Bracket angle = " + Math.ceil(p)); // System.out.println("Delta angle = " + 180.*angle/Math.PI); // System.out.println(dx + ", " + dy); } catch (MaxIterationsExceededException e) { // TODO System.err.println(e); } catch (FunctionEvaluationException e) { // TODO System.err.println(e); } }