/* * To parakatw programma ypologizei ena diasthma [a,b] mhkous * to poly tol to opoio periexei th riza ths exiswshs f(x)=0, * xrhsimopoiwntas th me8odo ths dixotomhshs. To programma * ypo8etei oti f(a)f(b) < 0. */ #include double f(double x) { return -5.0 - x*(2.0 - x*x); } int main() { double a = 2.0, b = 3.0, tol = 1.0e-6; int n; double h, c, fa, fb, fc; h = b - a; n = 1; fa = f(a); fb = f(b); while (h > tol) { c = (a + b) / 2.0; if (c == a || c == b) break; fc = f(c); if (fc == 0.0) break; if (fb * fc < 0) { a = c; fa = fc; } else { b = c; fb = fc; } printf("%2d %16.9e %16.9e %16.9e\n", n, c, fc, h); h *= 0.5; n++; } printf("\nRoot : %16.9e\n", (a + b)/2.0); printf("Length : %16.9e\n", h); return 0; }