当前位置:文档之家› 数学建模实验四:Matlab神经网络以及应用于汽油辛烷值预测

数学建模实验四:Matlab神经网络以及应用于汽油辛烷值预测

实验四:Matlab 神经网络以及应用于汽油辛烷值预测专业年级: 2014级信息与计算科学1班姓名: 黄志锐 学号:0110一、实验目的1. 掌握MATLAB 创建BP 神经网络并应用于拟合非线性函数2. 掌握MATLAB 创建REF 神经网络并应用于拟合非线性函数3. 掌握MATLAB 创建BP 神经网络和REF 神经网络解决实际问题4. 了解MATLAB 神经网络并行运算二、实验内容1. 建立BP 神经网络拟合非线性函数2212y x x =+第一步 数据选择和归一化根据非线性函数方程随机得到该函数的2000组数据,将数据存贮在文件中(下载后拷贝到Matlab 当前目录),其中input 是函数输入数据,output 是函数输出数据。

从输入输出数据中随机选取1900中数据作为网络训练数据,100组作为网络测试数据,并对数据进行归一化处理。

第二步 建立和训练BP 神经网络构建BP 神经网络,用训练数据训练,使网络对非线性函数输出具有预测能力。

第三步 BP 神经网络预测用训练好的BP 神经网络预测非线性函数输出。

第四步 结果分析通过BP 神经网络预测输出和期望输出分析BP 神经网络的拟合能力。

详细MATLAB代码如下:27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54disp(['神经网络的训练时间为', num2str(t1), '秒']); %% BP网络预测% 预测数据归一化inputn_test = mapminmax('apply', input_test, inputps); % 网络预测输出an = sim(net, inputn_test);% 网络输出反归一化BPoutput = mapminmax('reverse', an, outputps);%% 结果分析figure(1);plot(BPoutput, ':og');hold on;plot(output_test, '-*');legend('预测输出', '期望输出');title('BP网络预测输出', 'fontsize', 12);ylabel('函数输出', 'fontsize', 12);xlabel('样本', 'fontsize', 12);% 预测误差error = BPoutput-output_test;figure(2);plot(error, '-*');title('BP神经网络预测误差', 'fontsize', 12);ylabel('误差', 'fontsize', 12);xlabel('样本', 'fontsize', 12);figure(3);plot((output_test-BPoutput)./BPoutput, '-*');title('BP神经网络预测误差百分比');errorsum = sum(abs(error));MATLAB代码运行结果截图如下所示:MATLAB代码运行结果如下所示:图1 BP神经网络预测输出图示图2 BP神经网络预测误差图示图3 BP 神经网络预测误差百分比图示2. 建立RBF 神经网络拟合非线性函数22112220+10cos(2)10cos(2)y x x x x ππ=-+-第一步 建立exact RBF 神经网络拟合, 观察拟合效果详细MATLAB 代码如下:MATLAB代码运行结果如下所示:图4 RBF神经网络拟合效果图第二步建立approximate RBF神经网络拟合详细MATLAB代码如下:13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41F = 20+x1.^2-10*cos(2*pi*x1)+x2.^2-10*cos(2*pi*x2); %% 建立RBF神经网络% 采用approximate RBF神经网络。

spread为默认值net = newrb(x, F);%% 建立测试样本% generate the testing datainterval = ;[i, j] = meshgrid:interval:;row = size(i);tx1 = i(:);tx1 = tx1';tx2 = j(:);tx2 = tx2';tx = [tx1; tx2];%% 使用建立的RBF网络进行模拟,得出网络输出ty = sim(net, tx);%% 使用图像,画出3维图% 真正的函数图像interval = ;[x1, x2] = meshgrid:interval:;F = 20+x1.^2-10*cos(2*pi*x1)+x2.^2-10*cos(2*pi*x2); subplot(1, 3, 1);mesh(x1, x2, F);zlim([0, 60]);title('真正的函数图像')% 网络得出的函数图像v = reshape(ty, row);subplot(1, 3, 2);mesh(i, j, v);MATLAB代码运行结果截图如下所示:MATLAB代码运行结果如下所示:图5 RBF神经网络拟合结果图示讨论题:对于非线性函数220.252220.11212(){sin [50()]1}y x x x x =+⋅++(1)分别建立BP 神经网络和RBF 神经网络拟合并比较两者的性能差异。

