integrate method

  1. @override
IntegralResults integrate(
  1. String function
)
override

Calculates the numerical value of the function definite integral between lowerBound and upperBound.

Implementation

@override
IntegralResults integrate(String function) {
  // The 'step' of the algorithm
  final h = (upperBound - lowerBound) / intervals;

  // The initial approximation of the result
  var integralResult = evaluateFunction(function, lowerBound) +
      evaluateFunction(function, upperBound);

  // The list containing the various guesses of the algorithm
  final guesses = List<double>.filled(intervals, 0);

  // The actual algorithm
  for (var i = 0; i < intervals; ++i) {
    final x = lowerBound + i * h;

    integralResult += 2 * evaluateFunction(function, x);
    guesses[i] = integralResult;
  }

  return IntegralResults(
    guesses: guesses,
    result: integralResult * h / 2,
  );
}