Pages

This blog is under construction

Wednesday, December 19, 2018

C program to find the roots of an equation,using Newton Raphson method

Newton-Raphson Method:

The Newton-Raphson method is a powerful and elegant method to find the root of  an equation. This method is generally used to improve the results obtained by the previous methods. Let x0 be an approximate root of  f(x)=0, and let x1=x0+h  be the correct root which implies that f(x1)=0. 
We use Taylor’s theorem and expand           
           

           
  This is called Newton-Raphson formula.

Example:
Apply Newton-Raphson method to find an approximate root,correct to three decimal places,of  the  equation
 x3-3x-5=0,which lies near x=2.

Solution:
   Here f(x)=x3-3x-5    f1(x)= 3x2-3 (differentiation w.r.t. x of f(x))
    
  The Newton-Raphson iterative formula is   


Since x3 and x4 are identical upto 3 places of decimal, we take x4=2.279 as the required root, correct to three places of the decimal.
      
 Notes on Newton’s Method
Convergence rate for Newton’s method is very high!!
Error estimates are very good (however will be case dependent on the form of the function f(x)
Newton’s method can find complex roots.

Problem with Newton’s Method
If the local min/max is selected as an initial guess

C Program:
#include<stdio.h>
float f(float x);
float f1(float x);

void main()
{ float a,b,c,d;
   int i,j,n, flag=0;
   printf("\nEnter the no. of iterations");
   scanf("%d",&n);
   //printf("\nEnter the interval");
   // scanf("%f%f",&a,&b);
  for(j=-5;j<=5;j++)
  {   a=j;b=j+1;
    if(f(a)*f(b)<0)
    {printf("\nInterval found between %fand%f",a,b);
     printf("\nn\tc\tf(c)");
     for(i=0;i<n;i++)
      { c=a-f(a)/f1(a);
        printf("\n%d%10.6f %10.6f",i+1,c,f(c));
        a=c;
      }
     printf("\nThe approximate root of equation is=%f",c);
    flag=1;
   }
  
}
if(flag==0)
 printf("\n Interval did not find between range");
}

float f(float x)
{ return(x*x*x-3*x-5);
}
float f1(float x)
{return(3*x*x-3); }

Output:

Enter the no. of iterations 4                                                                                                                                          
Interval found between 2.000000 and 3.000000                                                                                                            
 n        c           f(c)                                                                                                                                 
1  2.333333   0.703703                                                                                                                                 
2  2.280555   0.019351                                                                                                                                 
3  2.279020   0.000016                                                                                                                                 
4  2.279019   0.000001                                                                                                                         
The approximate root of equation is=2.279019 

No comments:

Post a Comment