-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit.cpp
110 lines (105 loc) · 2.37 KB
/
fit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "fit.h"
#include<QVector>
Fit::Fit()
{
}
double Fit::sum(vector<double> Vnum, int n)
{
double dsum=0;
for (int i=0; i<n; i++)
{
dsum+= Vnum[i];
}
return dsum;
}
//乘积和
double Fit::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次方和
double Fit::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;
}
//x的ex次方与y的乘积的累加
double Fit::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;
}
//计算方程组的增广矩阵
void Fit::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);
}
//求解方程
void Fit::CalEquation(int exp, double coefficient[])
{
for(int k=1;k<exp;k++) //消元过程
{
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--) //回代求解
coefficient[l]=(Em[l][exp+1]-F(coefficient,l+1,exp))/Em[l][l];
}
//供CalEquation函数调用
double Fit::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;
}
QVector<double> Fit::GetPara(QVector<double> qvx, QVector<double> qvy)
{
double coefficient[5];
for(int i=0;i<5;i++) coefficient[i]=0;
vector<double> vx,vy;
QVector<double> qcof;
int exp =2 ;
int size = qvx.size();
for (int i=0; i<size; i++)
{
vx.push_back(qvx[i]);
vy.push_back(qvy[i]);
qDebug()<< qvx[i]<<qvy[i];
}
EMatrix(vx,vy,size,exp+1,coefficient);
for(int i=0;i<5;i++)
{
qcof.append(coefficient[i]);
}
return qcof;
}