Find Root using Newton-Raphson method in C++

Newton-Raphson Method:

The Newton-Raphson method (also known as Newton’s method) is a way to quickly find a good approximation for the root of a real-valued function f(x) = 0. It uses the idea that a continuous and differentiable function can be approximated by a straight line tangent to it.
Newton-Raphson formula:
                   xn+1 = xn-f(xn)/f ‘(xn)

How it works:

Suppose you need to find the root of a continuous, differentiable function f(x), and you know the root you are looking for is near the point x = x_0. Then Newton’s method tells us that a better approximation for the root is

x_1 = x_0 – \frac{f(x_0)}{f'(x_0)}.

This process may be repeated as many times as necessary to get the desired accuracy. In general, for any x-value x_n, the next value is given by

x_{n+1} = x_n – \frac{f(x_n)}{f'(x_n)}.

Note: the term “near” is used loosely because it does not need a precise definition in this context. However, x_0 should be closer to the root you need than to any other root (if the function has multiple roots)

C++ program to find root using Newton-Raphson method:

#include <iostream>
#include <math.h>
using namespace std;
float fn(float x)
     return  pow(x,2)+(3*x)+1 ;
float de(float x)
     return  2*x + 3 ;
int main()
{   float a,e=0,z;
    cout<<"Enter Number ";
    {   e++;
        cout<<"The iterative "<<e<<" root is "<<z;    
    return 0;


$g++ -o main *.cpp
Enter Number The iterative 1 root is -0.333333
The iterative 2 root is -0.380952
The iterative 3 root is -0.381966

Please comment in case of any query, issues or concerns.


Leave a Comment