solve method

  1. @override
({double convergence, double efficiency, List<double> guesses}) solve()
override

Generates the succession generated by the root-finding algorithm. 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 convergence named field, whose value represents the convergence rate for the generated succession (computed using convergence).

  • a efficiency named field, whose value represents the efficiency of the algorithm (computed using efficiency).

Implementation

@override
({List<double> guesses, double convergence, double efficiency}) solve() {
  // Exit immediately if the root is not bracketed
  if (evaluateOn(a) * evaluateOn(b) >= 0) {
    throw NonlinearException('The root is not bracketed in [$a, $b]');
  }

  final guesses = <double>[];
  var toleranceCheck = true;
  var n = 1;

  var tempA = a;
  var tempB = b;

  while (toleranceCheck && (n <= maxSteps)) {
    // Evaluating on A and B the function
    final fa = evaluateOn(tempA);
    final fb = evaluateOn(tempB);

    // Computing the guess
    final c = (fa * tempB - fb * tempA) / (fa - fb);
    final fc = evaluateOn(c);

    // Making sure the evaluation is not zero
    if (fc == 0) {
      break;
    }

    // Shrink the interval
    if (fa * fc < 0) {
      tempB = c;
    } else {
      tempA = c;
    }

    // Add the root to the list
    guesses.add(c);

    toleranceCheck = fc.abs() > tolerance;
    ++n;
  }

  return (
    guesses: guesses,
    convergence: convergence(guesses, maxSteps),
    efficiency: efficiency(guesses, maxSteps),
  );
}