
用二分法求方程2x3-4x2+3x-6=0在x=1.5附近的实数根,精度ε=10-5。C++源程序
2个回答
展开全部
朋友,你这道题明显有bug。
你给出的方程2x3-4x2+3x-6可以验证就是一个单调递增函数。
而在x=1.5处 原式=-3.75根本找不到零点
其实经过我验证 完全的零点就在x=2处
下面给出我的程序代码:
#include<iostream.h>
#include<math.h>
double f(double x)
{
return 2*x*x*x-4*x*x+3*x-6;
}
void main()
{
double e=1e-5;
double start,end,mid;
cout<<"输入二分法的起始最小值和最大值"<<endl;
cin>>start;
cin>>end;
while(f(start)*f(end)>0)
{
cout<<"f(start)*f(end)必须小于0!请重新输入二分法的起始最小值和最大值"<<endl;
cin>>start;
cin>>end;
}
if(fabs(f(start))<=e)
{cout<<"求出的值为x="<<start<<endl;}
else if(fabs(f(end))<=e)
{cout<<"求出的值为x="<<end<<endl;}
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="<<mid<<endl;
}
}
ps:你输入0,、3.计算出来的结果就是x=2
ps:如果将原式改成2x4-4x2+3x-6=0在x=1.5附近我倒是找到个实根。
你给出的方程2x3-4x2+3x-6可以验证就是一个单调递增函数。
而在x=1.5处 原式=-3.75根本找不到零点
其实经过我验证 完全的零点就在x=2处
下面给出我的程序代码:
#include<iostream.h>
#include<math.h>
double f(double x)
{
return 2*x*x*x-4*x*x+3*x-6;
}
void main()
{
double e=1e-5;
double start,end,mid;
cout<<"输入二分法的起始最小值和最大值"<<endl;
cin>>start;
cin>>end;
while(f(start)*f(end)>0)
{
cout<<"f(start)*f(end)必须小于0!请重新输入二分法的起始最小值和最大值"<<endl;
cin>>start;
cin>>end;
}
if(fabs(f(start))<=e)
{cout<<"求出的值为x="<<start<<endl;}
else if(fabs(f(end))<=e)
{cout<<"求出的值为x="<<end<<endl;}
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="<<mid<<endl;
}
}
ps:你输入0,、3.计算出来的结果就是x=2
ps:如果将原式改成2x4-4x2+3x-6=0在x=1.5附近我倒是找到个实根。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询