欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

灰色预测程序

程序员文章站 2022-07-14 14:25:58
...
  • 结果图

灰色预测程序灰色预测程序

  • 源码
function pre_data = Gray_Prediction(data, m)
%{
函数功能:灰色预测;
输入:
  data:参考数据,一行;
  m:预测点数;
输出:
  pre_data:预测数据;
示例:
clear; clc;
% data = [89677, 99215, 109655, 120333, 135823, 159878, 182321, 209407, 246619, 300670];
% data = [174, 179, 183, 189, 207, 234, 220.5, 256, 270, 285];
m = 2;
t1 = 1 : length(data);
t2 = 1 : m + length(data);
pre_data = Grey_Prediction(data, m);
plot(t1, data, 'o', t2, pre_data, 'linewidth', 2)
%} 
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
if nargin < 2
   error('输入参数不足!');
end
% 原始数据累加
csum_data = cumsum(data);  
n = length(data);
csum_at = zeros(1, n - 1);
for i = 1 : n - 1
    csum_at(i) = (csum_data(i) + csum_data(i + 1))/2;  % 生成累加矩阵
end
% 计算待定参数的值
D = data(2 : end)';
E = [- csum_at; ones(1, n - 1)];
c = (E*E')\E*D;
a = c(1);
b = c(2);
% 预测后续数据
pre = zeros(1, n + m);
pre(1) = data(1);
for i = 2 : n + m
    pre(i) = (data(1) - b/a)/exp(a*(i - 1)) + b/a ;
end
% 计算预测数据
pre_data = zeros(1, n + m);
pre_data(1) = data(1);
for i = 2 : n + m
    pre_data(i) = pre(i) - pre(i - 1); 
end