Reprint Please specify source: http://blog.csdn.net/lsh_2013/article/details/46697625
Least squares (also known as the least squares method) is a mathematical optimization technique. It matches by minimizing the squared error and finding the best function of the data.
The C + + implementation code is as follows:
#include <iostream> #include <vector> #include <cmath>using namespace std;//least squares fitting correlation function defines a double sum ( vector<double> vnum, int n);d ouble mutilsum (vector<double> Vx, vector<double> Vy, int n);d ouble Relatepow (vector<double> vx, int n, int ex);d ouble Relatemutixy (vector<double> VX, vector<double> Vy, int n, int ex); void Ematrix (vector<double> Vx, vector<double> Vy, int n, int ex, Double coefficient[]); void Ca lequation (int exp, double coefficient[]);d ouble F (double c[],int l,int m);d ouble em[6][4];//main function, where the data is to be synthesized two times curve int main ( int argc, char* argv[]) {double arry1[5]={0,0.25,0,5,0.75};d ouble arry2[5]={1,1.283,1.649,2.212,2.178};d ouble Coefficient[5];memset (coefficient,0,sizeof (double));vector<double> vx,vy;for (int i=0; i<5; i++) {Vx.push _back (Arry1[i]); Vy.push_back (Arry2[i]);} Ematrix (vx,vy,5,3,coefficient);p rintf ("fit equation: y =%lf +%lfx +%lfx^2 \ n", Coefficient[1],coefficient[2],coefficient[3] ); return 0;} Accumulate double sum (vector<double> vnum, int n) {double dsum=0;for (int i=0; i<n; i++) {dsum+=vnum[i];} return DSum;} Product and Double mutilsum (vector<double> Vx, vector<double> Vy, int n) {double dmultisum=0;for (int i=0; i<n; i+ +) {Dmultisum+=vx[i]*vy[i];} return dmultisum;} Ex secondary square and double Relatepow (vector<double> Vx, int n, int ex) {double resum=0;for (int i=0; i<n; i++) {Resum+=pow (vx[i ],EX);} return resum;} The summation of the sum of the X's ex-square and Y's multiplied double relatemutixy (vector<double> Vx, vector<double> Vy, int n, int ex) {double dremultisum =0;for (int i=0; i<n; i++) {Dremultisum+=pow (Vx[i],ex) *vy[i];} return dremultisum;} Augmented matrix for computational equations void Ematrix (vector<double> Vx, vector<double> Vy, int n, int ex, Double coefficient[]) {for (int i = 1; i<=ex; i++) {for (int j=1; j<=ex; J + +) {Em[i][j]=relatepow (vx,n,i+j-2);} Em[i][ex+1]=relatemutixy (vx,vy,n,i-1);} Em[1][1]=n; Calequation (ex,coefficient);} Solution equation void calequation (int exp, double coefficient[]) {for (int k=1;k<exp;k++)//elimination procedure {for (int i=k+1;i<exp+1;i++) {double p1=0;if (em[k][k]!=0) p1=em[i][k]/em[k][k];for (int j=k;j<exp+2;j++) EM[I][J]=EM[I][J]-EM[K][J]*P1 ;}} coefficient[exp]=em[exp][exp+1]/em[exp][exp];for (int l=exp-1;l>=1;l--)///Return Solution coefficient[l]= (Em[l][exp+1]-F ( COEFFICIENT,L+1,EXP))/em[l][l];} For calequation function call double F (double c[],int l,int m) {double sum=0;for (int i=l;i<=m;i++) Sum+=em[l-1][i]*c[i];return Sum }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The principle of least squares fitting polynomial and the realization of C + +