Arnab_Manna

Bisection_method_to_find_Roots

Sep 16th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. //bisection method self done
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<math.h>
  5. #define e 0.00001
  6.  
  7. float f(float x,float a0,float a1,float a2,float a3)
  8. {
  9.     float f1=a0*x*x*x+a1*x*x+a2*x+a3;
  10.     return f1;
  11. }
  12. int main()
  13. {
  14.     float f0,f1,a,b,c;
  15.     int count=0;
  16.     float a0,a1,a2,a3;
  17.     float xg,xl,x0,x;
  18.     printf("enter the value of\na0(x)^3+a1(x)^2+a2x+a3\na0  a1  a2  a3\n");
  19.     scanf("%f   %f  %f  %f",&a0,&a1,&a2,&a3);
  20.     fflush(stdin);
  21.     //x=sqrt(pow((a1/a0),2)-(2*(a3/a0)));
  22.     //x=sqrt(pow((a2/a1),2)-(2*(a3/a1)));
  23.     if(a0==0.0)//x^2+x+i
  24.     x=sqrt(pow((a2/a1),2)-(2*(a3/a1)));
  25.     else if(a1==0.0)//x^3+x+i
  26.     x=sqrt(pow((a1/a0),2)-(2*(a3/a0)));
  27.     else //x^3+x^2+x+i
  28.     x=sqrt(pow((a1/a0),2)-(2*(a2/a0)));
  29.    
  30.        
  31.     a=x;
  32.     b=x*(-1);
  33.     if(a>b) // assuming the lower and upper limit within which the root lies
  34.     {xg=a;xl=b;}
  35.     else
  36.     {xg=b;xl=a;}
  37.    
  38.     do
  39.     {
  40.           c=(xl+xg)/2.0;//finding the median of the two values
  41.           f0=f(c,a0,a1,a2,a3);//finding the function with the median values
  42.           if(f0<0)
  43.           {
  44.             xl=c;
  45.           }
  46.           else
  47.           {
  48.             xg=c;
  49.           }
  50.           count++;
  51.     }while(fabs(xg-xl)>e);
  52.    
  53.     printf("the value x for which equation is 0  is \t %f",c);
  54.     printf("\nthe number of iterations required is ==%d",count);
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment