當前位置:編程學習大全網 - 編程語言 - C語言編程:內容:用二分法求壹元三次方程的根,要求:由主函數調用求根子函數

C語言編程:內容:用二分法求壹元三次方程的根,要求:由主函數調用求根子函數

代碼如下,很完整

#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");

}

}

}

}

  • 上一篇:請問數據庫 SQL語言用什麽軟件編寫,運行?
  • 下一篇:家教系統軟件開發?
  • copyright 2024編程學習大全網