急救,拜托各位高手,进来看看!
用二分法求方程在(-10,10)之间的根。2x3-4x2+3x-6=0
搜索更多相关的解决方案:
急救
----------------解决方案--------------------------------------------------------
#include<iostream>
#include<cmath>
const double EPSION = 0.00001; //精度控制
bool f(const double &x) {
return ( (2*x*x*x - 4*x*x + 3*x - 6) > 0);
}
void main()
{
double min = -10.0, max = 10.0, temp1, temp2;
do {
temp1 = (min+max)/2.0;
f(temp1)? max=temp1 : min=temp1;
temp2 = (min+max)/2.0;
f(temp2)? max=temp2 : min=temp2;
}while (fabs(temp1-temp2) > EPSION);
std::cout << "x = " << temp2 << std::endl;
}
//结果: x = 2
----------------解决方案--------------------------------------------------------