c++求方程f(x)=2^x+3^x-4^x=0在[1,2]内的根,精确到10位小数
时间: 2024-01-05 21:21:09 浏览: 156
以下是使用C++求解方程f(x) = 2^x + 3^x - 4^x = 0在[1, 2]内的根,精确到10位小数的代码示例:
```cpp
#include <iostream>
#include <cmath>
double func(double x) {
return pow(2, x) + pow(3, x) - pow(4, x);
}
double bisection(double left, double right) {
double middle;
while (fabs(right - left) > 1e-10 || fabs(func(middle)) > 1e-10) {
middle = (left + right) / 2;
if (func(middle) * func(right) < 0) {
left = middle;
}
else if (func(middle) * func(left) < 0) {
right = middle;
}
}
return right;
}
int main() {
double root = bisection(1, 2);
std::cout.precision(10);
std::cout << "The root of the equation f(x) = 2^x + 3^x - 4^x = 0 in the interval [1, 2] is: " << root << std::endl;
return 0;
}
```
阅读全文