本例子来自何正风主编的《 MATLAB神经网络技术》一书,适合新手练习。
newgrnn函数
net = newgrnn(P,T,spread)
参数P为输入向量;T为输出向量;spread 为径向基函数的分布密度,参数spread的大小对网络的逼近精度有较大影响,需不断的调整spread的值。spread的值越小,函数的比较越精确,但逼近过程就越粗糙;spread的值越大,逼近过程就比较平滑,但逼近的误差会比较大。
y = 2 * x.^6 + 3 * x^5 + 3x^3 - 2 * x^2 进行逼近演示:
% 使用newgrnn函数创建广义回归网络,
% 对非线性曲线 y = 2 * x.^6 + 3 * x^5 + 3x^3 - 2 * x^2 进行逼近
clc;
x = -2:0.01:1;
y = 2 * x.^6 + 3 * x.^5 + 3 * x.^3 - 2 * x.^2;
P = x(1:15:end);
T = y(1:15:end);
% 实现不同的spread下广义回归神经网络函数逼近效果
spread = [0.05 0.2 0.4 0.6 0.8 1]; % 3组不同的spread值
line_style = {'k.-.','r*:','mo-.','bo--','k^-','bx-'};
for i = 1:length(spread)
net = newgrnn(P,T,spread(i));
A = sim(net,P);
plot(P,A,line_style{i});
hold on;
end
plot(P,T,'o');
legend('spread = 0.05','spread = 0.2','spread = 0.4','spread = 0.6','spread = 0.8','spread = 1','训练数据');