simpsFunction function
Compute the numerical integration of f() using the Simpson's rule.
a
: start interval of the integrationb
: final interval of the integrationn
: number of the points between the intervalf
: function to integrate
References
- "Numerical integration". https://rosettacode.org/wiki/Numerical_integration#Java. Retrieved 2019-07-31.
Examples
var i = simpsFunction(0, 2*pi, 20, (x) => cos(x));
print(truncate(i, 4));
/* output:
0
*/
Implementation
double simpsFunction(double a, double b, int n, Function f) {
var range = checkParamsGetRange(a, b, n);
var nFloat = n.toDouble();
double sum1 = f(a + range / (nFloat * 2.0));
var sum2 = 0.0;
for (var i = 1; i < n; i++) {
var x1 = a + range * (i.toDouble() + 0.5) / nFloat;
sum1 += f(x1);
var x2 = a + range * i.toDouble() / nFloat;
sum2 += f(x2);
}
return (f(a) + f(b) + sum1 * 4.0 + sum2 * 2.0) * range / (nFloat * 6.0);
}