急急急!编程实现用二分法求解f(x)= x3+4x2 -10=0在〔1,2〕内的一个实根

来源:百度知道 编辑:UC知道 时间:2024/06/18 00:21:04
1.编程实现用二分法求解f(x)= x3+4x2 -10=0在〔1,2〕内的一个实根,且要求满足精度 。|Xn-X*|<1/2*1000
紧急求助。谢谢。

#include <iostream>
#include <cmath>
using namespace std;

double f(double x)
{
return x*x*x + 4*x*x - 10;
}

int main()
{
double t1 = 1,t2 = 2;
double t = (t1+t2)/2;

while (fabs(f(t)) >= 1.0/2/1000)
{
if(f(t1)*f(t)<=0)
{
t2=t;
t=(t1+t2)/2;
}
else if(f(t)*f(t2)<=0)
{
t1=t;
t=(t1+t2)/2;
}
}
cout<<t<<endl;
return 1;
}

grdgedrfrr