%BP神经网络实现与数据预测closeallclear%%%读取文件(根据数据路径更改)filename='E:\y\12.txt';data=textread(filename);%取前25组数据作为样本data1=data(:,1);data2=data(:,2);xaxis=data1(1:25)';yaxis=data2(1:25)';%%%创建BP神经网络n=20;%神经元个数net=newff(minmax(xaxis),[n,1],{'tansig''purelin'},'trainlm');%当前输入层权值和阈值inputWeights=net.IW{1,1}inputbias=net.b{1}%当前网络层权值和阈值layerWeights=net.LW{2,1}layerbias=net.b{2}%BP网络训练net.trainParam.epochs=70;%网络训练时间设置net.trainParam.goal=0.01;%网络训练精度设置net=train(net,xaxis,yaxis);%开始训练网络%%%仿真xs=data1';ys=data2';y=sim(net,xs);%%%输出结果figure(1)plot(xs,ys,'-ob','LineWidth',2,'MarkerSize',10);holdonplot(xs,y,'-*r');legend('original','prediction');xlabel('periods');ylabel('sedimentation(mm)');%计算误差与标准差E=ys-y;E1=E(1:25);MSE=mse(E1);fprintf('%d',MSE);%网络工具箱也会提供方差的值