/* ATK IV - Numerical Programming (2006) */ /* Excercise 6.1 - Bisection Method */ #include #include float bisect(float x1, float x2, float acc); float f(float x); int main(void){ float x; float x1=1.0; float x2=2.0; float acc=1.0e-5; x=bisect(x1,x2,acc); printf("%f\n",x); return 0; } float bisect(float x1, float x2, float acc){ float z1=x1; float z2=x2; float f1=f(x1); float f2=f(x2); float zm,fm; while(fabs(z2-z1)>acc){ zm=0.5*(z1+z2); fm=f(zm); if(f1*fm < 0.0){ z2=zm; f2=fm; } else{ z1=zm; f1=fm; } } return zm; } float f(float x){ return exp(x)-5.0;}