www.gusucode.com > BP神经网络算法的matlab代码 > BP神经网络算法的matlab代码/bp源程序/bp2.m

    

clear

% 输入历史实测数据(34天)的汽轮机振动的日平均峰峰值
day=[40.6,37.2,36.2,36.8,36.4,36.8,35.6,37.2,35.2,35.8,36,35.9,39,40,40.4,39.2,36.6,35.8,34.8,35.8,37,38.2,41.2,40.2,37.6,37.8,39.8,40.8,38.8,38.6,42,47.2,46.6,44.2];


dayhistory=day(1:30);%取其中三十天作为历史数据样本
dayhismod=reshape(dayhistory,5,6);% 将历史数据分为6个样本,每个大小为5,其中reshape是以列排序的
dayday=day(1:25);% 取其中的前25天
daypost=day(6:30);%取其中的随后25天
p=reshape(dayday,5,5);% 将前25天数据分为5行5列矩阵作为网络的训练输入样本
t=reshape(daypost,5,5); %将随后的25天分为5行5列矩阵作为网络的目标输出向量
daylast=day(26:30);
h3=reshape(daylast,5,1);% 将倒数第二个样本作为网络测试时的输入样本
r=6:30;
rr=reshape(r,5,5);


%%%%%%%%%%%%%% 新建网络bp %%%%%%%%%%%%%%%%
net=newff(minmax(p),[5,5],{'purelin' 'purelin'},'trainlm');
y1=sim(net,p);
% 新建网络,其中minmax(p)为p的没一次输入的最大最小值向量
% 两层的传递函数均为purelin
% 训练函数为trainlm
% 所训练的网络大小为[5,5]
% 仿真训练前的网络

%%%%%%%%%%%  进行网络训练  %%%%%%%%%%%%%%
% network parameters:
%   epochs--epochs of the train
%   goal--errors goal of the network
%   lr--learning rate
%   shows--epochs between the displays
%   time--Maximum time to train in seconds
net.trainParam.epochs=200;  % 训练次数
nettrainParam.goal=0.0001;  % 误差期望值
% returns of the train:
%   net--New network
%    tr--Training record (epoch and perf).
%     Y--Network outputs.
%     E--Network errors.
[net,tr,Y,E]=train(net,p,t); 


%%%%%%%%%%%  网络测试 %%%%%%%%%%%%%%%%
% input the testing points here %

y21=sim(net,p);
y2=reshape(y21,1,25);
clf
plot(r,y2,'b-^')
hold on
plot(1:34,day,'r-*')
%%%%%%%%%%%%% 预测 %%%%%%%%%%%%%%%
y3=sim(net,h3);
plot(31:35,y3,'-*')
hold on

title('神经网络训练结果');
xlabel('时间(天)');
ylabel('仿真输出结果');
legend('仿真模拟值','实际值','神经网络预测值');
%%%%%%%%%%%%%%%%%%  绘制误差曲面 %%%%%%%%%%%%%
x=1:5;
y=1:5;
plot3(x,y,E(x,y))