现在的位置: 首页 > 综合 > 正文

二分法求方程根

2018年05月27日 ⁄ 综合 ⁄ 共 265字 ⁄ 字号 评论关闭
/*
 *方程: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;
}
【上篇】
【下篇】

抱歉!评论已关闭.