Example usage for org.apache.commons.math.analysis.integration UnivariateRealIntegrator setRelativeAccuracy

List of usage examples for org.apache.commons.math.analysis.integration UnivariateRealIntegrator setRelativeAccuracy

Introduction

In this page you can find the example usage for org.apache.commons.math.analysis.integration UnivariateRealIntegrator setRelativeAccuracy.

Prototype

void setRelativeAccuracy(double accuracy);

Source Link

Document

Set the relative accuracy.

Usage

From source file:sphericalGeo.SphericalDiffusionModel.java

/** anglerange[0] = lower bound, anglerange[1] = upper bound of range for angle2
 * **//*from   w  w w. j a  va  2 s.c  om*/
public double[] sample(double[] start, double time, double precision, double[] angleRange)
        throws ConvergenceException, FunctionEvaluationException, IllegalArgumentException {

    // first, sample an angle from the spherical diffusion density
    final double inverseVariance = precision / time;

    UnivariateRealFunction function = new UnivariateRealFunction() {
        @Override
        public double value(double x) throws FunctionEvaluationException {
            double logR = -x * x * inverseVariance / 2.0 + 0.5 * Math.log(x * Math.sin(x) * inverseVariance);

            return Math.exp(logR);
        }
    };

    UnivariateRealIntegrator integrator = new TrapezoidIntegrator();
    integrator.setAbsoluteAccuracy(1.0e-10);
    integrator.setRelativeAccuracy(1.0e-14);
    integrator.setMinimalIterationCount(2);
    integrator.setMaximalIterationCount(25);

    double[] cumulative = new double[NR_OF_STEPS];
    for (int i = 1; i < NR_OF_STEPS; i++) {
        cumulative[i] = cumulative[i - 1]
                + integrator.integrate(function, (i - 1) * Math.PI / NR_OF_STEPS, i * Math.PI / NR_OF_STEPS);
    }

    // normalise 
    double sum = cumulative[NR_OF_STEPS - 1];
    for (int i = 0; i < NR_OF_STEPS; i++) {
        cumulative[i] /= sum;
    }

    int i = Randomizer.randomChoice(cumulative);
    double angle = i * Math.PI / NR_OF_STEPS;

    // now we have an angle, use this to rotate the point [0,0] over
    // this angle in a random direction angle2
    double angle2 = angleRange[0] + Randomizer.nextDouble() * (angleRange[1] - angleRange[0]);
    //angleRange[0] = angle2;

    double[] xC = new double[] { Math.cos(angle), Math.sin(angle) * Math.cos(angle2),
            Math.sin(angle) * Math.sin(angle2) };

    // convert back to latitude, longitude relative to (lat=0, long=0)
    double[] xL = cartesian2Sperical(xC, true);
    //double [] sC = spherical2Cartesian(start[0], start[1]);
    double[] position = reverseMap(xL[0], xL[1], start[0], start[1]);
    return position;
}