C语言表编程:用二分法求一元三次方程的根 要求:又主函数调用求根子函数
答案:3 悬赏:40 手机版
解决时间 2021-03-01 19:49
- 提问者网友:爱唱彩虹
- 2021-03-01 01:36
C语言表编程:用二分法求一元三次方程的根 要求:又主函数调用求根子函数
最佳答案
- 五星知识达人网友:杯酒困英雄
- 2021-03-01 02:22
二分法的基本思路是:任意两个点x1和x2,判断区间(x1,x2)内有无一个实根,如果f(x1)与f(x2)符号相反,则说明有一实根。接着取(x1,x2)的中点x,检查f(x)和f(x2)是否同号,如果不同号,说明实根在(x,x2)之间,如果同号,在比较(x1,x),这样就将范围缩小一半,然后按上述方法不断的递归调用,直到区间相当小(找出根为止)!
比如用二分法求f(x)=x^3-6x-1=0的实根。
代码如下(已调试):
#include "math.h"
main()
{
float x,x1,x2;
float F(float x,float x1,float x2);
printf("请输入区间[x1,x2]\n");
scanf("%f%f",&x1,&x2);
printf("x=%f\n",F(x,x1,x2));
}
float F(float x,float x1,float x2)
{
float f,f1,f2;
do
{
f1=pow(x1,3)-6*x1-1.0;
f2=pow(x2,3)-6*x2-1.0;
}while(f1*f2>0); //确保输入的x1,x2使得f1,f2符号相反
do
{
x=(x1+x2)/2; //求x1,x2的中点
f=pow(x,3)-6*x-1.0;
if(f1*f>0) //当f与f1符号相同时
{x1=x;f1=f;}
else if(f2*f>0) //当f与f2符号相同时
{x2=x;f2=f;}
}while(fabs(f)>1e-6); //判断条件fabs(f)>1e-6的意思是f的值非常0
return x;
}
输入:1 5
则输出:x=2.528918
输入:-10 10
则输出:x=2.528918
比如用二分法求f(x)=x^3-6x-1=0的实根。
代码如下(已调试):
#include "math.h"
main()
{
float x,x1,x2;
float F(float x,float x1,float x2);
printf("请输入区间[x1,x2]\n");
scanf("%f%f",&x1,&x2);
printf("x=%f\n",F(x,x1,x2));
}
float F(float x,float x1,float x2)
{
float f,f1,f2;
do
{
f1=pow(x1,3)-6*x1-1.0;
f2=pow(x2,3)-6*x2-1.0;
}while(f1*f2>0); //确保输入的x1,x2使得f1,f2符号相反
do
{
x=(x1+x2)/2; //求x1,x2的中点
f=pow(x,3)-6*x-1.0;
if(f1*f>0) //当f与f1符号相同时
{x1=x;f1=f;}
else if(f2*f>0) //当f与f2符号相同时
{x2=x;f2=f;}
}while(fabs(f)>1e-6); //判断条件fabs(f)>1e-6的意思是f的值非常0
return x;
}
输入:1 5
则输出:x=2.528918
输入:-10 10
则输出:x=2.528918
全部回答
- 1楼网友:冷風如刀
- 2021-03-01 03:32
代码如下,很完整
#include
#include
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");
}
}
}
}
- 2楼网友:夜余生
- 2021-03-01 02:30
建议你问这些问题的时候给出方程,这样,大家直接就给出你算法和程序
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