clc;clear ;close all
load datafile
x1=data(:,1);
x2=data(:,2);
x3=data(:,3);
x4=data(:,4);
y1=data(:,5);
%%%数据样本181组:length(x1)=length(x2)=length(x3)=length(x4)=181;
meanx1=sum(x1)/length(x1);
meanx2=sum(x2)/length(x2);
meanx3=sum(x3)/length(x3);
meanx4=sum(x4)/length(x4);
%%%数据处理——归一化:
for i=1:181
x1Norm(i)=(x1(i,1)-meanx1)/std(x1);
x2Norm(i)=(x2(i,1)-meanx2)/std(x2);
x3Norm(i)=(x3(i,1)-meanx3)/std(x3);
x4Norm(i)=(x4(i,1)-meanx4)/std(x4);
end
%前150作为训练样本,训练神经网络
for m1=1:150
Intput(1,m1)=x1Norm(m1);
end
for m2=1:150
Intput(2,m2)=x2Norm(m2);
end
for m3=1:150
Intput(3,m3)=x3Norm(m3);
end
for m4=1:150
Intput(4,m4)=x4Norm(m4);
end
for m5=1:150
Tempout(m5)=y1(m5);
end
%%后31个样本用来测试神经网络
k=1;
for n1=151:181
Testinput(1,k)=x1Norm(n1);
k=k+1;
end
k=1;
for n2=151:181
Testinput(2,k)=x2Norm(n2);
k=k+1;
end
k=1;
for n3=151:181
Testinput(3,k)=x3Norm(n3);
k=k+1;
end
k=1;
for n4=151:181
Testinput(4,k)=x4Norm(n4);
k=k+1;
end
k=1;
for n5=151:181
Testoutput(k)=y1(n5);
k=k+1;
end
%%%训练样本:输入:Intput;输出:Tempout
%%%测试样本:输入:Testinput;输出:Testoutput
tic
[input,minp,maxp,output,mint,maxt]=premnmx(Intput,Tempout);
net=newff(minmax(Intput),[9,1],{'tansig','purelin'},'traingdm');
inputweights=net.IW{1,1};
inputbias=net.b{1};
layerweights=net.LW{2,1};
layerbias=net.b{2};
net.trainParam.epochs=5000;
net.trainParam.goal=0.01;
net.trainParam.show=50;
net.trainParam.lr=0.1;
net.trainParam.mc=0.9;
net=train(net,input,output);
out=sim(net,Testinput);
yuce= postmnmx(out,mint,maxt);%help " postmnmx"
e=Testoutput-yuce;
MSE=mse(e);
%Testoutput(1)=yuce(1);
%%%画图
figure;
subplot(2,1,1);
plot((1:31),Testoutput,'-*',(1:31),yuce,'-o');
ylabel('电力负荷');
title('bp负荷预测');
legend('实际负荷','预测负荷');
subplot(2,1,2);
erate=(Testoutput-yuce)./Testoutput;
stem(erate);
ylabel('相对误差值');
title('预测相对误差');
toc
评论0