|
高次方程求解,三次方程参考范盛金的公式,四次方程采用费拉里降次为两个二次方程,五次方程没有精确的求根公式,采用牛顿迭代法(利用导线逼近0点)进而降次为四次方程,再套用费拉里将次(包含复数解)。EQUATION.EXE
摘要:
=============================================================================
这是一个专注解方程和方程组的简单工具,帮助那些想快速计算出结果的童鞋们。求解
结果精确到小数点后12位,且支持复根。
=============================================================================
用法:
-----------------------------------------------------------------------------
equation [/O {parameters}]|[/M]|[/R {parameters}]
-----------------------------------------------------------------------------
/H 显示帮助信息
/O 求解一元方程
/M 求解多元方程组
/R 求解单位复元根
-----------------------------------------------------------------------------
示例:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
equation /O 3 0 5 -6 -9 //求解一元四次方程3x^4+0x^3+5x^2-6x-9=0
The solution of the equation 3x^4+0x^3+5x^2-6x-9=0 is:
┌────────────────────────┐
Imag root: -0.252438186547+1.696390660707i
-0.252438186547-1.696390660707i
Real root: 1.293411063722
-0.788534690627
└────────────────────────┘
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
equation /R 5 //求解5次复元根 x^5-1=0
┌────────────────────────┐
x[ 1]= 0.309016994375+0.951056516295i
x[ 2]=-0.809016994375+0.587785252292i
x[ 3]=-0.809016994375-0.587785252292i
x[ 4]= 0.309016994375-0.951056516295i
x[ 5]= 1.000000000000-0.000000000000i
└────────────────────────┘
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
equation /M //进入方程组模式
[Multivariable Equations] ---Quit using "q"
{2 //此处输入方程组个数
Equation coefficients:
3 2 5 //第一个方程式的系数
6 9 1 //第二个方程式的系数
Solution of equations:
┌───────────────┐
x[0]= 2.866666793823
x[1]=-1.800000071526
└───────────────┘
{{q //输入q退出该模式
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
equation.c源码(采取POS开源协议,即发布的每个作品都开源):- /* CONSOLE SOLVING EQUATION TOOLS, COPYRIGHT@2016~2018 BY HAPPY EQUATION.EXE VERSION 1.0*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>//定义帮助说明//////////////////////////////////////////////////////#define HELPINFORMATION "\SOLVING EQUATION TOOLS, COPYRIGHT@2016~2018 BY HAPPY, VERSION 1.0\n\-----------------------------------------------------------------\n\equation [/O {parameters}]|[/M]|[/R {parameters}]\n\-----------------------------------------------------------------\n\ /H Displays help information\n\ /O Solves the univariate equation\n\ /M Solving Systems of multivariable Equations\n\ /R Solve the unit root\n\-----------------------------------------------------------------\n\12/03/2016\n"/////////////////////////////////////////////////////////////////////***************功能函数群***************/void equation_set(int n) { int i, j, m, r, k=0, t, mark; float a[11][12]; printf("Equation coefficients:\n"); r=n; for(j=0; j<n; j++){ for(i=0; i<r+1; i++){ scanf("%f", &a[j][i]); } if(a[j][0]!=0){mark=j;} } //系数修正 for(j=0; j<n; j++){ if(a[j][0]+a[mark][0] != 0){ for(i=0; i<r+1; i++){ a[j][i] += a[mark][i]; } } } if(r==n) { printf("Solution of equations:\n"); for(t=0; t<n-1; t++) for(m=0; m<n; m++) for(j=0; j<n; j++) if(j!=m) { float b= a[j][m]/a[m][m]; for(i=0; i<n+1; i++) a[j][i] -= a[m][i]*b; } for(j=0; j<n; j++) { a[j][n] /=a[j][j]; a[j][j] /=a[j][j]; } } printf("┌───────────────┐\n"); for(j=0; j<r; j++){ printf(" x[%d]=%15.12f\n", j, a[j][n]); } printf("└───────────────┘\n");}void twice(double a, double b, double c){ double x1,x2,den=2*a,delt=b*b-4*a*c; if(a==0){ if(b==0){ printf(" Not a real equation"); } else{ printf(" Reduced to once equation\n Real root: x=%.12lf",-c/b); } } else{ if(delt>0){ x1=-b/den+sqrt(delt)/den; x2=-b/den-sqrt(delt)/den; printf(" Real root: %15.12lf\n %15.12lf",x1,x2); } else if(delt==0){ printf(" Double root: %15.12lf",-b/den); } else if(delt<0){ x1=sqrt(-delt)/den; printf(" Imag root: %15.12lf+%.12lfi\n %15.12lf-%.12lfi",-b/den,x1,-b/den,x1); } }}double thice(double a, double b, double c, double d, int gk){ double A=b*b-3*a*c,B=b*c-9*a*d,C=c*c-3*b*d,delta=B*B-4*A*C; if(A==0 && B==0){ if(gk!=1){printf(" Triple real: %15.12lf",-b/(3*a));} return -b/(3*a); } else if(delta>0){ double Y1=A*b+3*a*(-B+sqrt(delta))/2.0,Y2=A*b+3*a*(-B-sqrt(delta))/2.0; Y1=(Y1>0)? pow(Y1,1.0/3.0) :-pow(-Y1,1.0/3.0); Y2=(Y2>0)? pow(Y2,1.0/3.0) :-pow(-Y2,1.0/3.0); double x1=(-b-Y1-Y2)/(3.0*a); double prr=(-2*b+Y1+Y2)/(6.0*a),pri=(Y1-Y2)*sqrt(3)/(6.0*a); if(gk!=1){printf(" First real: %15.12lf\n Imag root: %15.12lf+%.12lfi\n %15.12lf-%.12lfi",x1,prr,pri,prr,pri);} return x1; } else if(delta==0){ if(gk!=1){printf(" Double real: %15.12lf\n Other real: %15.12lf\n",-B/(2.0*A),-b/a+B/A);} if(-B/(2.0*A) > -b/a+B/A){ return -B/(2.0*A); }else{ return -b/a+B/A; } } else if(delta<0){ double r=acos((2*A*b-3*a*B)/(2*A*sqrt(A))); double x1=(-b-2.0*sqrt(A)*cos(r/3.0))/(3.0*a); double x2=(-b-2*sqrt(A)*cos((r+2*M_PI)/3.0))/(3.0*a); double x3=(-b-2*sqrt(A)*cos((r+4*M_PI)/3.0))/(3.0*a); if(gk!=1){printf(" Triple real: %15.12lf\n %15.12lf\n %15.12lf",x1,x2,x3);} double tempv; if(x1 > x2){ tempv=x1; }else{ tempv=x2; } if(tempv > x3){ return tempv; }else{ return x3; } }}void foice(double a, double b, double c, double d, double e){ double y,f[4],M,N,P; int i; b/=a;c/=a;d/=a;e/=a;a=1; f[0]=8; f[1]=-4.0*c; f[2]=-(8.0*e-2.0*b*d); f[3]=-e*(b*b-4.0*c)-d*d; y=thice(f[0],f[1],f[2],f[3],1); M=sqrt(8.0*y+b*b-4.0*c); N=b*y-d; if(M==0){ P=sqrt(y*y-e); twice(2.0,b,2.0*(y+P)); printf("\n"); twice(2.0,b,2.0*(y-P)); }else{ twice(2.0,b+M,2.0*(y+N/M)); printf("\n"); twice(2.0,b-M,2.0*(y-N/M)); }}void fifth(double a, double b, double c, double d, double e, double f){ double r,x,F,F1; b/=a;c/=a;d/=a;e/=a;f/=a; a=-1.0; do { x=a; F=x*x*x*x*x+b*x*x*x*x+c*x*x*x+d*x*x+e*x+f; F1=5*x*x*x*x+4*b*x*x*x+3*c*x*x+2*d*x+e; a=x-F/F1; } while(fabs(x-a)>=1e-10); double e1=-f/x,d1=(e1-e)/x,c1=(d1-d)/x,b1=(c1-c)/x; foice(1.0,b1,c1,d1,e1); printf ("\n Iterative : %15.12lf",x);}/***************业务函数群***************///字符串转等式char* atop(char *s){ if(atof(s)>=0){ char *r=malloc(strlen(s)+strlen("+")+1); strcpy(r,"+");strcat(r,s); return r; } return s;}//解多元方程组void Multivariable_Equations(){ char str[8]; puts("[Multivariable Equations] ---Quit using "q""); while(printf("{"),fgets(str,7,stdin)!=NULL){ char *i=strchr(str,'\n'); if(i!=NULL){*i='\0';} if (i!=str){ if(!strcmp(str,"q")){break;} equation_set(atoi(str)); } }}//解一元高次方程void Univariate_Equation(int argc, char** argv){ if(argc==4){ printf("The solution of the equation %sx%s=0 is: \n",argv[2],atop(argv[3])); printf("┌────────────────────────┐\n"); if(atof(argv[2])==0){printf(" Not a real equation");}else{printf(" %15.12lf",-atof(argv[3])/atof(argv[2]));} printf("\n└────────────────────────┘"); } else if(argc==5){ printf("The solution of the equation %sx^2%sx%s=0 is: \n",argv[2],atop(argv[3]),atop(argv[4])); printf("┌────────────────────────┐\n"); twice(atof(argv[2]),atof(argv[3]),atof(argv[4])); printf("\n└────────────────────────┘"); } else if(argc==6){ printf("The solution of the equation %sx^3%sx^2%sx%s=0 is: \n",argv[2],atop(argv[3]),atop(argv[4]),atop(argv[5])); printf("┌────────────────────────┐\n"); if(atof(argv[2])==0){ printf(" Reduced to a quadratic equation\n"); twice(atof(argv[3]),atof(argv[4]),atof(argv[5])); } else{ thice(atof(argv[2]),atof(argv[3]),atof(argv[4]),atof(argv[5]),0); } printf("\n└────────────────────────┘"); } else if(argc==7){ printf("The solution of the equation %sx^4%sx^3%sx^2%sx%s=0 is: \n",argv[2],atop(argv[3]),atop(argv[4]),atop(argv[5]),atop(argv[6])); printf("┌────────────────────────┐\n"); if(atof(argv[2])==0){ printf(" Reduced to cubic equation\n"); if(atof(argv[3])==0){ printf(" Reduced to a quadratic equation\n"); twice(atof(argv[4]),atof(argv[5]),atof(argv[6])); }else{ thice(atof(argv[3]),atof(argv[4]),atof(argv[5]),atof(argv[6]),0); } } else{ foice(atof(argv[2]),atof(argv[3]),atof(argv[4]),atof(argv[5]),atof(argv[6])); } printf("\n└────────────────────────┘"); } else if(argc==8){ printf("The solution of the equation %sx^5%sx^4%sx^3%sx^2%sx%s=0 is: \n",argv[2],atop(argv[3]),atop(argv[4]),atop(argv[5]),atop(argv[6]),atop(argv[7])); printf("┌────────────────────────┐\n"); if(atof(argv[2])==0){ printf(" Reduced to foice equation\n"); if(atof(argv[3])==0){ printf(" Reduced to cubic equation\n"); if(atof(argv[4])==0){ printf(" Reduced to a quadratic equation\n"); twice(atof(argv[5]),atof(argv[6]),atof(argv[7])); }else{ thice(atof(argv[4]),atof(argv[5]),atof(argv[6]),atof(argv[7]),0); } }else{ foice(atof(argv[3]),atof(argv[4]),atof(argv[5]),atof(argv[6]),atof(argv[7])); } }else{ fifth(atof(argv[2]),atof(argv[3]),atof(argv[4]),atof(argv[5]),atof(argv[6]),atof(argv[7])); } printf("\n└────────────────────────┘"); }}//单位元根void Unit_Root(int argc, char** argv){ if(argc!=3){ printf("Missing parameters\n"); exit(1); } int i, n=atoi(argv[2]); printf("┌──────────────────────┐\n"); for(i=1; i<n; i++){ if(i==n){printf(" r[%2d]=%15.12lf%.12lfi",i,cos(2*i*M_PI/n),sin(2*i*M_PI/n));break;} if(sin(2*i*M_PI/n)<0){ printf(" r[%2d]=%15.12lf%.12lfi\n",i,cos(2*i*M_PI/n),sin(2*i*M_PI/n)); } else{ printf(" r[%2d]=%15.12lf+%.12lfi\n",i,cos(2*i*M_PI/n),sin(2*i*M_PI/n)); } } printf(" r[%2d]= 1.000000000000", n); printf("\n└──────────────────────┘");}/*************MAIN主函数入口*************/int main(int argc, char** argv){ if( (argc >1) && (argv[1][0]=='/') ) { switch(argv[1][1]) { case 'H': case 'h': fputs(HELPINFORMATION,stdout); exit(0); case 'M': case 'm': Multivariable_Equations(); break; case 'O': case 'o': Univariate_Equation(argc, argv); break; case 'R': case 'r': Unit_Root(argc, argv); break; default: fputs(HELPINFORMATION,stdout); exit(1); } return 0; } fputs(HELPINFORMATION,stdout); exit(1);}
复制代码 |
|