solve method
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 n = 1;
var x0 = a;
var x1 = b;
var y0 = evaluateOn(x0);
var y1 = evaluateOn(x1);
while (n <= maxSteps) {
final x2 = (x0 + x1) / 2;
final y2 = evaluateOn(x2);
// The guess on the n-th iteration
final x = x2 + (x2 - x0) * (y0 - y1).sign * y2 / sqrt(y2 * y2 - y0 * y1);
// Add the root to the list
guesses.add(x);
// Tolerance
if (min<double>((x - x0).abs(), (x - x1).abs()) < tolerance) {
break;
}
final y = evaluateOn(x);
// Fixing signs
if (y2.sign != y.sign) {
x0 = x2;
y0 = y2;
x1 = x;
y1 = y;
} else {
if (y1.sign != y.sign) {
x0 = x;
y0 = y;
} else {
x1 = x;
y1 = y;
}
}
++n;
}
return (
guesses: guesses,
convergence: convergence(guesses, maxSteps),
efficiency: efficiency(guesses, maxSteps),
);
}