📄 rbfnapproximation.m
字号:
% ==========================================================
%
% Neural Networks A Classroom Approach
% Satish Kumar
% Copyright Tata McGraw Hill, 2004
%
% MATLAB code that implements the RBFN approximation
% Reference: Table 8.9;Page 312
%
% ==========================================================
Q = 10; % 10 data points
noise = 0.6; % additive noise level
x = linspace(-2*pi,2*pi,Q); % X sample space
scatter = (2*rand(1,Q) - 1)*noise; % Generate noise
d = (2*sin(x) + x + scatter)'; % Generate Y data
testpts = 100; % Number of test data
testx = linspace(-2*pi, 2*pi, testpts);% Sample space X test
testy = (2*sin(testx) + testx)'; % Generate Y test data
sigma = .5; % Set common spread of basis functions
for i = 1:Q % Generate phi matrix 10 x 5
k=1;
for j = 1:Q/2 % Half the data points are basis centers
phi(i,j) = exp(-(x(i)-x(k))^2/(2*sigma^2));
k=k+2; % choose alternate data point as a basis
end
end
% Compute pseudo inverse
pseudoinv = inv(phi' * phi) * phi';
W = pseudoinv * d; % Compute weights
% Generate phi matrix for test data
for i = 1:testpts
k=1;
for j = 1:Q/2
testphi(i,j) = exp(-(testx(i)-x(k))^2/(2*sigma^2));
k=k+2;
end
end
% Generate approximant
f = testphi* W;
figure(1)
subplot(2,2,1)
hold on
plot(testx, testy,'b')
plot(x, d, 'r^');
plot(testx, f, 'k','LineWidth',2);
axis([-2*pi 2*pi -7 7])
grid on
title('(a)');
sigma = 1;
for i = 1:Q
k=1;
for j = 1:Q/2
phi(i,j) = exp(-(x(i)-x(k))^2/(2*sigma^2));
k=k+2;
end
end
pseudoinv = inv(phi' * phi) * phi';
W = pseudoinv * d;
for i = 1:testpts
k=1;
for j = 1:Q/2
testphi(i,j) = exp(-(testx(i)-x(k))^2/(2*sigma^2));
k=k+2;
end
end
f = testphi* W;
subplot(2,2,2)
hold on
plot(testx, testy,'b')
plot(x, d, 'r^');
plot(testx, f, 'k','LineWidth',2);
axis([-2*pi 2*pi -7 7])
grid on
title('(b)');
sigma = 5;
for i = 1:Q
k=1;
for j = 1:Q/2
phi(i,j) = exp(-(x(i)-x(k))^2/(2*sigma^2));
k=k+2; %choose each alternate data point as a basis
end
end
pseudoinv = inv(phi' * phi) * phi';
W = pseudoinv * d;
for i = 1:testpts
k=1;
for j = 1:Q/2
testphi(i,j) = exp(-(testx(i)-x(k))^2/(2*sigma^2));
k=k+2; %choose each alternate data point as a basis
end
end
f = testphi* W;
subplot(2,2,3)
hold on
plot(testx, testy,'b')
plot(x, d, 'r^');
plot(testx, f, 'k','LineWidth',2);
axis([-2*pi 2*pi -7 7])
grid on
title('(c)');
sigma = 10;
for i = 1:Q
k=1;
for j = 1:Q/2
phi(i,j) = exp(-(x(i)-x(k))^2/(2*sigma^2));
k=k+2; %choose each alternate data point as a basis
end
end
pseudoinv = inv(phi' * phi) * phi';
W = pseudoinv * d;
for i = 1:testpts
k=1;
for j = 1:Q/2
testphi(i,j) = exp(-(testx(i)-x(k))^2/(2*sigma^2));
k=k+2; %choose each alternate data point as a basis
end
end
f = testphi* W;
subplot(2,2,4)
hold on
plot(testx, testy,'b')
plot(x, d, 'r^');
plot(testx, f, 'k','LineWidth',2);
axis([-2*pi 2*pi -7 7])
grid on
title('(d)');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -