用二分法求方程(x+1)(x-2)(x-3)=1在区间(-1,0)的近似解(精确度0.1)为什么在球

发布时间:2021-02-25 09:39:51

用二分法求方程(x+1)(x-2)(x-3)=1在区间(-1,0)的近似解(精确度0.1)为什么在球二分法的时候要先看看是单调增还是减呢?

网友回答

#include
#include
float f(float x)
{float y;
y=(x+1)*(x-2)*(x-3)-1;
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-1);
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);
}判断单调是为了保证在某一个区间只有唯一解.
以上问题属网友观点,不代表本站立场,仅供参考!