用二分法求下面方程在(-10,10)的跟:2x^3-x^2+3x-6=0用c++
时间: 2024-06-06 19:08:29 浏览: 155
用C++实现二分法的线性方程求解
5星 · 资源好评率100%
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return 2 * pow(x, 3) - pow(x, 2) + 3 * x - 6;
}
int main() {
double left = -10;
double right = 10;
double mid;
const double EPSILON = 0.0001;
while (right - left > EPSILON) {
mid = (left + right) / 2;
if (f(mid) == 0) {
break;
} else if (f(mid) * f(left) < 0) {
right = mid;
} else {
left = mid;
}
}
cout << "The root is " << mid << endl;
return 0;
}
阅读全文