例1. 已知某曲面的四個角點坐標分別為( 0.1, 0.1 ), ( 0.1, 1.2 ), ( 1.1, 0.1 ), ( 1.1, 1.2 ),利用雙線性插值完成該曲面插值,繪制插值插值之後的曲面片。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig=plt.figure(figsize=(10,8))
ax=plt.gca(projection='3d')
x=np.array([0.1,0.1,1.1,1.1,0.1])
y=np.array([0.1,1.2,0.1,1.2,0.1])
z=np.array([1,0.3,1.9,0.6,1])
figure=ax.plot(x[0:2],y[0:2],z[0:2],c='r')
figure=ax.plot(x[1:3],y[1:3],z[1:3],c='y')
figure=ax.plot(x[2:4],y[2:4],z[2:4],c='b')
figure=ax.plot(x[3:5],y[3:5],z[3:5],c='g')
plt.show()
運行結果如下:
2)繪制利用曲面片內部插值點生成的網格曲面片
x_=np.arange(0,1.3,0.1)
y_=np.arange(0,1.3,0.1)
X,Y=np.meshgrid(x_,y_)
x1=x[0]
x2=x[2]
y1=y[0]
y2=y[1]
z1=z[0]
z2=z[2]
z3=z[1]
z4=z[3]
x2_1=x2-x1
y2_1=y2-y1
Y_1=Y-y1
X_1=X-x1
Z=(Y_1*X_1*(z1-z2-z3+z4))/(x2_1*y2_1)+(Y_1*(z3-z1))/y2_1+(X_1*(z2-z1))/x2_1+z1
fig=plt.figure(figsize=(10,8))
ax=fig.gca(projection='3d')
ax.plot_surface(X,Y,Z)
ax.view_init(15,-25)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_xlim(-3,3)
ax.set_ylim(-1,1.5)
plt.show()
運行結果:
3)增加外部插值點之後得到的插值曲面
x_=np.arange(-0.6,1.6,0.1)
y_=np.arange(-0.6,1.6,0.1)
X,Y=np.meshgrid(x_,y_)
x1=x[0]
x2=x[2]
y1=y[0]
y2=y[1]
z1=z[0]
z2=z[2]
z3=z[1]
z4=z[3]
x2_1=x2-x1
y2_1=y2-y1
Y_1=Y-y1
X_1=X-x1
Z=(Y_1*X_1*(z1-z2-z3+z4))/(x2_1*y2_1)+(Y_1*(z3-z1))/y2_1+(X_1*(z2-z1))/x2_1+z1
fig=plt.figure(figsize=(10,8))
ax=fig.gca(projection='3d')
ax.plot_surface(X,Y,Z)
ax.view_init(15,-25)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_xlim(-3,3)
ax.set_ylim(-1,1.5)
plt.show()
plt.show()
運行結果:
大家七夕節快樂呀!!呱呱呱