0% found this document useful (0 votes)
255 views1 page

Newton-Raphson Method C++

This document presents a C++ program that implements the Newton-Raphson method to find the root of the equation x^3 - x - 11 = 0. It includes function declarations for the equation and its derivative, user input for initial guess and desired accuracy, and a loop to iteratively calculate the root while displaying the results. The program concludes by outputting the calculated root of the equation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views1 page

Newton-Raphson Method C++

This document presents a C++ program that implements the Newton-Raphson method to find the root of the equation x^3 - x - 11 = 0. It includes function declarations for the equation and its derivative, user input for initial guess and desired accuracy, and a loop to iteratively calculate the root while displaying the results. The program concludes by outputting the calculated root of the equation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//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;
}

You might also like