Search code examples
javamathintegral

How do I integrate in java?


I'm working on a cumulative distribution function and I need to be able to solve integrals in Java.

I've already tried Math3 in Apache Commons, but I cannot figure that out either.

The type of integral I need to solve is for an Integral from a lower bound to an upper bound, and I need it to integrate for a variable, say 'T'.

The rest I can figure out, like using the variable in an equation. But when I tried the Simpson Integration from Math3 there were either 2 parameters or 4 parameters in the constructors. Using these:

// Construct an integrator with default settings.
SimpsonIntegrator()

// Build a Simpson integrator with given accuracies and iterations counts.
SimpsonIntegrator(double relativeAccuracy, double absoluteAccuracy, 
    int minimalIterationCount, int maximalIterationCount)

// Build a Simpson integrator with given iteration counts.
SimpsonIntegrator(int minimalIterationCount, int maximalIterationCount)

I also don't know what it means by the accuracy. And I'm not very good at Integrals, because I'm just using it for an equation to put on my calculator on the google play store.


Solution

  • There is a true value I for your integral. And there are the Simpson results S(n) for the numerical integration with 2n samples.

    The absolute accuracy bounds the error

    abs(S(n)-I)
    

    the relative accuracy is a bound for

    abs(S(n)-I)/abs(I).
    

    The absolute error doubles when doubling the integrand, the relative error remains invariant under such scalings. Both error types have their uses, for displaying purposes the relative error should be more important since it gives the number of valid digits. For instance, if the result is 0.01, then an absolute error of 0.01 gives a result between 0.00 and 0.03. A relative error of 0.1 in the same case restricts the numerical result between 0.009 and 0.011.


    The other two numbers give the starting and bailout value for n (or 2n).


    Since I is not known, its value and the errors have to be estimated. For that it is used that Simpson has error order 4, which means that asymptotically

    S(n) = I + C·n^(-4)+O(n^(-6))
    

    so that

    16·S(2n)-S(n) = 15·I + O(n^(-6))
    

    gives an improved value for I and

    16·n^4·(S(n)-S(2n)) = 15·C + O(n^(-2))
    

    gives an estimate on the coefficient C which allows then to get a good estimate on the optimal n required to meet the error bounds.

    C*nopt^(-4) <= min(absacc, relacc*abs(I))
    
    nopt = ceil( pow(C/min(absacc, relacc*abs(I)), 0.25) )
    

    So compute first approximations with the minimal n, then compute the optimal n (if smaller than the maximal n) and confirm the first error estimates. If that fails, compute a new optimal n with the new approximations.