www.pudn.com > RBFFunction.rar > RBFFunction.m


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%%%  RBF网络用于函数逼近                                 20051218 
%%%%  神经网络工具箱 
% clear all;clc; 
%  
% X = [0:0.1:1]; 
% T=sin(5*X)+X; 
%  
% net = newrb(X,T); 
% x1 = [0.05:0.1:0.95]; 
% Y1 = sim(net,x1); 
% T2 = sim(net,X); 
%  
% figure(1); 
% plot(X,T,'b-',x1,Y1,'r+',X,T2,'g-'); 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%%%  RBF网络用于函数逼近                                  20051218 
%%%%  未使用神经网络工具箱,离线算法 
%%%% 
% clear;clc; 
%  
% X = [0:0.05:1]; 
% N = length(X);
% Nr = 5; 
% T = sin(5*X).^2+X.^2+cos(X); 
%  
% %%%% 计算中心矢量 
% dc = 1; 
% t = 0; 
% c = randn(1,Nr); 
% while dc>0.0001 
%     classNum = zeros(1,Nr); 
%     classSum = zeros(1,Nr); 
%     for k=1:N 
%         d = (X(k)*ones(1,Nr)-c).^2; 
%         [m,I] = min(d); 
%         classNum(I) = classNum(I)+1; 
%         classSum(I) = classSum(I)+X(k); 
%     end 
%     for i = 1:Nr 
%         if classNum(i)>0.5 
%             classSum(i) = classSum(i)/classNum(i); 
%         else 
%             classSum(i)=0; 
%         end 
%     end 
%     c1 = classSum; 
%     temp0 = sum((c1-c).^2); 
%     temp1 = sum(c1.^2); 
%     dc = temp0/temp1; 
%     t = t+1; 
%     c = c1; 
% end 
%  
% %%%% 计算方差参数 
% classVar = zeros(1,Nr); 
% classNum = zeros(1,Nr); 
% for k=1:N 
%     d = (X(k)*ones-c).^2; 
%     [m,I]=min(d); 
%     classNum(I) = classNum(I)+1; 
%     classVar(I) = classVar(I)+(X(k)-c(I))^2; 
% end 
% classVar = classVar./classNum; 
% %classVar = 2; 
% %%%% 学习输出层权植 
% dww=1; 
% a = 0.2; 
% nn = 0; 
% W0 = randn(1,Nr); 
% while dww>0.01 
% dww = 0; 
% a = a/(1+nn/1000); 
% W = W0; 
% for k =1:N 
%     R = exp(-(X(k)*ones(1,Nr)-c).^2./classVar/2); 
%     y = W*R'; 
%     dW = a*(T(k)-y)*R; 
%     W = W+dW; 
% end 
% dww = norm(W-W0); 
% W0 = W; 
% nn= nn+1; 
% end 
%  
% %%%% 测试学习算法有效性 
% t1 = [0:0.05:1]; 
% Yout1 =zeros(1,N); 
% for i =1:N 
%     R = exp(-(t1(i)*ones(1,Nr)-c).^2./classVar/2); 
%     Yout1(i) = W*R'; 
% end 
%  
% t2 = [0.025:0.05:0.975]; 
% Yout2 =zeros(1,length(t2)); 
% for i =1:length(t2) 
%     R = exp(-(t2(i)*ones(1,Nr)-c).^2./classVar/2); 
%     Yout2(i) = W*R'; 
% end 
% figure(1); 
% plot(t1,Yout1,'b-',t2,Yout2,'r+'); 
% figure(2); 
% plot(X,T,'b-'); 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%%%  RBF网络用于函数逼近                                       20051219     
%%%%  未使用神经网络工具箱,且是实时的,在线算法 
%%%% 
clear;clc; 
 
X = [0:0.1:1]; 
N = length(X); 
Nr = 6; 
T=exp(X)+X.^2+sin(X); 
%%%% 计算中心矢量 
a = 0.2; 
c = randn(1,Nr); 
for k =1:N 
    d = (X(k)*ones(1,Nr)-c).^2; 
    [m,I]=min(d); 
    c1 = c; 
    c1(I) = c(I)+a*[X(k)-c(I)]; 
    c = c1; 
    a = a/(1+sqrt(k/Nr)); 
end 
%%%% 计算方差 
deta = zeros(1,Nr); 
for k=1:N 
    deta = deta + (X(k)*ones(1,Nr)-c).^2; 
end 
deta = deta/N; 
%%%% 权值迭代 
R = zeros(1,Nr); 
W0 = 0.2*randn(1,Nr); 
dww = 1; 
a2 = 0.4; 
n2 = 0; 
while dww>0.001 
W = W0; 
y = zeros(1,N); 
for k =1:N 
    R = exp(-(X(k)*ones(1,Nr)-c).^2./(2*deta)); 
    y(k) = W*R';%输出 
    dW = a2*(T(k)-y(k))*R; 
    W = W+dW;  %更新权值 
end 
dww = norm(W-W0); 
n2 = n2+1; 
W0 = W; 
dE = 0; 
for k =1:N 
    dE = dE+1/2*(T(k)-y(k))^2; 
end 
E(n2) = dE; 
end 
%%%% 测试算法 
t1 = [0:0.1:1]; 
Yout1 =zeros(1,N); 
for i =1:N 
    R = exp(-(t1(i)*ones(1,Nr)-c).^2./(2*deta)); 
    Yout1(i) = W*R'; 
end 
t2 = [0:0.05:1]; 
Yout2 =zeros(1,length(t2)); 
for i =1:length(t2) 
    R = exp(-(t2(i)*ones(1,Nr)-c).^2./(2*deta)); 
    Yout2(i) = W*R'; 
end 
figure(1); 
plot(t1,Yout1,'b-',t2,Yout2,'r+',X,T,'g-'); 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%