【发布时间】:2017-01-19 14:36:05
【问题描述】:
我正在尝试对 Jacobi 迭代方法生成的 3D 表面进行动画处理,每次迭代后都会生成一个矩阵 UF 并将其存储在列表中 UFK 我能够自己绘制每次迭代,但我想要创建一个动画,显示从凹面到平面平滑的演变和收敛。谢谢。
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.animation as animation
%matplotlib notebook
Nx = 15
Ny = 15
tol = 1e-3
err = 1
k = 0
Uy0 = 200*np.ones((1,Nx)) # Boundry condition at y=0 # lower boundry
UNy = 200*np.ones((1,Nx)) # Boundry condition at y=Ny # Upper boundary
Ux0 = 200*np.ones(Ny) # Boundry condition at x=0 # left boundry
UNx = 200*np.ones(Ny) # Boundry condition at x=Nx # Right boundary
# initial the whole matrix: the value at the interior nodes
U = np.zeros((Ny,Nx))
#Adding boundry conditions to the matrix
U[0] = UNy
U[Ny-1] = Uy0
U[:,Nx-1] = UNx
U[:,0]= Ux0
# Iterate Jacobi method
UFK=[]
UFK.append(U.copy())
NFK=[]
UF=U.copy()
while True:
k=k+1
for i in range (1,Nx-1):
for j in range (1,Ny-1):
UF[j,i] = (UF[j+1,i]+UF[j,i+1]+UF[j-1,i]+UF[j,i-1])*0.25 #the matrix i want to plot after each iteration
UFK.append(UF.copy())
H = UFK[-1]-UFK[-2]
N = np.linalg.norm(H)
NFK.append(N)
if N <= tol:
break
def data(t,UFK,surf):
for t in range(0,k-1):
L = UFK[t]
ax.clear()
surf = ax.plot_surface(XX, YY, L, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
return surf
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, Nx)
Y = np.arange(0, Ny)
XX,YY = np.meshgrid(X, Y)
surf = ax.plot_surface(XX, YY, UFK[0],rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_zlim(0, 200)
ax.zaxis.set_major_locator(LinearLocator(10))
fig.colorbar(surf, shrink=0.5, aspect=10)
ax.set_xlabel('X nodes - Axis')
ax.set_ylabel('Y nodes - Axis')
ax.set_zlabel('Value')
ani = animation.FuncAnimation(fig, data, fargs=(UFK,surf), interval=10, repeat=True )
plt.show()
【问题讨论】:
-
问题缺乏清晰的问题描述,代码无法运行。为了在此处发布问题,您需要准确地陈述问题并最好提供可验证的示例代码。
-
感谢您的回复。我添加了代码的初始化部分,我试图为 Z 值是矩阵
UFK [i]的绘图设置动画。 while 循环中的每次迭代都会生成一个存储在列表UFK中的矩阵,它们是 Z 坐标。如果我绘制一个矩阵surf = ax.plot_surface(XX, YY, UFK[5],rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)它可以工作,但我希望所有迭代在动画图中一个接一个地显示。 -
我想做这样的事情 [nugnux.my.id/2015/11/…
标签: python numpy matplotlib