Tuesday, 21 April 2015

Newton_Rahphson

Newton Rahphson Method

Code :
 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 double f(double x)  
 {  
      return ((x*x*x)-x-11);  
 }  
 double df(double x)  
 {  
      return ((3*x*x)-1);  
 }  
 void main()  
 {  
      double a,b,x=0,eps;  
      int acc,i=0,temp;  
      clrscr();  
      printf("Equation : X^3-X-11");  
      printf("\nenter a and b:");  
      scanf("%lf %lf",&a,&b);  
      while(f(a)*f(b)>0)  
      {  
           printf("\nintervals are invalid");  
           printf("\nenter a and b:");  
           scanf("%lf %lf",&a,&b);  
      }  
      printf("\nenter accuracy upto 6:");  
      scanf("%d",&acc);  
      eps=pow(10,-(acc));  
      while(fabs(x-a)>eps)  
      {  
           x=a;  
           a=a-(f(a)/df(a));  
           printf("\ninteration no.:%d\tx:%lf",i++,a);  
      }  
      x=a*pow(10,acc);  
      temp=(int)x;  
      a=temp/pow(10,acc);  
      printf("\n\nrequired %dD accurate ans is %g",acc,a);  
      getch();  
 }  

Output:

No comments:

Post a Comment