共有七个完整算法%1.基于聚类的RBF网设计算法%一维输入,一维输出,逼近效果很好!SamNum=100;%总样本数TestSamNum=101;%测试样本数InDim=1;%样本输入维数ClusterNum=10;%隐节点数,即聚类样本数Overlap=1.0;%隐节点重叠系数%根据目标函数获得样本输入输出rand('state',sum(100*clock))NoiseVar=0.1;Noise=NoiseVar*randn(1,SamNum);SamIn=8*rand(1,SamNum)-4;SamOutNoNoise=1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);SamOut=SamOutNoNoise+Noise;TestSamIn=-4:0.08:4;TestSamOut=1.1*(1-TestSamIn+2*TestSamIn.^2).*exp(-TestSamIn.^2/2);figureholdongridplot(SamIn,SamOut,'k+')plot(TestSamIn,TestSamOut,'r--')xlabel('Inputx');ylabel('Outputy');Centers=SamIn(:,1:ClusterNum);NumberInClusters=zeros(ClusterNum,1);%各类中的样本数,初始化为零IndexInClusters=zeros(ClusterNum,SamNum);%各类所含样本的索引号while1,NumberInClusters=zeros(ClusterNum,1);%各类中的样本数,初始化为零IndexInClusters=zeros(ClusterNum,SamNum);%各类所含样本的索引号%按最小距离原则对所有样本进行分类fori=1:SamNumAllDistance=dist(Centers',SamIn(:,i));[MinDist,Pos]=min(AllDistance);NumberInClusters(Pos)=NumberInClusters(Pos)+1;IndexInClusters(Pos,NumberInClusters(Pos))=i;end%保存旧的聚类中心OldCenters=Centers;fori=1:ClusterNumIndex=IndexInClusters(i,1:NumberInClusters(i));Centers(:,i)=mean(SamIn(:,Index)')';end%判断新旧聚类中心能否一致,是则结束聚类EqualNum=sum(sum(Centers==OldCenters));ifEqualNum==InDim*ClusterNum,break,endend%计算各隐节点的扩展常数(宽度)AllDistances=dist(Centers',Centers);%计算隐节点数据中心间的距离(矩阵)Maximum=max(max(AllDistances));%找出其中最大的一个距离fori=1:ClusterNum%将对角线上的0替换为较大的值AllDistances(i,i)=Maximum+1;endSpreads=Overlap*min(AllDistances)';%以隐节点间的最小距离作为扩展常数%计算各隐节点的输出权值Distance=dist(Centers',SamIn);%计算各样本输入离各数据中心的距离SpreadsMat=repmat(Spreads,1,SamNum);HiddenUnitOut=radbas(Distance./SpreadsMat);%计算隐节点输出阵HiddenUnitOutEx=[HiddenUnitOut'ones(SamNum,1)]';%考虑偏移W2Ex=SamOut*pinv(HiddenUnitOutEx);%求广义输出权值W2=W2Ex(:,1:ClusterNum);%输出权值B2=W2Ex(:,ClusterNum+1)
1