integrate method
Calculates the numerical value of the definite function integral between lowerBound and upperBound. Returns a Record object whose members are:
-
a
guesses
named field, which contains the list of values generated by the algorithm on each step; -
a
result
named field, which contains the evaluation of the integral in the[a, b]
interval.
Implementation
@override
({List<double> guesses, double result}) integrate() {
// Make sure to throw an exception if 'intervals' is odd.
if (intervals % 2 != 0) {
throw const NumericalIntegrationException(
'There must be an even number of partitions.',
);
}
// The 'step' of the algorithm.
final h = (upperBound - lowerBound) / intervals;
// Keeping track separately of the sums of the even and odd series.
var oddSum = 0.0;
var evenSum = 0.0;
// The list containing the various guesses of the algorithm.
final guesses = List<double>.filled(intervals, 0);
// The first iteration.
for (var i = 1; i < intervals; i += 2) {
oddSum += evaluateFunction(lowerBound + i * h);
guesses[i] = oddSum;
}
// The second iteration.
for (var i = 2; i < intervals - 1; i += 2) {
evenSum += evaluateFunction(lowerBound + i * h);
guesses[i] = evenSum;
}
// Returning the result.
final bounds = evaluateFunction(lowerBound) + evaluateFunction(upperBound);
return (
guesses: guesses,
result: (bounds + (evenSum * 2) + (oddSum * 4)) * h / 3,
);
}