Newton Raphson Method



 Algorithm : Newton Raphson Method
Newton_raphson(a,b)
{
Let us consider a function f(x)=0.This algorithm finds a root of f(x)=0 between a and b.

error=10-6
if(f(a)*f(b)>0)				 //f(a) is a function
	{				 which returns the value of f(x) at x=a   
	  print “Root not lie between [a,b]” 
        }						    
	else
	{
	pi=a
	do
	  {
		pi+1=pi-(f(pi)/f’(pi)) //f’(pi) is a function 
		piprev=pi               which returns the value of d(f(x))/dx at x=pi

		pi=pi+1	
          }
while(|pi-pi+1|>error)		   
	print “pi”
  }
}


C Program For Newton Raphson Method

Source Code:

#include<stdio.h>
#include<conio.h>
#include<math.h>

void newton(float);
float f(float);
float der(float);
void main()
{
 int i;
 clrscr();
 printf("\nThe equation is : X^3-9X+1=0\n\n");
 for(i=-32768;i<32767;i++)
 {
  if(f(i)*f(i+1)<0)
 newton(i);
 }
 getch();
}

void newton(float a)
{
 float error,c,Cprev,FA;
 error=0.000001;
 printf("a         f(a)         der(a)        c        ");
 printf("\n-----------------------------------------------------");
  c=a-(f(a)/der(a));
  do
  {
   FA=f(a);
printf("\n%f   %f   %f   %f   ",a,FA,der(a),c);
    a=c;
    c=a-(f(a)/der(a));
   }while(fabs(a-c)>error);
   printf("\nThe root is %f\n",c);
 }

float f(float x)
{
 return (x*x*x-9*x+1);
}
float der(float m)
{
 return  (3*m*m-9);
}


 OUTPUT :

The equation is : X^3-9X+1=0  
                                                  

a         f(a)         der(a)        c                                          
-----------------------------------------------------                           
-4.000000   -27.000000   39.000000   -3.307692                                  
-3.307692   -5.419663   23.822485   -3.080190                                   
-3.080190   -0.501813   19.462715   -3.054407                                   
-3.054407   -0.006126   18.988204   -3.054084  
                                 
The root is -3.054084  
                                                         
a         f(a)         der(a)        c                                          
-----------------------------------------------------                           
0.000000   1.000000   -9.000000   0.111111                                      
0.111111   0.001372   -8.962963   0.111264  
                                    
The root is 0.111264  
                                                          
a         f(a)         der(a)        c                                          
-----------------------------------------------------                           
2.000000   -9.000000   3.000000   5.000000                                      
5.000000   81.000000   66.000000   3.772727                                     
3.772727   20.744459   33.700413   3.157172                                     
3.157172   4.055310   20.903209   2.963168                                      
2.963168   0.349182   17.341092   2.943032                                      
2.943032   0.003596   16.984308   2.942820 
                                     
The root is 2.942820                                                            
                                                                                


Share:

0 comments