水文分析与计算——缺失流量线性插值及相关分析
[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.zzzyk.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
补充:软件开发 , C++ ,