赞
踩
‘nearest’ | 最近项插值 |
---|---|
‘linear’ | 线性插值 |
‘spline’ | 立方样条插值 |
‘cubic’ | 立方插值 |
‘complete’ | 一阶导数 |
---|---|
‘not-a-knot’ | 非扭结条件(没有边界条件) |
‘periodic’ | 周期条件 |
'second‘ | 二阶导数 |
x | 0 | 3 | 5 | 7 | 9 | 11 | 12 | 13 | 14 | 15 |
---|---|---|---|---|---|---|---|---|---|---|
y | 0 | 1.2 | 1.7 | 2.0 | 2.1 | 2.0 | 1.8 | 1.2 | 1.0 | 1.6 |
x0=[0 3 5 7 9 11 12 13 14 15]; y0=[0 1.2 1.7 2.0 2.1 2.0 1.8 1.2 1.0 1.6]; x = 0:0.1:15; y1 = interp1(x0,y0,x);%默认线性插值 y2 = interp1(x0,y0,x,'spline');%三次样条插值 pp1=csape(x0,y0);%边界条件为Lagrange条件 y3=fnval(pp1,x); subplot(1,3,1) plot(x0,y0,'+',x,y1) title('Piecewise linear') subplot(1,3,2) plot(x0,y0,'+',x,y2) title('Spline1') subplot(1,3,3) plot(x0,y0,'+',x,y3) title('Spline2')
import numpy as np from scipy import interpolate #插值 import matplotlib.pyplot as plt #Pyplot 是 Matplotlib 的子库,提供了和 MATLAB 类似的绘图 API。Pyplot 是常用的绘图模块,能很方便让用户绘制 2D 图表。 x0 = np.array([0, 3, 5, 7, 9, 11, 12, 13, 14, 15]) y0 = np.array([0, 1.2, 1.7, 2.0, 2.1, 2.0, 1.8, 1.2, 1.0, 1.6]) x1 = np.arange(0, 15, 0.1)#numpy.arange(start, stop, step, dtype) np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) plt.figure(figsize=(8, 6))#创建一个8*6点(point)的点图 ###将图作在一张图上 for method in [ "slinear", "cubic"]: # 插值方式 f = interpolate.interp1d(x0, y0, kind=method) #一维数据的插值运算可以通过方法 interp1d() 完成。kind=method指插值方法 y1 = f(x1)#得到插值结果 plt.plot(x1, y1, label=method) plt.plot(x0,y0,'o',label="datas") plt.title('Interpolation') plt.legend(loc="lower right")#图例的位置 plt.show() ###3张子图 i=1 for method in [ "slinear", "cubic"]: # 插值方式 f = interpolate.interp1d(x0, y0, kind=method) #一维数据的插值运算可以通过方法 interp1d() 完成。kind=method指插值方法 y1 = f(x1)#得到插值结果 plt.subplot(1, 3, i) plt.plot(x1, y1) plt.title(method) i=i+1 plt.subplot(1, 3, i) plt.plot(x0,y0,'o-') plt.title("Piecewise linear") plt.suptitle('Interpolation') plt.show()
clear,clc x=100:100:500; y=100:100:400; z=[636 697 624 478 450 698 712 630 478 420 680 674 598 412 400 662 626 552 334 310]; xi=100:10:500;yi=100:10:400; pp=csape({x,y},z'); cz=fnval(pp,{xi,yi}); [i,j]=find(cz==max(max(cz))); disp(["最高点的地址:" num2str([xi(i),yi(j)])]); disp(["最高点的高程:" num2str(cz(i,j))]); [X,Y]=meshgrid(xi,yi); surf(X,Y,cz')
"最高点的地址:" "170 180"
"最高点的高程:" "720.6252"
import numpy as np from scipy import interpolate #插值 import matplotlib.cm as cm #曲面的配色 import matplotlib.pyplot as plt #绘图 from mpl_toolkits.mplot3d import Axes3D #3D图 x0, y0 = np.mgrid[100:500:5j, 100:400:4j]#生成网格图5*4,5j代表5段,没有j代表间距为5 z0 = np.array([636, 697, 624, 478, 450, 698, 712, 630, 478, 420, 680, 674, 598, 412, 400, 662, 626, 552, 334, 310]).reshape((4, 5)) z0=z0.T#注意z是5*4 f = interpolate.interp2d(x0, y0, z0, kind='cubic')#由样本点生成三次样条插值 x1 = np.linspace(100, 500, 100)#np.linspace(start, stop, num=50) y1 = np.linspace(100, 400, 100) z1 = f(x1, y1)#插值结果 x1, y1 = np.meshgrid(x1, y1) fig = plt.figure() ax=Axes3D(fig)#3D对象 #ax=fig.add_subplot(111,projection='3d') #ax= plt.subplot(1, 2, 1, projection='3d') surf = ax.plot_surface(x1, y1, z1, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0.5, antialiased=True)#rstride行之间的跨度,cstride列之间的跨度cmap颜色映射表,默认是“rainbow” plt.title('Interpolation-2D(The peak: {:3f})'.format(np.max(z1))) ax.scatter(x0,y0,z0,c='r')#三维散点图 ax.contour(x1, y1, z1,zdir='z',offset=300)#投影到z平面的z=300上 ax.set_zlabel('Z') # 坐标轴 ax.set_ylabel('Y') ax.set_xlabel('X') plt.colorbar(surf, shrink=0.5, aspect=5) plt.show() #最高点处的地址 x1[np.where(z1==np.max(z1))] #array([144.44444444]) y1[np.where(z1==np.max(z1))] #array([200.])
clc, clear
clc, clear
x=[129,140,103.5,88,185.5,195,105,157.5,107.5,77,81,162,162,117.5];
y=[7.5,141.5,23,147,22.5,137.5,85.5,-6.5,-81,3,56.5,-66.5,84,-33.5];
z=-[4,8,6,8,6,8,8,9,9,8,8,9,4,9];
xmm=minmax(x); %求x的最小值和最大值
ymm=minmax(y); %求y的最小值和最大值
xi=xmm(1):xmm(2);
yi=ymm(1):ymm(2);
zi1=griddata(x,y,z,xi,yi','cubic'); %立方插值
zi2=griddata(x,y,z,xi,yi','nearest'); %最近点插值
zi=zi1; %立方插值和最近点插值的混合插值的初始值
zi(isnan(zi1))=zi2(isnan(zi1)); %把立方插值中的不确定值换成最近点插值的结果
subplot(1,2,1), plot(x,y,'*');
subplot(1,2,2), mesh(xi,yi,zi);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。