二分法求方程根

来源:互联网 发布:网络人工投票公司 编辑:程序博客网 时间:2024/05/02 01:39
/* *方程:2*x*x*x - 4*x*x + 3*x - 6 = 0; */#include <iostream>#include <cmath>using namespace std;double f(double x);int main(void){double x1 = -10, x2 = 10, x;    while(fabs(x1-x2)>=1e-6){x = (x1+x2)/2;if(f(x)*f(x1)<0){x2 = x;}else{x1 = x;}}cout << x1 << endl;}double f(double x){return 2*x*x*x - 4*x*x + 3*x - 6;}


原创粉丝点击