(2)就BP 神经网络验证单线程运算和并行运算的运行时间差异。

(1)详细MATLAB 代码如下:MATLAB代码运行结果截图如下所示:MATLAB代码运行结果如下所示:图6 BP神经网络拟合效果图详细MATLAB代码如下:MATLAB代码运行结果如下所示:图7 RBF神经网络拟合效果图详细MATLAB代码如下:5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33% 产生2*sample的随机矩阵,随机数在区间[a, b]中a = -2;b = 2;x = a + (b-a).*rand(2, sample);% 计算网络输出y值y = f(x(1, :), x(2, :));%% 训练数据归一化input_train = x;output_train = y;[input_n, input_ps] = mapminmax(input_train);[output_n, output_ps] = mapminmax(output_train);%% 构建和训练BP神经网络% BP神经网络构建hiddenSizes = 500;trainFcn = 'trainlm';% net = newff(input_n, output_n, hiddenSizes);net = fitnet(hiddenSizes,trainFcn);= 300;= ;= ;net = train(net, input_n, output_n);%% BP网络预测input_test = a + (b-a).*rand(2, sample*;output_test = f(input_test(1, :), input_test(2, :)); inputn_test = mapminmax('apply', input_test, input_ps); prediction = sim(net, inputn_test);BP_output = mapminmax('reverse', prediction, output_ps); %% 结果分析figure(1);34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62plot(BP_output, ':og');hold on;plot(output_test, '-*');legend('预测输出', '期望输出');title('BP网络预测输出', 'fontsize', 12);ylabel('函数输出', 'fontsize', 12);xlabel('样本', 'fontsize', 12);% 预测误差error = BP_output-output_test;figure(2);plot(error, '-*');title('BP神经网络预测误差', 'fontsize', 12); ylabel('误差', 'fontsize', 12);xlabel('样本', 'fontsize', 12);figure(3);plot((output_test-BP_output)./BP_output, '-*'); title('BP神经网络预测误差百分比');errorsum = sum(abs(error));%% 使用图像,画出3维图% 真正的函数图像interval = ;[x1, x2] = meshgrid(a:interval:b);y = f(x1, x2);subplot(1, 3, 1);mesh(x1, x2, y);zlim([-2, 5]);title('真正的函数图像')% 网络得出的函数图像tx1 = x1(:);MATLAB代码运行结果如下所示:图8 BP神经网络预测输出图示图9 BP神经网络预测误差图示图10 BP神经网络拟合效果图详细MATLAB代码如下:13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41spread = ;net = newrb(x, F, spread );%% 建立测试样本interval = ;[i, j] = meshgrid(a:interval:b);row = size(i);tx1 = i(:);tx1 = tx1';tx2 = j(:);tx2 = tx2';tx = [tx1; tx2];%% 使用建立的RBF网络进行模拟,得出网络输出ty = sim(net, tx);%% 使用图像,画出3维图% 真正的函数图像interval = ;[x1, x2] = meshgrid(a:interval:b);F = f(x1, x2);subplot(1, 3, 1);mesh(x1, x2, F);zlim([-2, 5]);title('真正的函数图像')% 网络得出的函数图像v = reshape(ty, row);subplot(1, 3, 2);mesh(i, j, v);zlim([-2, 5]);title('RBF神经网络结果')% 误差图像MATLAB 代码运行结果如下所示:图11 RBF 神经网络拟合效果图通过分析上述结果可知,对于非线性函数220.252220.11212(){sin [50()]1}y x x x x =+⋅++RBF 神经网络(spread 值为)的拟合效果比BP 神经网络(隐含层神经元的个数为500个)的拟合效果更好,且RBF 神经网络性能更佳(耗时更少)。

相关主题