Advertisement
luizaspan

Bisection method III

Aug 28th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. // para cosx=0, com a=0 e b=3 e epsilon
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. // #include <assert.h>
  6.  
  7. #define pi M_PI
  8. #define eps 1e-5
  9.  
  10. double bisec(double x)
  11. {
  12.     double f;
  13.     int m=5; //2310
  14.  
  15.     f=tan(x)-m*x;
  16.     return f;
  17. }
  18.  
  19. int main(void)
  20. {
  21.     double a=0.1, b=1.57, c, y;
  22.     int i;
  23.  
  24.     // assert(bisec(a)>0);
  25.     // assert(bisec(b)>0);
  26.  
  27.     while (fmax(fabs(bisec(a)),fabs(bisec(b)))>eps) // b-a
  28.     {
  29.         c=(a+b)/2;
  30.         y=bisec(c);
  31.  
  32.         if (y<0)
  33.             a=c;
  34.  
  35.         else if (y>0)
  36.             b=c;
  37.  
  38.         else if (y==0)
  39.             break;
  40.  
  41.         i++;
  42.     }
  43.  
  44.     printf("Raiz calculada: %e \nIterações = %d \n",c,i);
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement