数值方法二分法解非线性方程

解非线性方程y=x^3 +4*x^2-10在区间[1,2]的根,精确到小数点后第三位
严格按照科学出版社数值计算方法定义


#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;
}

}

在这里插入图片描述

发布了30 篇原创文章 · 获赞 9 · 访问量 1332

猜你喜欢

转载自blog.csdn.net/weixin_43625164/article/details/104613250
今日推荐