#define_AFXDLL#includeiostream#includecmath#includecstdlib#includeafxwin.h#includeDeviceContext.husingnamespacestd;#includeWindows.h#includestdio.h#includecmath#defineMax200classfiting{public:fiting();//¹¹ÔìboolFindMax(intt);//ÁÐÖ÷ÔªËØvoidXiaoQu(intt);//ÏûÈ¥voidHuiDai(intt);//intinversion();//ͳ³ïº¯Êývoidop();//Êä³öµ½ÆÁÄ»ÉÏvoidfiting1();//¼ÆËã³öx¡¢noiseºÍyvoidchange();//·¶µÂÃɵÂdoublenoise[Max];//ÔëÉùµãdoubleM[Max][Max];//Ô¾ØÕódoubleE[Max][Max];//Äæ¾ØÕódoublea[Max];//ϵÊýdoublex[Max],y[Max],Y[Max];//xºÍydoublexx[Max][Max];//°Ñx·¶µÂÃɵ»¯voidassignment();//°Ñxx¸³¸øMvoidcalculate();//¼ÆËãintnum;//È¡³önum½×voidrinv();double**aa;~fiting(){inti;for(i=0;inum;i++){delete[]aa[i];}delete[]aa;}};voidfiting::op(){DeviceContextdevice;HDChdc=device.GetCurDC();MoveToEx(hdc,0,0,NULL);for(inti=0;inum;i++){for(intj=0;jnum;j++){::SetPixel(device.GetCurDC(),x[i]*15,150+y[i]*100,RGB(255,0,0));HPENpen;pen=CreatePen(PS_SOLID,1,RGB(255,0,0));LineTo(hdc,x[i],Y[j]);}}}intfiting::inversion(){//inti;fiting1();change();assignment();/*for(i=0;inum-1;i++){if(FindMax(i)){XiaoQu(i);}else{cout¸Ã¾ØÕ󲻿ÉÄæ!endl;return0;}}for(i=num-1;i=0;i--){HuiDai(i);}*/rinv();calculate();op();return1;}fiting::fiting(){coutÇëÊäÈë½×Êý£º;cinnum;aa=newdouble*[Max];for(inti=0;inum;i++){aa[i]=newdouble[num+8];}}voidfiting::calculate(){for(inti=0;inum;i++){for(intj=0;jnum;j++){a[j]=a[j]+aa[i][j]*y[j];}}for(intii=0;iinum;ii++){for(intjj=0;jjnum;jj++){Y[jj]=Y[jj]+xx[ii][jj]*a[jj];}}}voidfiting::assignment(){for(inti=0;inum;i++){for(intj=0;jnum;j++){aa[i][j]=xx[i][j];}}}voidfiting::fiting1(){for(inti=0;iMax;i++){x[i]=i+1;noise[i]=(rand()%100)*0.003;y[i]=sin(0.1*x[i])+noise[i];}}voidfiting::change(){doubletemp[Max];longt=1;for(inti=0;iMax;i++){temp[i]=x[i];xx[0][i]=1;}for(inti=1;iMax;i++){for(intj=0;jMax;j++){xx[i][j]=temp[j];temp[j]=temp[j]*x[j];}}}//boolfiting::FindMax(intt)//ÁÐÖ÷ÔªËØ//{//inti,j,k=t;////doublemax=fabs(M[t][t]),temp;//for(i=t+1;inum;i++)//{//if(maxfabs(M[i][t]))//{//max=fabs(M[i][t]);//k=i;//}//}//if(max==0)//{//return0;//}//for(j=0;jnum;j++)//{//temp=M[t][j];//M[t][j]=M[k][j];//M[k][j]=temp;////temp=E[t][j];//E[t][j]=E[k][j];//E[k][j]=temp;//}//return1;//}////voidfiting::XiaoQu(intt)//ÏûÈ¥//{//inti,j;//doublemax=M[t][t],m;//for(i=t+1;inum;i++)//{//m=M[i][t]/max;//for(j=0;jnum;j++)//{//M[i][j]=M[i][j]-M[t][j]*m;//E[i][j]=E[i][j]-E[t][j]*m;//}//}//}////voidfiting::HuiDai(intt)//{//inti,j;//doublemax;//max=M[t][t];//for(i=t;inum;i++)//{//M[t][i]=M[t][i]/max;//}//for(i=0;inum;i++)//{//E[t][i]=E[t][i]/max;//}//for(i=t-1;i=0;i--)//{//max=M[i][t];//M[i][t]=0;//for(j=0;jnum;j++)//{//E[i][j]=E[i][j]-max*E[t][j];//}//}//}intmain(){fitingf;f.inversion();return0;}voidfiting::rinv()//¾ØÕóÇóÄæ{int*is,*js,i,j,k;doubled,p;is=newint[Max];js=newint[Max];for(k=0;knum;k++){d=0.0;for(i=k;inum;i++)for(j=k;jnum;j++){p=fabs(aa[i][j]);if(pd){d=p;is[k]=i;js[k]=j;}}if(d+1.0==1.0){delete[]is,js;cout\nAΪÆæÒì¾ØÕó!ûÓÐÄæ¾ØÕó¡£endl;exit(1);}if(is[k]!=k)//È«Ñ¡Ö÷Ôªfor(j=0;jnum;j++){p=aa[k][j];aa[k][j]=aa[is[k]][j];aa[is[k]][j]=p;}if(js[k]!=k)for(i=0;inum;i++){p=aa[i][k];aa[i][k]=aa[i][js[k]];aa[i][js[k]]=p;}if(aa[k][k]==0){into=k+1;while(aa[o][o]==0){o++;}for(i=0;inum;i++){doublejj=aa[k][i];aa[k][i]=aa[o][i];aa[o][i]=jj;}}aa[k][k]=1.0/aa[k][k];for(j=0;jnum;j++){if(j!=k)aa[k][j]=aa[k][j]*aa[k][k];}for(i=0;inum;i++){if(i!=k)for(j=0;jnum;j++)if(j!=k)aa[i][j]=aa[i][j]-aa[i][k]*aa[k][j];}for(i=0;inum;i++){if(i!=k)aa[i][k]=-aa[i][k]*aa[k][k];}}for(k=num-1;k=0;k--){if(js[k]!=k)for(j=0;jnum;j++){p=aa[k][j];aa[k][j]=aa[js[k]][j];aa[js[k]][j]=p;}if(is[k]!=k)for(i=0;inum;i++){p=aa[i][k];aa[i][k]=aa[i][is[k]];aa[i][is[k]]=p;}}delete[]is,js;}