Advertisement
luizaspan

Bisection method II

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