/* This program is supposed to perform a bracketed Newton's method by doing an interval bisection step if the Newton iteration would leave the interval. The code written here doesn't work and needs debugging. */ #include #include #include double g(double x){ return tan(x)-0.5*exp(x); } double dg(double x){ double t1=cos(x); return 1/(t1*t1)-0.5*exp(x); } double bnewton(double a,double b){ double ga=g(a),gb=g(b); if(ga*gb>0){ printf("Error! g(a) and g(b) same sign!\n"); exit(1); } double x0=(a+b)/2; for(int i=0;i<200;i++){ double y0=g(x0),x1=x0-y0/dg(x0); if(x1>b || x10){ if(ga>0) a=x0; else b=x0; } else { if(gb>0) a=x0; else b=x0; } x0=(a+b)/2; } else { x0=x1; } printf("a=%g x0=%g b=%g\n",a,x0,b); } return x0; } int main(){ double z=bnewton(3.2/2,3*3.141/2); printf("z=%.15g\n",z); return 0; }