描述
有函数:
f(x) = x5 - 15 * x4+ 85 * x3- 225 * x2+ 274 * x - 121
已知 f(1.5) > 0 , f(2.4) < 0 且方程 f(x) = 0 在区间 [1.5,2.4] 有且只有一个根,请用二分法求出该根。
输入无。输出该方程在区间[1.5,2.4]中的根。要求四舍五入到小数点后6位。
https://www.cnblogs.com/huashanqingzhu/p/6882823.html
/* 二分法求方程的根 */ #include<iostream> #include<cstdio> using namespace std; #define EPS 1e-6 double f(double x) { return x*x*x*x*x - 15 * x*x*x*x + 85 * x *x*x- 225*x*x+274*x-121; } int main() { double root, left = 1.5, right = 2.4,y; int times = 1; root = left + (right - left) / 2; y = f(root); while (fabs(y) > EPS) { if (y < 0) { right = root; } else { left = root; } root = left + (right - left) / 2; y = f(root); times++; } printf("%.8f\n", root); printf("%d\n", times); return 0; }