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.
No comments:
Post a Comment