%利用神经网络工具箱对一个正弦函数进行拟合,源码如下:clc;clear;closeall;%-----------------------------------------------%产生训练样本与测试样本P1=-1:0.05:1;%训练样本T1=sin(P1*pi);%训练目标P2=-1:0.02:1;%测试样本T2=sin(P2*pi);%测试目标%%%BP网络模型构建%-------------------------------------------%设置网络参数NodeNum=10;%隐藏节点数TypeNum=1;%输出维数TF1='tansig';TF2='purelin';%判别函数(缺省值)net1=newff(minmax(P1),[NodeNumTypeNum],{TF1TF2});%net1=init(net1);%神经网络初始化%------------------------------------------------------%指定训练参数net1.trainFcn='trainlm';net1.trainParam.show=20;%训练显示间隔net1.trainParam.lr=0.3;%学习步长-traubgd,traubgdmnet1.trainParam.epochs=50;%最大训练次数net1.trainParam.goal=0.001;%最小均方误差net1.trainParam.time=inf;%最大训练时间%-------------------------------------------------------%训练net1=train(net1,P1,T1);%训练%--------------------------------------------------%测试Y1=sim(net1,P1);%训练样本实际输出Y2=sim(net1,P2);%测试样本实际输出MSE1=mean((T1-Y1).^2);MSE2=mean((T2-Y2).^2);%%RBF网络模型构建%-------------------------------------------%设置网络参数eg=0.001;%设置均方差训练精度sc=1;%设置散布函数net2=newrb(P1,T1,eg,sc);%网络训练Y3=sim(net2,P1);%训练样本实际输出Y4=sim(net2,P2);%测试样本实际输出MSE3=mean((T1-Y3).^2);MSE4=mean((T2-Y4).^2);%-----------------------------------------%结果作图figure(1)plot(P2,T2,'r',P2,Y2,'b',P2,Y4,'G');title('r为真实值,b为BP预测值,G为rbf预测值');%%%%结果显示设置精度为0.1设置精度为0.01设置精度为0.001