二分法c语言程序代码一元三次方程(c语言2分法解方程)
求c语言二分法求一元三次方程的根语句的注释
#includestdio.h
#includemath.h
void?main()
{
float?x0,x1,x2,fx0,fx1,fx2;
do
{
printf("enter?x1??x2:");
scanf("%f,%f",x1,x2); //输入在哪个区间寻找方程的根
fx1=x1*((2*x1-4)*x1+3)-6; //计算在两个端点,函数的值
fx2=x2*((2*x2-4)*x2+3)-6;
}while(fx1*fx20); //必须要函数在两个端点,函数的值异号,才能保证区间内有根
do
{
x0=(x1+x2)/2; //x0为区间的中点
fx0=x0*((2*x0-4)*x0+3)-6; //中点的函数值
if((fx0*fx1)0) //将中点修改为函数值与中点函数值同号的端点
{
x2=x0;
fx2=fx0;
}
else
{
x1=x0;
fx2=fx0;
}
}while(fabs(fx0)=1e-5); //直到中点的函数值与0足够接近
printf("x=%6.2f\n",x0);
}
怎么用二分法解一元三次方程
(1)实系数一元三次代数方程至少有一个实根
(2)首先确定实根所在的范围,即确定求解区间。为此确定方程f(x)=0 -- 使得 f(a)f(b)0
的较小的a,b值 [a,b]作为求解区间。设:ab,其中点为:(a+b)/2=c
(3)之后用二分法(迭代法的一种):计算 |f(c)| 的值,比较|f(a)|、|f(b)|、|f(c)|的大小,
取出 两个较小者作为新的求解区间[a,b],再算出c,再比较|f(a)|、|f(b)|、|f(c)|的
大小,直到得出满足迭代精度的解为止。
(4)当得到一个实数解x1之后,用原方程f(x)除以(x-x1)可以得到一个二次方程,再用求根
公式得到另外两个根。
C语言编程:内容:用二分法求一元三次方程的根,要求:由主函数调用求根子函数
代码如下,很完整
#includestdio.h
#includemath.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;i3;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*fm0)
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*fm0)
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*fm0)
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;i3;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*fm0)
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;i3;i++)
printf("r[%d]=%.2f\n",i,r[i]);
for(i=0;i3;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]==0x3*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*fm0)
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;i3;i++)
printf("r[%d]=%.2f\n",i,r[i]);
for(i=0;i3;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]!=0x3*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*fm0)
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;i3;i++)
printf("r[%d]=%.2f\n",i,r[i]);
for(i=0;i3;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;i3;i++)
printf("r[%d]=%.2f\n",i,r[i]);
for(i=0;i3;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");
}
}
}
}
跪求用c语言编写用二分法求一元三次方程的解法,(系数为2,-4,3,-6,根为2)
就是求
2X^3-4X^2+3X-6=0
如果2分法的话要有两个边界才行噢,
而且要假设单调性.
(2分法的必要条件)

C语言表编程:用二分法求一元三次方程的根 要求:又主函数调用求根子函数
二分法的基本思路是:任意两个点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*f20); //确保输入的x1,x2使得f1,f2符号相反
do
{
x=(x1+x2)/2; //求x1,x2的中点
f=pow(x,3)-6*x-1.0;
if(f1*f0) //当f与f1符号相同时
{x1=x;f1=f;}
else if(f2*f0) //当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