matlab用rbf作为基函数进行插值计算

这篇博客探讨了1D和2D插值的方法,主要使用了Matlab的interp1和griddata函数,以及径向基函数(RBF)进行插值。通过示例展示了RBF的不同函数类型,如thinplate, linear, cubic, gaussian和multiquadric,以及平滑参数对结果的影响。此外,还涉及了随机数据的1D插值,并比较了不同RBF函数的选择对结果的精度。
摘要由CSDN通过智能技术生成

%**************************************************************************
% 1D Interpolation
%**************************************************************************

x = 0:1.25:10; 
y = sin(x); 
xi = 0:.1:10; 

%Matlab
yi = interp1(x,y,xi); 
subplot(2,1,1); plot(x,y,'o',xi,yi, xi, sin(xi),'r'); title('Interpolation using Matlab function interp1');

%RBF
%op=rbfcreate(x, y,'RBFFunction', 'thinplate'); rbfcheck(op);
%op=rbfcreate(x, y,'RBFFunction', 'linear'); rbfcheck(op);
%op=rbfcreate(x, y,'RBFFunction', 'cubic'); rbfcheck(op);
%op=rbfcreate(x, y,'RBFFunction', 'gaussian'); rbfcheck(op);
op=rbfcreate(x, y,'RBFFunction', 'multiquadric', 'RBFConstant', 2); rbfcheck(op);
op=rbfcreate(x, y); rbfcheck(op);
%op=rbfcreate(x, y,'RBFFunction', 'gaussian');
%op=rbfcreate(x, y);
fi = rbfinterp(xi, op);
subplot(2,1,2); plot(x, y,'o', xi, fi,xi, sin(xi),'r'); title('RBF interpolation');

yi=rbfinterp_ez(x,y,xi);
plot(x, y,'o', xi, fi,xi, sin(xi),'r'); title('RBF interpolation');

clear all; figure;
%**************************************************************************
% 2D Interpolation
%**************************************************************************
%Matlab standard interpolation using griddata 
rand('seed',0)
x = rand(50,1)*4-2; y = rand(50,1)*4-2;
z = x.*exp(-x.^2-y.^2);

ti = -2:.05:2; 
[XI,YI] = meshgrid(ti,ti);
ZI = griddata(x,y,z,XI,YI,'cubic');

subplot(2,2,1); mesh(XI,YI,ZI), hold, axis([-2 2 -2 2 -0.5 0.5]); 
plot3(x,y,z,'.r'), hold off; title('Interpolation using Matlab function griddata(method=cubic)');

subplot(2,2,3); pcolor(abs(ZI - XI.*exp(-XI.^2-YI.^2))); colorbar; title('griddata(method=cubic) interpolation error');

%RBF interpolation
%op=rbfcreate([X(:)'; Y(:)'], Z(:)','RBFFunction', 'thinplate'); rbfcheck(op);
%op=rbfcreate([X(:)'; Y(:)'], Z(:)','RBFFunction', 'linear'); rbfcheck(op);
%op=rbfcreate([X(:)'; Y(:)'], Z(:)','RBFFunction', 'cubic'); rbfcheck(op);
%op=rbfcreate([X(:)'; Y(:)'], Z(:)','RBFFunction', 'gaussian'); rbfcheck(op);
op=rbfcreate([x'; y'], z','RBFFunction', 'multiquadric', 'RBFConstant', 2);
rbfcheck(op);
%op=rbfcreate(x, y,'RBFFunction', 'gaussian');
%op=rbfcreate(x, y);
ZI = rbfinterp([XI(:)'; YI(:)'], op);
ZI = reshape(ZI, size(XI));
subplot(2,2,2); mesh(XI,YI,ZI), hold
plot3(x,y,z,'.r'), hold off; title('RBF interpolation'); axis([-2 2 -2 2 -0.5 0.5]);
subplot(2,2,4); pcolor(abs(ZI - XI.*exp(-XI.^2-YI.^2))); colorbar; title('RBF interpolation error');

clear all;
%**************************************************************************
% 1D smooth interpolation 
%**************************************************************************

x = 0:1.25:10; 
y = sin(x)+rand(size(x))-0.5; 
xi = 0:.1:10; 

%Matlab
yi = interp1(x,y,xi); 
subplot(2,1,1); plot(x,y,'o',xi,yi, xi, sin(xi),'r'); title('Interpolation using Matlab function interp1');

%RBF
op=rbfcreate(x, y,'RBFFunction', 'multiquadric', 'RBFConstant', 2, 'RBFSmooth', 0.1); rbfcheck(op);
fi = rbfinterp(xi, op);
subplot(2,1,2); plot(x, y,'o', xi, fi,xi, sin(xi),'r'); title('RBF interpolation');

clear all;
%**************************************************************************
% 1D  interpolation of random data
%**************************************************************************
N = 30;
smooth = 1e-2;
x = rand(1, N); 
y = rand(1, N); 
xi = [x rand(1, 10*N)]; 

%RBF
%op=rbfcreate(x, y,'RBFFunction', 'multiquadric', 'RBFConstant', 2, 'RBFSmooth', 1e-6); rbfcheck(op);
op=rbfcreate(x, y,'RBFFunction', 'cubic', 'RBFSmooth', smooth); 
yi = rbfinterp(xi, op);
[xi,index] = sort(xi);
subplot(2,2,1); plot(x, y,'.r', xi, yi(index)); title('RBF cubic interpolation'); 

op=rbfcreate(x, y,'RBFFunction', 'linear', 'RBFSmooth', smooth); 
yi = rbfinterp(xi, op);
[xi,index] = sort(xi);
subplot(2,2,2); plot(x, y,'.r', xi, yi(index)); title('RBF linear interpolation');

op=rbfcreate(x, y,'RBFFunction', 'multiquadric', 'RBFSmooth', smooth); 
yi = rbfinterp(xi, op);
[xi,index] = sort(xi);
subplot(2,2,3); plot(x, y,'.r', xi, yi(index)); title('RBF multiquadric interpolation');

op=rbfcreate(x, y,'RBFFunction', 'gaussian', 'RBFSmooth', smooth); 
yi = rbfinterp(xi, op);
[xi,index] = sort(xi);
subplot(2,2,4); plot(x, y,'.r', xi, yi(index)); title('RBF gaussian interpolation');

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值