作业帮 > 数学 > 作业

二分法 方程 x3+5=o的近似值,精确度为0.1

来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:数学作业 时间:2024/07/05 00:27:20
二分法 方程 x3+5=o的近似值,精确度为0.1
二分法 方程 x3+5=o的近似值,精确度为0.1
#include
#include
float f(float x)
{
float y;
y=2*5-4*2+3*x-6;//自定义方程
return(y);
}
float xpoint(float x1,float x2)
{
float y;
y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));//求 x轴点 交点坐标
return(y);
}
float root(float x1,float x2)
{
float x,y,y1;
y1=f(x1);
do
{
x=xpoint(x1,x2);
y=f(x);
if(y*y1>0)
{
y1=y;
x1=x;
}
else x2=x;
}
while(fabs(y)>=1e-2);
return(x);
}
void main(){
float x1,x2,f1,f2,x;
do
{
printf("请输入方程解得范围x1,x2(注意中间用逗号隔开):\n");
scanf("%f,%f",&x1,&x2);
f1=f(x1);
f2=f(x2);
}
while(f1*f2>=0);
x=root(x1,x2);
printf("A root of equation is %.1f\n",x);
}