您好,欢迎访问三七文档
#includestdio.h#includemath.hvoidnihe();voidgs();voidmain(){inti,j,m,n;floato[50];floatx[50],y[50],a[50][50];printf(输入数据节点数n=,n);scanf(%d,&n);for(i=1;i=n;i++){printf(i=%d\n,i);}printf(各节点的数据x[i]\n);for(i=1;i=n;i++){printf(x[%d]=,i);scanf(%f,&x[i]);}printf(各节点的数据y[i]\n);for(i=1;i=n;i++){printf(y[%d]=,i);scanf(%f,&y[i]);}printf(\n);printf(拟合的多项式次数m=,m);scanf(%d,&m);}voidnihe(floatx[50],floaty[50],intm,intn){inti,j,k=0,c=1,w=1;floatf,a[50][50],o[50];;do{f=0;for(i=1;i=n;i++){f=f+pow(x[i],k)*pow(x[i],k);}a[c][c]=f;a[c+1][c-1]=f;a[c-1][c+1]=f;c++;k++;}while(k=m);k=1;c=1;do{f=0;for(i=1;i=n;i++){f=f+pow(x[i],k);}a[c+1][c]=f;a[c][c+1]=f;c++;k++;k++;}while(k=m+1);k=0;c=1;do{f=0;for(i=1;i=n;i++){f=f+y[i]*pow(x[i],k);}k++;a[c][m+2]=f;c++;}while(k=m);for(i=1;i=m+1;i++){for(j=1;j=m+2;j++){printf(%f\t,a[i][j]);}printf(\n);}printf(------------------------------------------------------------------------\n);}voidgs(floata[50][50],floato[50],intm){inti,j,c=1,z=1,t=1,w=1,n;floatl,max,p,q;n=m+1;do{max=a[z][t];for(i=z+1,j=t;i=n;i++){if(fabs(a[i][j])fabs(max)){max=a[i][j];w=i;}else{w=z;}}if(c=n-1){for(i=z,j=t;j=n+1;j++){q=a[i][j];a[i][j]=a[w][j];a[w][j]=q;}}for(i=1;i=n;i++){for(j=1;j=n+1;j++){printf(%f\t,a[i][j]);}printf(\n);}printf(------------------------------------------------------------------------\n);for(i=z,w=t;i=n;i++){l=a[i+1][w]/a[w][w];for(j=1;j=n+1;j++){a[i+1][j]=a[i+1][j]-l*a[w][j];}}z++;t++;c++;}while(c=n);if(a[n][n]==0){printf(矩阵奇异\n);return;}else{i=n;o[i]=(a[i][n+1]/a[i][i]);for(i=n-1;i=1;i--){p=0;for(j=i;jn;j++){p=p+a[i][j+1]*o[j+1];}o[i]=(a[i][n+1]-p)/a[i][i];}}for(i=1;i=m+1;i++){printf(o[%d]=%f\n,i,o[i]);}}
本文标题:曲线拟合C语言程序
链接地址:https://www.777doc.com/doc-1882894 .html