径向基函数神经网络(RBFNN)的三维插值

本文拟合z=xe(-x2+y)函数,同时计算最大绝对误差和平均绝对误差插值。matlab代码如下:

clc;
x0=zeros(100,1)';
y0=zeros(100,1)';
z0=zeros(100,1)';
for i=1:100
    x0(i)=-2+4*rand*(1);
    y0(i)=-2+4*rand*(1);
    z0(i)=x0(i)*exp(-x0(i)^2+y0(i));
end
figure(1);
plot3(x0,y0,z0,'*');
hold on;

%RBF
sigma=0.2;
x_g=zeros(100,100);
for j=1:100
    for k=1:100
        dist=sqrt((x0(j)-x0(k))^2+(y0(j)-y0(k))^2);
        %x_g(j,k)=exp(-(dist)/2*sigma^2);%gauss
        x_g(j,k)=((dist)^2+1)^0.5;
    end
end
w=x_g\z0';

x_hat=-2:0.1:2;
y_hat=-2:0.1:2;

z_g=zeros(length(x_hat),length(x_hat));

for p=1:length(x_hat)
    for n=1:length(x_hat)
        for m=1:100
            dist2=sqrt((x0(m)-x_hat(p))^2+(y0(m)-y_hat(n))^2);
            %f=w(m)*exp(-((x0(m)-x_hat(p))^2+(y0(m)-y_hat(n))^2)/2*sigma^2);%gauss
            f=w(m)*((dist2)^2+1)^0.5;
            z_g(p,n)=z_g(p,n)+f;
        end
        z_real=x_hat(p)*exp(-x_hat(p)^2+y_hat(n));
    end
end

mesh(x_hat,y_hat,z_g');
%set(gcf, 'Renderer', 'ZBuffer');
axis([-2 2 -2 2 -2 2]);
E_max=max(max(abs(z_g-z_real)));
E_avg=mean(mean(abs(z_g-z_real)));

结果:

网太慢传不上图片了,自己运行吧。

你可能感兴趣的:(笔记,神经网络,matlab,算法)