1、matlab拉格朗日插值
function yi=Lagrange(x,y,xi) %x为向量,全部的插值节点 %y为向量,插值节点处的函数值 %xi为标量或向量,被估计函数的自变量; %yi为xi处的函数估计值 n=length(x);m=length(y); %插值点与它的函数值应有相同的个数 if n~=m error('The lengths of X and Y must be equal!'); return; end yi=zeros(size(xi)); for k=1:n w=ones(size(xi)); for j=[1:k-1 k+1:n] %输入的插值节点必须互异 if abs(x(k)-x(j))<eps error('the DATA is error'); return; end w=(xi-x(j))/(x(k)-x(j)).*w; end yi=yi+w*y(k); end
2、Matlab作两个模式类的距离(判别分析),
X1,X2为----类1,类2的训练样本“样品*变量”矩阵
X-为待判样品的“样品*变量”矩阵
COEFF---判别函数的系数向量
DIST-----待判样品的判别函数值向量
CLASS----待判样品的分类
function [coeff,dist,class]=dclass(x1,x2,x) [N1,p]=size(x1); [N2,p]=size(x2); [N,p]=size(x); meanx1=mean(x1); meanx2=mean(x2); covx1=(N1-1)*cov(x1); covx2=(N2-1)*cov(x2); mean=(meanx1+meanx2)./2; cov=(covx1+covx2)./(N1+N2-2); coeff=inv(cov)*(meanx1-meanx2)'; dist=[]; class=[]; for byk=1:N w=(x(byk,:)-mean)*coeff; if w>0 r=1; else r=2; end dist=[dist,w]; class=[class,r]; end coeff=coeff';
3、好像是中心差分的差分表
function [Dy,dy,jdw,n]=diffext1(fun,x0,jdwc,max1) h=1;j=1; n=1;jdW=1;xdW=1; x1=x0+h;x2=x0-h; Dy(1,1)=(feval(fun,x1)- feval(fun,x2))/(2*h); while((jdW>jdwc)&(j<max1)) j;x1=x0+2^(-j)*h;x2=x0-2^(-j)*h; Dy(j+1,1)=(feval(fun,x1)-feval(fun,x2))/(2^(1-j)*h); for k=1:j k;Dy(j+1,k+1)= Dy(j+1,k)+( Dy(j+1,k)- Dy(j,k))/(4^k-1); end jdW=abs(Dy(j+1,j+1)-Dy(j+1,j)); j=j+1; end [n,n]=size(Dy);jdw=abs(Dy(n,n)-Dy(n,n-1)); dy= Dy(n,n);
function yi=Lagrange(x,y,xi)%x为向量,全部的插值节点%y为向量,插值节点处的函数值%xi为标量或向量,被估计函数的自变量;%yi为xi处的函数估计值n=length(x);m=length(y);%插值点与它的函数值应有相同的个数if n~=m error('The lengths of X and Y must be equal!'); return;endyi=zeros(size(xi));for k=1:n w=ones(size(xi)); for j=[1:k-1 k+1:n] %输入的插值节点必须互异 if abs(x(k)-x(j))<eps error('the DATA is error'); return; end w=(xi-x(j))/(x(k)-x(j)).*w; end yi=yi+w*y(k);end