// Secant Method for the root of f(x)
// Analytically the solution is x^3-28 = 0
// => x = 28^(1/3) = 3.0365889718756625194208095785057...
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double f(double x) { return x*x*x-28.; } // The function f(x)

int main() {

  double  x2=3.5, x1=3.6, Tolerance=1.0E-12;
  cout << setprecision(18) << fixed;

  do {

    double Error = (x2-x1)/(f(x2)-f(x1))*f(x2); // Estimate the error
    cout << "Root = " << x2 << " Error = " << Error << endl;

    if ( abs(Error) < Tolerance ) break;        // Terminate if the tolerance is satisfied
    x1 = x2;                                    // Improve x1
    x2 = x2 - Error;                            // Improve x2

  } while(1);

}
