C语言编程:内容:用二分法求一元三次方程的根,要求:由主函数调用求根子函数

 我来答
164zsq
2011-12-09 · TA获得超过467个赞
知道小有建树答主
回答量:486
采纳率:0%
帮助的人:441万
展开全部
代码如下,很完整
#include<stdio.h>
#include<math.h>
void main()
{
double x0,x1,xm,f0,f1,fm,x2,x3;//x2,x3是驻点,x0,x1,xm,f0,x1是二分法求根的工具。
double a[3],r[3];
int i,j=0;
printf("input 3 coefficients:\n");
for(i=0;i<3;i++)
{
printf("a[%d]=",i);
scanf("%lf",a+i);
}
printf("the function is:\n");
printf("x*x*x+%5.2f*x*x+%5.2f*x+%5.2f=0\n",a[0],a[1],a[2]);
//y=3*x*x+2*a[0]*x+a[1]

if(4*a[0]*a[0]-12*a[1]<0)//方程单调递增,与横轴只有一个交点。
{
printf("input 2 numbers as your will:\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
if(f0*f1==0)
{
if(f0==0)
{
xm=x0;
printf("the only root of the function is %.2f\n",xm);
}
else
{
xm=x1;
printf("the only root of the function is %.2f\n",xm);
}
if(fabs(xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2])<1e-6)
printf("comgratulations! the answer is right!\n");
else
printf("sorry, you should try again.\n");
}
else
{
while(f0*f1>=0)
{
printf("input 2 numbers again:\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
}

do
{
xm=(x0+x1)/2;
fm=xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2];
if(f0*fm>0)
x0=xm;
else
x1=xm;
}while(fabs(x0-x1)>1e-6);
xm=(x0+x1)/2;
printf("the only root of the function is %.2f\n",xm);
if(fabs(xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2])<1e-6)
printf("congratulations! the answer is right!\n");
else
printf("sorry, you should try again.\n");
}

}

