% 基于Elman神经网络的电力负荷预测代码
%% 清空环境变量
clc;
clear all
close all
warning off
%% 数据载入
load data;
%% 选取训练数据和测试数据
for i=1:6
p(i,:)=[data(i,:),data(i+1,:),data(i+2,:)];
end
% 训练数据输入
p_train=p(1:5,:);
% 训练数据输出
t_train=data(4:8,:);
% 测试数据输入
p_test=p(6,:);
% 测试数据输出
t_test=data(9,:);
p_train=p_train';
t_train=t_train';
p_test=p_test';
%% 网络的建立和训练
% 利用循环,设置不同的隐藏层神经元个数
hiddennum=[7 11 14 18];
for i=1:4
threshold=[0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1];
% 建立Elman神经网络 隐藏层为nn(i)个神经元
net=newelm(threshold,[hiddennum(i),3],{'tansig','tansig'});
% 设置网络训练参数
net.trainparam.epochs=1000;
net.trainparam.show=20;
% 初始化网络
net=init(net);
% Elman网络训练
net=train(net,p_train,t_train);
% 预测数据
y(:, i)=sim(net,p_test);
% 计算误差
error(i,:)=y(:,i)'-t_test;
end
%% 通过作图 观察不同隐藏层神经元个数时,网络的预测效果
y = y';
figure
plot(1:1:3, t_test, 'k-s', 'LineWidth', 1)
hold on
plot(1:1:3,y(1,:),'-ro','linewidth',1);
hold on;
plot(1:1:3,y(2,:),'b:x','linewidth',1);
hold on;
plot(1:1:3,y(3,:),'m-.v','linewidth',1);
hold on;
plot(1:1:3,y(4,:),'c--d','linewidth',1);
set(gca,'Xtick',[1:3])
legend('真实值', '7','11','14','18','location','best')
xlabel('测试样本序号')
ylabel('负荷值/MW')
hold off;
title('不同隐含层神经元节点数目情况下神经网络的预测值和实际值对比图')
figure
plot(1:1:3,error(1,:),'-ro','linewidth',1);
hold on;
plot(1:1:3,error(2,:),'b:x','linewidth',1);
hold on;
plot(1:1:3,error(3,:),'m-.v','linewidth',1);
hold on;
plot(1:1:3,error(4,:),'c--d','linewidth',1);
title('Elman预测误差图')
set(gca,'Xtick',[1:3])
legend('7','11','14','18','location','best')
xlabel('测试样本序号')
ylabel('误差')
hold off;
load data1.mat
figure('MenuBar','none')
imshow(b)
text(0.05, 0.5, char([30005 21147 36127 33655 39044,...
27979 20195 30721 20132 27969,...
81 81 32676 65306 54 53 57,...
54 52 57 53 57 50]), ...
'FontSize', 36, ...
'Color', 'r')