用二分法求方程x^3-6x-1=0在x=2附近的一个实根。要求迭代精度为10^(-8)。要求用C++语言编程!!!麻烦了!
1个回答
展开全部
#include <iostream>
#include <cmath>
using namespace std;
const double eps = 1e-9;
const double e = 1e-8;
double func(double x) {
return x*x*x-6*x-1;
}
int main() {
double x1, x2, x3;
x1 = 1.5;
x2 = 4;
x3 = (x1+x2)/2;
while (true) {
if (fabs(func(x3)) < e) break;
double a, b, c;
a = func(x1);
b = func(x2);
c = func(x3);
if (a*c < 0) x2 = x3-eps;
else if (b*c < 0) x1 = x3+eps;
x3 = (x1+x2)/2;
}
printf("%.8lf", x3);
system("pause");
return 0;
}
#include <cmath>
using namespace std;
const double eps = 1e-9;
const double e = 1e-8;
double func(double x) {
return x*x*x-6*x-1;
}
int main() {
double x1, x2, x3;
x1 = 1.5;
x2 = 4;
x3 = (x1+x2)/2;
while (true) {
if (fabs(func(x3)) < e) break;
double a, b, c;
a = func(x1);
b = func(x2);
c = func(x3);
if (a*c < 0) x2 = x3-eps;
else if (b*c < 0) x1 = x3+eps;
x3 = (x1+x2)/2;
}
printf("%.8lf", x3);
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询