《C++最小二乘法求多项式拟合曲线.doc》由会员分享,可在线阅读,更多相关《C++最小二乘法求多项式拟合曲线.doc(32页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateC+最小二乘法求多项式拟合曲线C+最小二乘法求多项式拟合曲线/ shujunihe.cpp : 定义控制台应用程序的入口点。/ quanzhuyuan.cpp : 定义控制台应用程序的入口点。/#include stdafx.h#include #include#includeusing namespace std;class shujunihepublic:shuj
2、unihe():xlh(0),fyl(false),cnt(0),n(9) void printb(double (&)3);void printa(double 33);void printx(double x3,double a33,double b3,int n); void quanzhuyuan(double a33,double b,int x23); void restoreA(double a33,double x);double Sum(int r,int c,double );double Sumf(int r,double f,double x);void restore
3、B(double b,double f,double x);void printx2(int x);private:int xlh;bool fyl;int cnt;int n;void shujunihe:printx2(int x2)for(int i=0;i3;i+)coutx2i ;coutendl;void shujunihe:restoreB(double b,double f,double x) for(int i=0;i=2;i+)bi=Sumf(i,f,x);double shujunihe:Sumf(int r,double f,double x)double sum=0;
4、for(int i=0;i=n-1;i+)sum+=fi*pow(xi, r);return sum;double shujunihe:Sum(int r,int c,double x)double sum=0;for(int i=0;i=n-1;i+ )sum+=pow(xi, r+c);return sum;void shujunihe:restoreA(double a33,double x)for(int i=0;i=2;i+)for(int j=0;j=2;j+)aij=Sum(i,j,x);a00=n;void shujunihe:quanzhuyuan(double d33,do
5、uble c3,int x23)double max;int row,col;double temp1,temp2;int temp3;double btemp1;for(int i=0;i3;i+) max=fabs(dii);row=i;col=i;for(int j=i;j3;j+)for(int k=i;kmax)max=fabs(djk);row=j;col=k;/*for(int n=0;nd.length;n+)/hang temp1=ain; ain=arown; arown=temp1; btemp1=bi; bi=brow; brow=btemp1;for(int m=0;
6、md.length;m+)/lie temp2=ami;ami=amcol;amcol=temp2;*/if(col!=i&row!=i)fyl=true; xlh=col;for(int n=0;n3;n+)/hang temp1=din; din=drown; drown=temp1; btemp1=ci; ci=crow; crow=btemp1;for(int m=0;m3;m+)/lie temp2=dmi;dmi=dmcol;dmcol=temp2;elseif(row!=i&col=i)for(int n=0;n3;n+)/hang temp1=din; din=drown; d
7、rown=temp1; btemp1=ci; ci=crow; crow=btemp1;elseif(col!=i&row=i)fyl=true;for(int m=0;m3;m+)/lie xlh=col;temp2=dmi;dmi=dmcol;dmcol=temp2;if(fyl)temp3=x2i;x2i=x2xlh;x2xlh=temp3;/x1cnt=i;/y1cnt=xlh;cnt+;fyl=false;for(int j=i+1;j3;j+)/J表示行double l=dji/dii; for(int e=i+1;e=0;i-)double sum=0;for(int j=i+1
8、;j=n-1;j+) sum+=aij*xj; xi=1/aii*(bi-sum);for(int k=0;k=n-1;k+)coutxk ;coutendl;void shujunihe:printa(double a33)for(int i=0;i3;i+)for(int j=0;j3;j+)coutaij ; coutendl;void shujunihe:printb(double (&b)3)int size=sizeof(b)/sizeof(double);for (int s=0;ssize;s+)coutbs ;coutendl;int main() double x=1,3,
9、4,5,6,7,8,9,10; double f=10,5,4,2,1,1,2,3,4; double a33;double b3;double x13;int x23=1,2,3;shujunihe xy;xy.restoreA(a,x);xy.restoreB(b,f,x);cout正规方程的系数矩阵aendl; xy.printa(a);cout正规方程的bendl; xy.printb(b);xy.quanzhuyuan(a,b,x2);cout全主元化简后的系数矩阵aendl; xy.printa(a); cout全主元化简后的bendl; xy.printb(b);coutx的求解的值endl; xy.printx(x1,a,b,3); coutx对应的位置endl; xy.printx2(x2);-