数学问题!!用二分法求方程X^5-3X+1=0在(0,1)上的近似解,精确到C=0.001,写出算法算法如下:#include<iostream.h&gt

数学问题!!用二分法求方程X^5-3X+1=0在(0,1)上的近似解,精确到C=0.001,写出算法算法如下:#include<iostream.h>
算法如下:
#include
#include

double f(double x)
{
return x*x*x*x*x-3*x+1; //方程
}

void main()
{
double e=1e-3; //精确值
double start,end,mid;
cout<<"输入二分法的起始最小值和最大值"< cin>>start;
cin>>end;
while(f(start)*f(end)>0)
{
cout<<"f(start)*f(end)必须小于0!请重新输入二分法的起始最小值和最大值"< cin>>start;
cin>>end;
}

if(fabs(f(start))<=e)
{cout<<"求出的值为x="< else if(fabs(f(end))<=e)
{cout<<"求出的值为x="< else
{
mid=(start+end)/2;
while(fabs(f(mid))>e)
{
if(f(start)*f(mid)<0)
{
end=mid;
}
else
{
start=mid;
}
mid=(start+end)/2;
}
cout<<"求出的值为x="< }
}

ps:运行程序,输入起始值0和1,求出了x=0.334961

← 上一篇: 下一篇: →