要求把代码补充完整,我只填了第一部分的空。。求大/神解答!
"""--------------------------------------------------------
TP3A : Interpolation in Lagrange basis of data on a 2D grid
Remark :
as we will use the two numpy functions
np.meshgrid and np.outer,
we will transpose the outputs of np.meshgrid so as to
get the following numbering :
(0,0) (0,1) (0,2) ...
(1,0) (1,1) (1,2) ...
(2,0) (2,1) (2,2) ...
... ... ...
--------------------------------------------------------"""
import numpy as np
import matplotlib.pyplot as plt
import time
"""--------------------
functions
--------------------"""
# initial surface for the tests
def f(x,y) :
return np.exp(-(x+1)**2 - (y+1)**2) + np.exp(-(x-1)**2 - (y-1)**2)
def LagrangePoly(k,xi,t):
""" Determination of the k-th Lagrange polynomial L_k(x)
associated with points xi,
for a vector t of numerical values
Input:
k = integer designing the Lagrange polynomial to be evaluated
xi = vector of points defining the Lagrange polynomial
t = vector of real values (sampling of an interval [a,b])
Output:
y = L_k(t)
"""
n = np.size(xi) # degree + 1
y = np.ones(np.size(t))
for j in range(n):
------ #if j != k:
------ #y = y * (t - xi[j])/ (xi[k] - xi[j])
return y
"""--------------------------------------
MAIN PROGRAM
--------------------------------------"""
plt.close()
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
ax.set_title('2D Lagrange interpolation')
ax.view_init(35, -40)
# plot of the initial test surface :
x = np.linspace(-2,2,200)
y = np.linspace(-2,2,200)
X,Y = np.meshgrid(x,y)
Z = f(X,Y)
#ax.plot_wireframe(X, Y, Z, color='c')
ax.plot_surface(X, Y, Z,
rstride=5, cstride=5,
linewidth=0.5,
alpha=0.6)
# sampling of the test surface => points to be interpolated
n = 5 # number of points = degree + 1
m = 7 # number of points = degree + 1
xi = np.linspace(-2,2,n)
yj = np.linspace(-2,2,m)
------
------
------
Z0 = Z0.T
start = time.time() # optional (to evaluate duration of computations)
# Lagrange interpolation of data X0(i,j), Y0(i,j), Z0(i,j)
# by a 2D explicit polynomial surface in Lagrange basis
u = np.linspace(-2,2,100)
v = np.linspace(-2,2,200)
U,V = np.meshgrid(u,v)
U = U.T
V = V.T
# Lagrange interpolant S = S(u,v)
S = np.zeros((100,200))
for i in range(n):
for j in range(m):
------
------
------
#ax.plot_surface(U ,V , S)
ax.plot_wireframe(U, V, S, color='r', linewidth=0.5)
end = time.time()
print("Duration =" , end - start)