径向基函数插值(2)一维数据的插值
生活随笔
收集整理的這篇文章主要介紹了
径向基函数插值(2)一维数据的插值
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
假設(shè)我們有N組數(shù)據(jù)(xi,yi),,,,,,,,這時我們根據(jù)徑向基函數(shù)我們的目的主要是找到徑向基函數(shù)中的位置參數(shù)的值,
我們要用這些已知數(shù)據(jù)的值用最小二乘法得到這些參數(shù)。。
現(xiàn)在我們用一般的方法matlb自帶的插值函數(shù)進行新值得計算:
<span style="font-size:18px;">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');</span>
現(xiàn)在我們用徑向基函數(shù)的方法進行插值
1、
<span style="font-size:18px;">x = 0:1.25:10; y = sin(x); xi = 0:.1:10; </span>產(chǎn)生數(shù)據(jù)集,和需要插值的數(shù)據(jù)
2、設(shè)定調(diào)用函數(shù)的參數(shù)
% 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'); 求解徑向基函數(shù)的系數(shù)過程 function options = rbfcreate(x, y, varargin) %RBFCREATE Creates an RBF interpolation % OPTIONS = RBFSET(X, Y, 'NAME1',VALUE1,'NAME2',VALUE2,...) creates an % radial base function interpolation % % RBFCREATE with no input arguments displays all property names and their % possible values. % %RBFCREATE PROPERTIES % % % Alex Chirokov, alex.chirokov@gmail.com % 16 Feb 2006 tic; % Print out possible values of properties. if (nargin == 0) & (nargout == 0)fprintf(' x: [ dim by n matrix of coordinates for the nodes ]\n');fprintf(' y: [ 1 by n vector of values at nodes ]\n');fprintf(' RBFFunction: [ gaussian | thinplate | cubic | multiquadrics | {linear} ]\n');fprintf(' RBFConstant: [ positive scalar ]\n');fprintf(' RBFSmooth: [ positive scalar {0} ]\n');fprintf(' Stats: [ on | {off} ]\n');fprintf('\n');return; end Names = ['RBFFunction ''RBFConstant ''RBFSmooth ''Stats ' ]; [m,n] = size(Names); names = lower(Names);options = []; for j = 1:moptions.(deblank(Names(j,:))) = []; end%************************************************************************** %Check input arrays %************************************************************************** [nXDim nXCount]=size(x); [nYDim nYCount]=size(y);if (nXCount~=nYCount)error(sprintf('x and y should have the same number of rows')); end;if (nYDim~=1)error(sprintf('y should be n by 1 vector')); end;options.('x') = x; options.('y') = y; %************************************************************************** %Default values %************************************************************************** options.('RBFFunction') = 'linear'; options.('RBFConstant') = (prod(max(x')-min(x'))/nXCount)^(1/nXDim); %approx. average distance between the nodes options.('RBFSmooth') = 0; options.('Stats') = 'off';%************************************************************************** % Argument parsing code: similar to ODESET.m %**************************************************************************i = 1; % A finite state machine to parse name-value pairs. if rem(nargin-2,2) ~= 0error('Arguments must occur in name-value pairs.'); end expectval = 0; % start expecting a name, not a value while i <= nargin-2arg = varargin{i};if ~expectvalif ~isstr(arg)error(sprintf('Expected argument %d to be a string property name.', i));endlowArg = lower(arg);j = strmatch(lowArg,names);if isempty(j) % if no matcheserror(sprintf('Unrecognized property name ''%s''.', arg));elseif length(j) > 1 % if more than one match% Check for any exact matches (in case any names are subsets of others)k = strmatch(lowArg,names,'exact');if length(k) == 1j = k;elsemsg = sprintf('Ambiguous property name ''%s'' ', arg);msg = [msg '(' deblank(Names(j(1),:))];for k = j(2:length(j))'msg = [msg ', ' deblank(Names(k,:))];endmsg = sprintf('%s).', msg);error(msg);endendexpectval = 1; % we expect a value nextelseoptions.(deblank(Names(j,:))) = arg;expectval = 0; endi = i + 1; endif expectvalerror(sprintf('Expected value for property ''%s''.', arg)); end%************************************************************************** % Creating RBF Interpolatin %**************************************************************************%選擇所用基函數(shù)的形式 這五種函數(shù) 高斯 線性 立方 薄板 多項式 switch lower(options.('RBFFunction')) case 'linear' options.('rbfphi') = @rbfphi_linear; %線性case 'cubic'options.('rbfphi') = @rbfphi_cubic;%立方case 'multiquadric'options.('rbfphi') = @rbfphi_multiquadrics;%多項式case 'thinplate'options.('rbfphi') = @rbfphi_thinplate;%薄板case 'gaussian'options.('rbfphi') = @rbfphi_gaussian;%高斯型otherwiseoptions.('rbfphi') = @rbfphi_linear; endphi = options.('rbfphi'); % 調(diào)用函數(shù),求解出數(shù)據(jù)集使用調(diào)用||x-xi||函數(shù)的值 A=rbfAssemble(x, phi, options.('RBFConstant'), options.('RBFSmooth')); b=[y'; zeros(nXDim+1, 1)]; %這是數(shù)據(jù)集對應(yīng)的輸出值Y %inverse rbfcoeff=A\b; %這是徑向基函數(shù)對應(yīng)的參數(shù)系數(shù)%SVD % [U,S,V] = svd(A); % % for i=1:1:nXCount+1 % if (S(i,i)>0) S(i,i)=1/S(i,i); end; % end; % rbfcoeff = V*S'*U*b;options.('rbfcoeff') = rbfcoeff;if (strcmp(options.('Stats'),'on'))fprintf('%d point RBF interpolation was created in %e sec\n', length(y), toc); fprintf('\n'); end;function [A]=rbfAssemble(x, phi, const, smooth) %x 為已知的數(shù)據(jù)集 %phi 表示調(diào)用的是哪個基函數(shù)形式 %const 表示高斯形式時的方差 %smooth 偏移量 一般為0 [dim n]=size(x); %得到數(shù)據(jù)集的維數(shù) A=zeros(n,n); %初始化有數(shù)據(jù)集帶入基函數(shù)中得到的值 for i=1:nfor j=1:ir=norm(x(:,i)-x(:,j)); %每個數(shù)據(jù)跟其他數(shù)據(jù)之間的距離temp=feval(phi,r, const); %帶入相應(yīng)的基函數(shù)中得到的值,存儲到A中,用于求解系數(shù)A(i,j)=temp;A(j,i)=temp;endA(i,i) = A(i,i) - smooth; end % Polynomial part P=[ones(n,1) x']; A = [ A PP' zeros(dim+1,dim+1)]; %************************************************************************** % Radial Base Functions %************************************************************************** %五種基函數(shù)的表達式 function u=rbfphi_linear(r, const) u=r;function u=rbfphi_cubic(r, const) u=r.*r.*r;function u=rbfphi_gaussian(r, const) u=exp(-0.5*r.*r/(const*const));function u=rbfphi_multiquadrics(r, const) u=sqrt(1+r.*r/(const*const));function u=rbfphi_thinplate(r, const) u=r.*r.*log(r+1);這是進行求解新插入值得過程
已知新的插入值,和上一個函數(shù)得到的徑向基函數(shù)的參數(shù),進行計算,得到新插入值的解
總結(jié)
以上是生活随笔為你收集整理的径向基函数插值(2)一维数据的插值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 径向基函数插值(1)
- 下一篇: 径向基函数插值(3)二维数据的插值