Monday, March 9, 2015

Definite integrals calculation in Java

IntegralApproximations.java

A class containing methods to calculate definite integrals and error margins.

Supports:


Example: Calculating the integral of sine in [0,pi/2] using 2 strips.

int n = 2;

double[] x = new double[n + 1];

double[] y = new double[n + 1];

x[0] = 0.28559933214452704;

x[1] = 0.8567979964335803;

x[2] = 1.4279966607226338;

for (int i = 0; i < n + 1; i++) {

    y[i] = (double) Math.round(Math.sin(x[i]) * 1000000) / 1000000;

}

System.out.println("Trapezoidal approximation : " + trapezoidal(x, y));

System.out.println("Simpson approximation     : " + simpson(x, y));

Calculating the error

double e1, e2;

double[] xe = new double[199];

double[] ye = new double[199];

double start = 0;

double dif = x[0] / 198;

for (int i = 0; i < 199; i++) {

    xe[i] = start;

    start += dif;

    ye[i] = Math.sin(xe[i]);

}

e1 = simpson(xe, ye) + simpError(x[0], 198, Math.sin(x[0]));

start = x[2];

dif = ((Math.PI / 2) - x[2]) / 198;

for (int i = 0; i < 199; i++) {

    xe[i] = start;

    start += dif;

    ye[i] = Math.sin(xe[i]);

}

e2 = simpson(xe, ye) + simpError(Math.PI / 2 - x[2], 198, 1);

System.out.println("Maximum theoretical error (trapezoidal) : " + (e1 + e2 + trapError(x[2] - x[0], n, Math.sin(x[2]))));

System.out.println("Actual error (trapezoidal) : " + (1 - trapezoidal(x, y)));

System.out.println("Maximum theoretical error (simpson)     : " + (e1 + e2 + simpError(x[2] - x[0], n, Math.sin(x[2]))));

System.out.println("Actual error (simpson)     : " + (1 - simpson(x, y)));

Output

Trapezoidal approximation : 0.7948383637221537

Simpson approximation     : 0.8176811695057372

Maximum theoretical error (trapezoidal) : 0.21356634499073798

Actual error (trapezoidal) : 0.20516163627784634

Maximum theoretical error (simpson)     : 0.18349059382360064

Actual error (simpson)     : 0.18231883049426278

Simpson's rule in Java

Wikipedia

Calculating the integral:


/**

*
@param x ascending order,odd number of points
@param y
@return
*/

static public double simpson(double[] x, double[] y) {
    double s1 = 0, s2 = 0;
    double dif = x[x.length - 1] - x[0];
    int n = x.length - 1;
    if (n % 2 != 0) {
        throw new IllegalArgumentException();
    }
    for (int i = 1; i <= (n * 0.5) - 1; i++) {
        s1 += y[2 * i];
    }
    s1 *= 2;
    for (int i = 1; i <= n * 0.5; i++) {
        s2 += y[(2 * i) - 1];
    }
    s2 *= 4;
    return ((dif / (3 * n)) * (y[0] + y[n] + s1 + s2));

}

Calculating the maximum theoretical error:


/**

*
@param dif total length of the strips
@param n number of strips
@param max max value of the 4nd derivative
@return
*/

static public double simpError(double dif, int n, double max) {
    return ((Math.pow(dif, 5) / (180 * n * n * n * n)) * max);
}

This is part of the Definite Integral Approximations project.

Trapezoidal rule in Java

Wikipedia

Calculating the integral:


/**

*

* @param x ascending order

* @param y

* @return

*/

static public double trapezoidal(double[] x, double[] y) {

    double s = 0;

    double dif = x[x.length - 1] - x[0];

    int n = x.length - 1;

    for (int i = 1; i <= n - 1; i++) {

        s += y[i];

    }

    s *= 2;

    return (dif / (2 * n)) * (y[0] + y[n] + s);

}

Calculating the maximum theoretical error:


/**

*

* @param dif total length of the strips

* @param n number of strips

* @param max max value of the 2nd derivative

* @return

*/

static public double trapError(double dif, int n, double max) {

    return ((Math.pow(dif, 3) / (12 * n * n)) * max);

}

This is part of the Definite Integral Approximations project.