C#代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceIDWTest{publicclassIDW{publicIDW(){}///summary距离平方反比加权插值法/summary///paramname=x已知点列的X坐标/param///paramname=y已知点列的Y坐标/param///paramname=z已知点列的值/param///paramname=z插值点列的X坐标/param///paramname=z插值点列的Y坐标/param///returns插值结果/returnspublicstaticListdoubleInterpolate(Listdoublex,Listdoubley,Listdoublez,ListdoubleX,ListdoubleY){if(x.Count3||x.Count!=y.Count||x.Count!=z.Count||X.Count!=Y.Count)returnnull;intm0=x.Count;intm1=X.Count;ListdoubleZ=newListdouble();//距离列表Listdoubler=newListdouble();for(inti=0;im1;i++){for(intj=0;jm0;j++){doubletmpDis=Math.Sqrt(Math.Pow(X[i]-x[j],2)+Math.Pow(Y[i]-y[j],2));r.Add(tmpDis);}}//插值函数for(inti=0;im1;i++){//查找重复boolifFind=false;for(intj=m0*i;jm0*i+m0;j++){if(Math.Abs(r[j])0.0001){Z.Add(z[j-m0*i]);ifFind=true;break;}else{}}if(ifFind)continue;doublenumerator=0;doubledenominator=0;for(intj=m0*i;jm0*i+m0;j++){numerator+=z[j-m0*i]/(r[j]*r[j]);denominator+=1/(r[j]*r[j]);}Z.Add(numerator/denominator);}returnZ;}}}MatLab代码:%IDW(反距离加权插值法)%其中x,y,z为已知坐标及其函数值,X,Y为要插值的坐标%x,y,z,X,Y最高为二维的,不可为三维%不考虑x,y中出现重复坐标的情况function[Z]=IDW(x,y,z,X,Y)[m0,n0]=size(x);[m1,n1]=size(X);%生成距离矩阵r(m0*m1*n1,n0)fori=1:m1forj=1:n1r(m0*n1*(i-1)+m0*(j-1)+1:m0*n1*(i-1)+m0*(j),:)=sqrt((X(i,j)-x).^2+(Y(i,j)-y).^2);endend%定义插值函数fori=1:m1forj=1:n1iffind(r(m0*n1*(i-1)+m0*(j-1)+1:m0*n1*(i-1)+m0*(j),:)==0)[m2,n2]=find(r(m0*n1*(i-1)+m0*(j-1)+1:m0*n1*(i-1)+m0*(j),:)==0);Z(i,j)=z(m2,n2);elsenumerator=sum(sum(z./r(m0*n1*(i-1)+m0*(j-1)+1:m0*n1*(i-1)+m0*(j),:)));denominator=sum(sum(1./r(m0*n1*(i-1)+m0*(j-1)+1:m0*n1*(i-1)+m0*(j),:)));Z(i,j)=numerator/denominator;endendend