Numerical method dichotomy to solve nonlinear equations

Solve the nonlinear equation y = x ^ 3 + 4 * x ^ 2-10 at the root of the interval [1,2], accurate to the third decimal place,
strictly in accordance with the numerical calculation method of Science Press


#include <iostream>
using namespace std;
#define WUCHA 0.0005//误差
double hanshu(double x) {//函数
	return x * x * x + x * x * 4.0 - 10.0;
}

int main()
{
	double a = 1;
	double b = 2;
	double midd;
	int count = 1;
	while ((b - a)/2 > WUCHA) {
		midd = (a + b) / 2;
		if (hanshu(midd) > 0.0) {
			b=midd;
		}
		else {
			a=midd;
		}
		count++;
		
	}
	cout << "结果:" << (a+b)/2 << endl;
	cout << "运算次数:" << count;
	return 0;
}

}

Insert picture description here

Published 30 original articles · won 9 · visited 1332

Guess you like

Origin blog.csdn.net/weixin_43625164/article/details/104613250