Example usage for org.apache.commons.math.analysis UnivariateRealSolver solve

List of usage examples for org.apache.commons.math.analysis UnivariateRealSolver solve

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis UnivariateRealSolver solve.

Prototype

double solve(double min, double max) throws ConvergenceException, FunctionEvaluationException;

Source Link

Document

Solve for a zero root in the given interval.

Usage

From source file:desmoj.core.dist.ContDistCustom.java

/**
 * Returns the next sample from this distribution. The value depends upon
 * the seed, the number of values taken from the stream by using this method
 * before and the alpha and beta parameters specified for this distribution.
 * //w  w  w  . j  ava  2s .  com
 * @return Double : The next gamma distributed sample from this
 *         distribution.
 */
public Double sample() {

    double newSample; //

    incrementObservations(); // increase count of samples

    randomNumber = randomGenerator.nextDouble();
    if (isAntithetic()) {
        randomNumber = 1 - randomNumber;

    }

    Function functiontosolve = new Function() {

        public double value(double x) {

            return distFunction.value(x) - randomNumber;
            // Decrease input function by randomNumber to make the desired
            // sample be at a zero root.
        }

        public String getDescription() {
            return null;
        }
    };

    UnivariateRealSolver solver = new BisectionSolver(functiontosolve);
    try {
        newSample = solver.solve(lowerBound, upperBound); // Finding zero
        // root

    } catch (Exception e) {
        sendWarning("Failed to find sample, returning -1",
                "CustomContDist : " + getName() + " Method: sample()",
                "The solver could not deal with the distribution function specified.",
                "Make sure the CustomFunction this Distribution is using is a proper "
                        + "distribution function and the upper and lower bounds are set accordingly");
        newSample = -1;
    }
    if (this.currentlySendTraceNotes())
        this.traceLastSample(Double.toString(newSample));
    return newSample;

}