trapzFunction function
Compute the numerical integration of f() using the trapezoidal 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 = trapzFunction(0, 2*pi, 20, (x) => cos(x));
print(truncate(i, 4));
/* output:
0
*/
Implementation
double trapzFunction(double a, double b, int n, Function f) {
var range = checkParamsGetRange(a, b, n);
var nFloat = n.toDouble();
var sum = 0.0;
for (var i = 1; i < n; i++) {
var x = a + range * i.toDouble() / nFloat;
sum += f(x);
}
sum += (f(a) + f(b)) / 2.0;
return sum * range / nFloat;
}