灰色预测GM(1,1)连续导入百组数据,进行级比分析计算C的具体值并输出——完整代码(自动输出)

%导入原始数据
c = 0;
flag = 0;
fore_num = 5;%(年)
hyq = 1;
flag_1 = 1;
y = zeros(154+2 , 60+fore_num*12);
y( 1 , 1 ) = 2008;
y( 1 , 1+12 ) = 2009;
y( 1 , 1+12+12 ) = 2010;
y( 1 , 1+12+12+12 ) = 2011;
y( 1 , 1+12+12+12+12 ) = 2012;
for list_1 = 1 : 12
    y( 2,list_1 ) = list_1;
end
for list_2 = 1+12 : 12+12
    y( 2,list_2 ) = list_2-12;
end
for list_3 = 1+12+12 : 12+12+12
    y( 2,list_3 ) = list_3-12-12;
end
for list_4 = 1+12+12+12 : 12+12+12+12
    y( 2,list_4 ) = list_4-12-12-12;
end
for list_5 = 1+12+12+12+12 : 12+12+12+12+12
    y( 2,list_5 ) = list_5-12-12-12-12;
end
ori_matrix = xlsread('各城镇月度需求原始数据' , 1 ,' B3  :  BI156 ' );
for h = 1 : 154
x0 = ori_matrix(h , :);
b = x0(1,1);
length_x0 = length(x0);
%Ratio为检验系数,即数列的级比
%x0 = x0 + 40;
Ratio = zeros(1 , length_x0  -  1);
for i = 1 : length_x0  -  1
    Ratio(i) = x0(i) / x0(i+1);
end
x1 = ones(1 , length_x0);
%对数据的级比做判断
if ( exp(-2 / (length_x0 + 1)) < min(Ratio) && max(Ratio) < exp(2 / (length_x0 + 1)) )
%0.83381.1994    
%对x0数组进行维数累加求和,得到x1
x1 = cumsum(x0);
%生成均值序列  alpha自取
alpha = 0.5;%input('please enter alpha number:  ');
z1 = zeros(1 , length_x0 - 1);
for k = 2 : length_x0
    z1(k-1) = alpha * x1(k) + (1-alpha) * x1(k-1);
end
%解方程得到最小二乘法的解为 a_b 
y0 = x0( 2 : 1 : length_x0 );%()中没有,说明只有行没有列
B = [-z1 ; ones( 1 , length_x0 - 1 )];
%gj为a和b的数据
a_b = ( inv(B * B') ) * B * y0';

n = fore_num*12;%input('预测的数据个数为:');
for k = 1 : length_x0 + n - 1
x1(k+1) = ( (x0(1) - (a_b(2) / a_b(1)) ) * ( exp(-k * a_b(1)) ) ) + a_b(2) / a_b(1);
end
x0(1) = x1(1);
for k = 2 : length_x0 + n
  x0(k) = x1(k) - x1(k-1);
end
else
    disp('数据不满足检验,将对数据进行处理')
    while(1)
        x0 = x0 + 1;
        Ratio = zeros(1 , length_x0 - 1);
        for i = 1 : length_x0 - 1
            Ratio(i) = x0(i) / x0(i+1);
        end
        if( exp(-2 / (length_x0+1)) < min(Ratio) && max(Ratio) < exp(2 / (length_x0+1)) )
            break;
        end
    end
    c = x0(1,1) - b;
    fprintf( '数据进行处理完成,c的值为%d\n' , c ); 
    flag = 1;
end
if(flag == 1)
length_x0 = length(x0);
%Ratio为检验系数,即数列的级比
%x0 = x0 + 40;
Ratio = zeros(1 , length_x0 - 1);
for i = 1:length_x0 - 1
    Ratio(i) = x0(i) / x0(i+1);
end
x1 = ones(1 , length_x0);
%对数据的级比做判断
if ( exp(-2 / (length_x0+1)) < min(Ratio) && max(Ratio) < exp(2 / (length_x0+1)) )
%0.83381.1994    
%对x0数组进行维数累加求和,得到x1
x1 = cumsum(x0);
%生成均值序列  alpha自取
alpha = 0.5;%input('please enter alpha number:  ');
z1 = zeros(1 , length_x0 - 1);
for k = 2 : length_x0
    z1(k-1) = alpha * x1(k) + (1-alpha) * x1(k-1);
end
%解方程得到最小二乘法的解为 a_b 
y0 = x0( 2 : 1 : length_x0 );%()中没有,说明只有行没有列
B = [-z1 ; ones( 1 , length_x0-1 )];
%gj为a和b的数据
a_b = ( inv(B * B') ) * B * y0';

n = fore_num * 12;%input('预测的数据个数为:');
for k = 1 : length_x0 + n - 1
x1(k+1) = ( (x0(1) - (a_b(2) / a_b(1)) ) * ( exp(-k * a_b(1)) ) ) + a_b(2) / a_b(1);
end
x0(1) = x1(1);
for k = 2 : length_x0 + n
  x0(k) = x1(k) - x1(k-1);
end
end
end
x0 = x0 -c ;
if(flag_1 ==1)
for q = 1:fore_num
    y( 1 , 1+12+12+12+12+12*q ) = 2012+q;
end
for o = 1 : (fore_num*12)
    y( 2 , 12+12+12+12+12+o ) = hyq;
    hyq = hyq + 1;
    if(hyq == 13)
        hyq = 1;
    end
end
flag_1 = 0;
end
y( h+2 , : ) = x0;
fprintf( '写入第%d行\n' , h ); 
end
%plot(x0,'*')
xlswrite( '预测完成数据_级比_总' , y , 1)
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值