[cpp]
//相關分析.h
void XiangGuanFenXi()
{
using namespace std;
const int M = 6209;//倚用系列長度
const int Cha = 4380;//待插補系列長度
double H[M] = {0},//水位
Q[M] = {0},//流量
AverageH = 0,
AverageQ = 0,//均值
a, b,//待定回歸系數
r = 0,//線性相關系數
sigmaH =0,
sigmaQ = 0;//均方差
double ChaH[Cha],
ChaQ[Cha];//待插系列
ifstream infile;
infile.open("infile_H_Q.txt");
for(int i = 0; i < M; i++)
{
infile>>H[i]>>Q[i];
AverageH += H[i],
AverageQ += Q[i];
}
infile.close();
AverageH /= M,
AverageQ /= M;
for(int i = 0; i < M; i++)
{
r += (H[i] - AverageH)*(Q[i] - AverageQ);
sigmaH += pow(H[i] - AverageH, 2);
sigmaQ += pow(Q[i] - AverageQ, 2);
}
r /= pow(sigmaH*sigmaQ, 0.5);
sigmaH = pow(sigmaH/(M - 1), 0.5);
sigmaQ = pow(sigmaQ/(M - 1), 0.5);
b = r*sigmaQ/sigmaH;
a = AverageQ - b*AverageH;
cout<<"線性相關系數r = "<<r<<endl
<<"流量Q倚水位H的回歸系數估計值分別為:"<<endl
<<"a = "<<a<<endl
<<"b = "<<b<<endl
<<"需要進行插值請輸入1,否則請輸入0"<<endl;
cin>>r;
if(r == 1)
{
ofstream outfile;
outfile.open("outfile_ChaQ.txt");
infile.open("infile_ChaH.txt");
for(int i = 0; i < Cha; i++)
{ www.2cto.com
infile>>ChaH[i];
ChaQ[i] = a + b*ChaH[i];
if(ChaQ[i] < 0) ChaQ[i] = 0;//流量不可能為負值
outfile<<ChaQ[i]<<endl;
}
infile.close();
outfile.close();
}
}
作者:Superwen_go