Advertisement
Sheyshya

bisection 2

Dec 9th, 2019
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define EPS 0.005
  4.  
  5. double func(double x)
  6. {
  7.     return x*x*x-2*x-5;
  8. }
  9.  void bisection(double a, double b)
  10. {
  11.     double e;
  12.     if (func(a) * func(b) > 0)
  13.     {
  14.         e=func(a)*func(b);
  15.         printf("%f\n",e);
  16.         printf("You have not assumed right xn and xp as func(xn) * func(xp)>0 \n");
  17.         return;
  18.     }
  19.  
  20.     double c;
  21.      printf("a\tb\tc\tfm\te\n");
  22.     while ((b-a) >= EPS)
  23.     {
  24.         // Find middle point
  25.         double e;
  26.        double d;
  27.  
  28.  
  29.  
  30.         c = (a+b)/2;
  31.  d=((b-a)/(a));
  32.   e=func(c);
  33.  
  34.    printf("%.4f\t %.4f\t %.4f\t %.4f\t %.4f\t",a,b,c,e,d);
  35.  
  36.         // Check if middle point is root
  37.         if (func(c) == 0.0)
  38.             break;
  39.  
  40.         // Decide the side to repeat the steps
  41.         else if (func(c) < 0)
  42.            a = c;
  43.         else
  44.            b= c;
  45.  
  46.          printf("\n");
  47.  
  48.  
  49.     }
  50.    printf("\nThe value of root is : %.4f\n",c );
  51.  
  52. }
  53. int main()
  54. {
  55.     // Initial values assumed
  56. int x1,x2;
  57. scanf("%d %d",&x1,&x2);
  58.  
  59.  
  60.  
  61.  
  62.      bisection(x1, x2);
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement