//Newton-Raphson Method
//Find the root of the equation x^3 - x - 11 = 0 using Newton-Raphson method.
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double f(double x); //function declaration
double f(double x)
{
double E = pow(x, 3.0) - x - 11;
return E;
}
double fprime(double x); //derivative function declation
double fprime(double x)
{
double dE = 3 * pow(x, 2.0) - 1;
return dE;
}
int main()
{
int iter = 0;
double x, x1, e, fx, fx1;
[Link](4);
[Link](ios::fixed);
cout << "Enter the initial guess: " << endl;
cin >> x1;
cout << "Enter the desired accuracy: " << endl;
cin >> e;
//table heading
cout << "iter" << setw(18) << "x{i}" << setw(18) << "x{i+1}" << setw(19) <<
"|x{i+1}-x{i}|" << endl;
cout << "----------------------------------------------------------------------"
<< endl;
//calculation
do
{
x = x1;
fx = f(x);
fx1 = fprime(x);
x1 = x - (fx / fx1);
iter++;
cout << iter << setw(18) << x << setw(18) << x1 << setw(18) << fabs(x1 - x)
<< endl;
} while (fabs(x1 - x) >= e);
cout << "The root of the eqaution is " << x1 << endl;
return 0;
}