/************************* Program 2.6 ****************************/ /* */ /************************************************************************/ /* 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() /* This program uses the bisection method to find the root of f(x)=exp(x)*ln(x)-x*x=0. Copyright (c) Tao Pang 1997. */ { int istep; double dl=1e-6; double a,b,x0,x1,dx; a = 1; b = 2; dx = b-a; istep = 0; while (fabs(dx) > dl) { x0 = (a+b)/2; 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); }