else//方程有增有减,但与横轴的交点不确定。
{
x2=(-2*a[0]-sqrt(4*a[0]*a[0]-12*a[1]))/6;
x3=(-2*a[0]+sqrt(4*a[0]*a[0]-12*a[1]))/6;
printf("the stagnation of the function are:\n");
printf("x2=%.2f\nx3=%.2f\n",x2,x3);

if((x2*x2*x2+a[0]*x2*x2+a[1]*x2+a[2])>0&&(x3*x3*x3+a[0]*x3*x3+a[1]*x3+a[2])>0)
//方程左半单调递增支和横轴有交点。
{
printf("input 2 numbers to start the procedure, and one of the number should be x2, and the other should be smaller than x2. you will get just one root.\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
while(f0*f1>=0)
{
printf("input 2 numbers again:\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
}

do
{
xm=(x0+x1)/2;
fm=xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2];
if(f0*fm>0)
x0=xm;
else
x1=xm;
}while(fabs(x0-x1)>1e-6);
xm=(x0+x1)/2;
printf("the only root of the function is %.2f\n",xm);
if(fabs(xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2])<1e-6)
printf("congratulations! the answer is right!\n");
else
printf("sorry, you should try again.\n");
}

else if((x2*x2*x2+a[0]*x2*x2+a[1]*x2+a[2])<0&&(x3*x3*x3+a[0]*x3*x3+a[1]*x3+a[2])<0)
//方程右半单调递增支和横轴有交点
{
printf("input 2 numbers to start the procedure, and one of the number should be x3, and the other should be bigger than x3.you will get just one root.\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
while(f0*f1>=0)
{
printf("input 2 numbers again:\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
}

do
{
xm=(x0+x1)/2;
fm=xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2];
if(f0*fm>0)
x0=xm;
else
x1=xm;
}while(fabs(x0-x1)>1e-6);
xm=(x0+x1)/2;
printf("the only root of the function is %.2f\n",xm);
if(fabs(xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2])<1e-6)
printf("congratulations! the answer is right!\n");
else
printf("sorry, you should try again.\n");
}

else if((x2*x2*x2+a[0]*x2*x2+a[1]*x2+a[2])*(x3*x3*x3+a[0]*x3*x3+a[1]*x3+a[2])<0)//一般方程有三个交点,分别位于增、减、增区间。
{
printf("you will get 3 roots, type in 2 numbers 3 times, and in the first case, the bigger number you type in should be x2; and in the second case, the 2 numbers you type in should be x2 and x3, and in the third case, the smaller one should be x3.\n");
for(i=0;i<3;i++)
{
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
while(f0*f1>=0)
{
printf("input 2 numbers again:\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
}

do
{
xm=(x0+x1)/2;
fm=xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2];
if(f0*fm>0)
x0=xm;
else
x1=xm;
}while(fabs(x0-x1)>1e-6);
r[i]=(x0+x1)/2;
printf("Ok, next!\n");
}//三次循环找三个根。

printf("3 roots of the function are:\n");
for(i=0;i<3;i++)
printf("r[%d]=%.2f\n",i,r[i]);
for(i=0;i<3;i++)
if(fabs(r[i]*r[i]*r[i]+a[0]*r[i]*r[i]+a[1]*r[i]+a[2])<1e-6)
j++;
if(j==3)
printf("congratulations, the answer are all right!\n");
else
printf("sorry, you should try again.\n");

}

else
{
if(x2*x2*x2+a[0]*x2*x2+a[1]*x2+a[2]==0&&x3*x3*x3+a[0]*x3*x3+a[1]*x3+a[2]!=0)//x2是一个二重根
{
r[0]=r[1]=x2;
//补充剩下的
printf("input 2 numbers to start the procedure, and one of the number should be x3, and the other should be bigger than x3.you will get just one root.\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
while(f0*f1>=0)
{
printf("input 2 numbers again:\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
}

do
{
xm=(x0+x1)/2;
fm=xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2];
if(f0*fm>0)
x0=xm;
else
x1=xm;
}while(fabs(x0-x1)>1e-6);
r[2]=(x0+x1)/2;
printf("the 2 roots of the function are:\n");
for(i=0;i<3;i++)
printf("r[%d]=%.2f\n",i,r[i]);
for(i=0;i<3;i++)
if(fabs(r[i]*r[i]*r[i]+a[0]*r[i]*r[i]+a[1]*r[i]+a[2])<1e-6)
j++;
if(j==3)
printf("congratulations, the answer are all right!\n");
else
printf("sorry, you should try again.\n");
}

else if(x2*x2*x2+a[0]*x2*x2+a[1]*x2+a[2]!=0&&x3*x3*x3+a[0]*x3*x3+a[1]*x3+a[2]==0)
{
r[0]=r[1]=x3;
//补充剩下的
printf("input 2 numbers to start the procedure, and one of the number should be x2, and the other should be smaller than x2.you will get just one root.\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
while(f0*f1>=0)
{
printf("input 2 numbers again:\n");
printf("x0=");
scanf("%lf",&x0);
printf("x1=");
scanf("%lf",&x1);
f0=x0*x0*x0+a[0]*x0*x0+a[1]*x0+a[2];
f1=x1*x1*x1+a[0]*x1*x1+a[1]*x1+a[2];
}

do
{
xm=(x0+x1)/2;
fm=xm*xm*xm+a[0]*xm*xm+a[1]*xm+a[2];
if(f0*fm>0)
x0=xm;
else
x1=xm;
}while(fabs(x0-x1)>1e-6);
r[2]=(x0+x1)/2;
printf("the 2 roots of the function are:\n");
for(i=0;i<3;i++)
printf("r[%d]=%.2f\n",i,r[i]);
for(i=0;i<3;i++)
if(fabs(r[i]*r[i]*r[i]+a[0]*r[i]*r[i]+a[1]*r[i]+a[2])<1e-6)
j++;
if(j==3)
printf("congratulations, the answer are all right!\n");
else
printf("sorry, you should try again.\n");
}

else
{
r[0]=r[1]=r[2]=x2;
printf("3 roots are equal!\n");
printf("the 3 roots are:\n");
for(i=0;i<3;i++)
printf("r[%d]=%.2f\n",i,r[i]);
for(i=0;i<3;i++)
if(fabs(r[i]*r[i]*r[i]+a[0]*r[i]*r[i]+a[1]*r[i]+a[2])<1e-6)
j++;
if(j==3)
printf("congratulations, the answer are all right!\n");
else
printf("sorry, you should try again.\n");

}
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式