top of page

1  #include <stdio.h>
2  #include <math.h>
3  int main (void){
4     double eps;
5     double a,b,c;
6     double fa,fb,fc;
7     eps=1.e-6;         /* accuracy */
8     a=0;
9     b=1.5;
10    fa=sin(a)-.5;     /* function in equation */
11    fb=sin(b)-.5;
12
13    if(fa*fb>0){       /* root does not exist */
14       printf("Wrong interval a=%f, b=%f: fa=%f, fb=%f\n",a,b,fa,fb);
15       return -1;
16    }
17
18    while (fabs(b-a) > eps ) {
19       c=(a+b)/2.;
20       fc=sin(c)-.5;
21       printf(" c = %f fc = %f \n",c,fc);
22
23       if(fa*fc<0.){
24          b=c;
25          fb=fc;
26       }
27       else{
28          a=c;
29          fa=fc;
30       }
31    }
32    printf(" c = %f fc = %f \n",c,fc);
33    return 0;
34 }

  • b-facebook
  • Twitter Round
  • b-googleplus
bottom of page