Pages

Tuesday, 21 April 2015

Secant

Secant Method

Code : 
 #include<stdio.h>  
 #include<conio.h>  
 #include<math.h>  
 double f(double x)  
 {  
      return ((x*x*x)-x-11);  
 }  
 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=b-(f(b)*((a-b)/(f(a)-f(b))));  
           printf("\ninteration no.:%d\tx:%lf",i++,x);  
           a=b;  
           b=x;  
      }  
      a=x*pow(10,acc);  
      temp=(int)a;  
      x=temp/pow(10,acc);  
      printf("\n\nrequired %dD accurate ans is %g",acc,x);  
      getch();  
 }  

Output :

No comments:

Post a Comment