function [ STA ] = ERR3( ERP,RH,N,ID,Message,Fig )
%ERR Help
%   误差分析，给出统计数据并出图
%   给出管道流量观测的相对误差ERP，和水位测量的精度ERH，用蒙特卡洛模拟出J，X1，X2，X3，X4，X5
%   ERP：管道流量观测的相对误差，取值为0或0.0005
%   RH：水位测量的相对误差，取值为0或0.0001
%   ID：需要截取的实验场次，取值范围为1~40
%   Message：需要截取的上下游断面标号，取值为1-2或1-3或2-3

ForAnalysis= Roughness( ERP,RH,ID,Message );
[~,Col]=size(ForAnalysis);
for i=1:Col
    J{i}=[];
    J_1{i}=[];
    J_2{i}=[];
    X1{i}=[];
    X2{i}=[];
    X3{i}=[];
    X4{i}=[];
end

for j=1:N
    ForAnalysis= Roughness( ERP,RH,ID,Message );
    for i=1:Col
        J{i}=[J{i},ForAnalysis{i}(:,1)];
        X1{i}=[X1{i},ForAnalysis{i}(:,2)];
        X2{i}=[X2{i},ForAnalysis{i}(:,3)];
        X3{i}=[X3{i},ForAnalysis{i}(:,4)];
        X4{i}=[X4{i},ForAnalysis{i}(:,5)];
    end
end

JJ=J;
XX1=X1;
XX2=X2;
XX3=X3;
XX4=X4;
clear J X1 X2 X3 X4
for i=1:2
    J{i}=[];X1{i}=[];X2{i}=[];X3{i}=[];X4{i}=[];
end

for i=1:40
    index1=fix((i-1)/20)+1;
    J{index1}=[J{index1};JJ{i}];
    X1{index1}=[X1{index1};XX1{i}];
    X2{index1}=[X2{index1};XX2{i}];
    X3{index1}=[X3{index1};XX3{i}];
    X4{index1}=[X4{index1};XX4{i}];
end

index2=1;
for i=1:2
    MJ=mean(J{i}');
    MX1=mean(X1{i}');
    MX2=mean(X2{i}');
    MX3=mean(X3{i}');
    MX4=mean(X4{i}');
    
    Y=MJ';
    X_1=MX1';
    B1=regress(Y,X_1);
    X_2=[MX1',MX2',MX3',MX4'];
    B2=regress(Y,X_2);
    
    for j=1:20
    MJ=mean(JJ{index2}');
    MX1=mean(XX1{index2}');
    MX2=mean(XX2{index2}');
    MX3=mean(XX3{index2}');
    MX4=mean(XX4{index2}');
    Y=MJ';
    X_1=MX1';
    X_2=[MX1',MX2',MX3',MX4'];
    
    
    STA(index2,1)=B1;
    STA(index2,2)=mean((Y-X_1*B1).^2);%均方误差
    STA(index2,3)=1-sum((Y-X_1*B1).^2)./sum((Y-mean(Y)).^2);%决定性系数
    STA(index2,4)=B2(1);
    STA(index2,5)=B2(2);
    STA(index2,6)=B2(3);
    STA(index2,7)=B2(4);
    STA(index2,8)=mean((Y-X_2*B2).^2);%均方误差
    STA(index2,9)=1-sum((Y-X_2*B2).^2)./sum((Y-mean(Y)).^2);%决定性系数
    
    index2=index2+1;
    
    end
end


end

