plot 运行出错。
Show older comments
x=[158.86
159.52
161.04
162.38
164.08
165.32
166.79
168.36
169.97
172.17
174.49
176.59
179.63
181.52
183.79
185.61
187.67
189.38
190.53
191.77
192.67
193.14
193.90
193.74
194.38
194.88
195.52
195.83
196.16
196.53
197.68
197.98
197.98
198.61
197.42
198.06]';
% 该脚本用来做NAR神经网络预测
% 作者:
lag=3; % 自回归阶数
iinput=x; % x为原始序列(行向量)
n=length(iinput);
%准备输入和输出数据
inputs=zeros(lag,n-lag);
for i=1:n-lag
inputs(:,i)=iinput(i:i+lag-1)';
end
targets=x(lag+1:end);
%创建网络
hiddenLayerSize = 10; %隐藏层神经元个数
net = fitnet(hiddenLayerSize);
% 避免过拟合,划分训练,测试和验证数据的比例
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
%训练网络
[net,tr] = train(net,inputs,targets);
%% 根据图表判断拟合好坏
yn=net(inputs);
errors=targets-yn;
figure, ploterrcorr(errors) %绘制误差的自相关情况(20lags)
figure, parcorr(errors) %绘制偏相关情况
%[h,pValue,stat,cValue]=lbqtest(errors) %Ljung-Box Q检验(20lags)
figure,plotresponse(con2seq(targets),con2seq(yn))%看预测的趋势与原趋势
%figure, ploterrhist(errors) %误差直方图
%figure, plotperform(tr) %误差下降线
%% 下面预测往后预测几个时间段
fn=7; %预测步数为fn。
f_in=iinput(n-lag+1:end)';
f_out=zeros(1,fn); %预测输出
% 多步预测时,用下面的循环将网络输出重新输入
for i=1:fn
f_out(i)=net(f_in);
f_in=[f_in(2:end);f_out(i)];
end
% 画出预测图
figure,plot(1978:2013,iinput,'b',2013:2055,[iinput(end),f_out],'r')
title('人口数量统计与预测 ') %添加图像标题
xlabel('年份')
ylabel('人口数量(万人)')
最后运行的时候出现==》Error using plot
Vectors must be the same lengths.
大神帮我解答一下
Answers (0)
Categories
Find more on 线图 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!