/************************* Program 2.7 ****************************/ /* */ /************************************************************************/ /* Please Note: */ /* */ /* (1) This computer program is written by Tao Pang in conjunction with */ /* his book, "An Introduction to Computational Physics," published */ /* by Cambridge University Press in 1997. */ /* */ /* (2) No warranties, express or implied, are made for this program. */ /* */ /************************************************************************/ # include # include # define f(x) (exp(x)*log(x)-x*x) main() /* Root of f(x)=0 with the bisection method. Copyright (c) Tao Pang 1997. */ { int istep; double dl=1e-6; double a,b,x0,dx; a = 1.0; b = 2.0; dx = b-a; istep = 0; while (fabs(dx) > dl) { x0 = (a+b)/2.0; if ((f(a)*f(x0))<0) { b = x0; dx = b-a; } else { a = x0; dx = b-a; } istep = istep+1; } printf("%4d %16.8lf %16.8lf\n", istep,x0,dx); }