V-Lab @ ANDC

Newton-Raphson method for finding roots of equations

Aim

For finding the roots of linear, quadratic, third degree polynomial and transcendental equations from Newton-Raphson Method

Theory

What is 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.

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 = x0. Then Newton's method tells us that a better approximation for the root is ----

Newton algo

Procedure

  1. Start the program.
  2. Enter the coefficients of the polynomial.
  3. Enter the iterations you want.
  4. Enter the guess
  5. x1= guess
  6. guess= x1- (f(x)/f'(x))
  7. Process will run until the desired iteration is reached.
  8. Terminate.

C++ Code

                        
                            // newton raphson method
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define    f(x)  x-tan(x)
#define   g(x)  1-(1/cos(x)*(1/cos(x)))
using namespace std;
int main()
{
	 double x0, x1, f0, f1, g0, e;
	 int step = 1, N;
	 //precision set(upto 6)
     //cout<< setprecision(6)<< fixed;
     cout<< endl<<"**"<< endl;
	 cout<<"Newton Raphson Method"<< endl;
	 cout<<"**"<< endl; cout<<"the equation is like a=tan(a) , here for instance, the equation in f(x)=x-tan(x)"<>x0;
	 cout<<"Enter error : ";
	 cin>>e;
	 cout<<"Enter maximum loops : ";
	 cin>>N;

	 do
	 {
		  g0 = g(x0);
		  f0 = f(x0);
		  if(g0 == 0.0)
		  {
			   cout<<"Mathematical Error.";
			   exit(0);
		  }
		  x1 = x0 - f0/g0;
		  cout<<"Iteration-"<< step<<":\t x = "<< setw(15)<< x1<<" and f(x) = "<< setw(15)<< f(x1)<< endl;
		  x0 = x1;
		  step = step+1;
		  if(step > N)
		  {
			   cout<<"Not Convergent.";
			   exit(0);
		  }
		  f0 = f(x1);
	 }while(fabs(f1)>=e);

	 cout<< endl<<"The Root is: "<< x1;
	 return 0;
}

}
                        
                    

Observations

Take Observations from the method and tabulate it for the given intervals.

Plot a graph also. (function vs root).

Result

Hence we findout the roots for the given polynomial.

It is best method to solve the non-linear equations. It can also be used to solve the system of non-linear equations, non-linear differential and non-linear integral equations. The order of convergence is quadric i.e. of second order which makes this method fast as compared to other methods. It is very easy to implement on computer.