#!/usr/local/bin/tcc -run #include #include /* 2 分法 */ double bisection(double a, double b, double eps); /* 関数の定義 */ double f(double x); int main(void){ double a, b, x, h, y1, y2, eps = pow(2.0, -30.0); int n; printf("** 初期区間 [a, b] の入力: "); scanf("%lf %lf", &a, &b); printf("** 区間の分割数 n の入力: "); scanf("%d", &n); /* 対象区間を探索しながら 2 分法を適用 */ h = (b-a)/n; y1 = f(a); for(x=a+h; x<=b; x+=h){ y2=f(x); if(y1*y2 < 0.0) printf("x = %lf\n", bisection(x-h, x, eps)); y1 = y2; } return 0; } /* 2 分法 */ double bisection(double a, double b, double eps){ double c; do { c = (a+b)/2.0; if(f(a)*f(c) < 0.0) b = c; else a = c; } while (fabs(b-a) >= eps); c = (a+b)/2.0;; return c; } /* 関数の定義 */ double f(double x){ return pow(x, 5.0) - 5*pow(x, 3.0)+4*x; }