public Secant(double x0, double x1, double tolerance, int maxIterations, F f) { this.x0 = x0; this.x1 = x1; this.tolerance = tolerance; this.maxIterations = maxIterations; this.currentI = 2; this.f = f; this.q0 = f.eval(x0); this.q1 = f.eval(x1); }
@Override public void step() { x = x1 - q1 * (x1 - x0) / (q1 - q0); if (currentI < maxIterations && finished != true) { if (Math.abs(x - x1) < tolerance) { foundSolution = true; finished = true; return; } currentI++; x0 = x1; q0 = q1; x1 = x; q1 = f.eval(x); } else finished = true; }
@Override public String printStep() { return String.format("\n%d %15f %15f", currentI, x1, f.eval(x1)); }