Example usage for org.apache.commons.math3.analysis.solvers NewtonRaphsonSolver solve

List of usage examples for org.apache.commons.math3.analysis.solvers NewtonRaphsonSolver solve

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.solvers NewtonRaphsonSolver solve.

Prototype

@Override
public double solve(int maxEval, final UnivariateDifferentiableFunction f, final double min, final double max)
        throws TooManyEvaluationsException 

Source Link

Document

Find a zero near the midpoint of min and max .

Usage

From source file:curveavg.MedialAxisTransform.java

/**
 * Uses the NewtonRaphson algorithm iteratively to span the range [0,1] and
 * find all the real roots.//from w  w  w. j a v  a2 s  .  com
 */
public static SolverResult solveQuinticNewtonRaphson(float a, float b, float c, float d, float e, float f) {

    // Initialize the result
    System.out.println("quintic params: " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f);
    SolverResult res = new SolverResult();
    res.re = new float[6];
    res.im = new float[6];
    for (int i = 0; i < 6; i++) {
        res.im[i] = 1.0f;
    }

    // Create the quintic function and the solver
    QuinticFunction qf = new QuinticFunction(a, b, c, d, e, f);
    NewtonRaphsonSolver solver = new NewtonRaphsonSolver();

    // Iteratively call the NewtonRaphson solution until no solution 
    // exists
    double minBound = 0.0;
    double sol;
    int root_idx = 0;
    while (true) {

        // Attempt to get the solution
        try {
            sol = solver.solve(100, qf, minBound, 1.0);
        } catch (TooManyEvaluationsException | NumberIsTooLargeException exc) {
            break;
        }

        // Check if the solution is within the bounds (shouldn't be 
        // necessary but it is...)
        if (sol < minBound || sol > 1) {
            break;
        }

        // Save the root and update the minimum bound
        res.re[root_idx] = (float) sol;
        res.im[root_idx] = 0.0f;
        minBound = sol + 1e-3;
        root_idx++;
    }

    System.out.println("#quintic roots: " + root_idx);
    return res;
}