如何在Python中制作热方程式的3D模型?
How to make 3D model of heat equation in Python?
Given:
和
We have formula:
我做了 3D 模型,但是我不能给出像 when x = 0 u(0,t) = 0
这样的条件
import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def u(x,t,n):
for i in range(1,n):
alpha=((6*(-1)**i-30)/(i**2*np.pi**2))
e=np.exp((-(np.pi**2)*(i**2)*t))
sin=np.sin((i*np.pi*x)/3)
u=alpha*e*sin
return u
N=20
L = 4 # length
att = 20 # iteration
x = np.linspace(0, L ,N) #x-array
t = np.linspace(0, L, N) #t-array
X, Y = np.meshgrid(x, t)
Z = u(X, Y, att)
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=1000)
plt.show()
My 3D model:
如果你在部分傅立叶和计算中实际计算了一个和,这会有所帮助,目前你只是 return 该和的最后一项。
def u(x,t,n):
u = 0*x
for i in range(1,n):
alpha=((6*(-1)**i-30)/(i**2*np.pi**2))
e=np.exp((-(np.pi**2)*(i**2)*t))
sin=np.sin((i*np.pi*x)/3)
u+=alpha*e*sin
return u
你确定傅立叶系数吗?其中的数字 30
对我来说有点可疑。而且频率也很奇怪,u(x,0)
的延续应该是一个周期为8的奇数矩形波。注意,是a=3
但是L=4
.
Given:
和
We have formula:
我做了 3D 模型,但是我不能给出像 when x = 0 u(0,t) = 0
这样的条件import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def u(x,t,n):
for i in range(1,n):
alpha=((6*(-1)**i-30)/(i**2*np.pi**2))
e=np.exp((-(np.pi**2)*(i**2)*t))
sin=np.sin((i*np.pi*x)/3)
u=alpha*e*sin
return u
N=20
L = 4 # length
att = 20 # iteration
x = np.linspace(0, L ,N) #x-array
t = np.linspace(0, L, N) #t-array
X, Y = np.meshgrid(x, t)
Z = u(X, Y, att)
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=1000)
plt.show()
My 3D model:
如果你在部分傅立叶和计算中实际计算了一个和,这会有所帮助,目前你只是 return 该和的最后一项。
def u(x,t,n):
u = 0*x
for i in range(1,n):
alpha=((6*(-1)**i-30)/(i**2*np.pi**2))
e=np.exp((-(np.pi**2)*(i**2)*t))
sin=np.sin((i*np.pi*x)/3)
u+=alpha*e*sin
return u
你确定傅立叶系数吗?其中的数字 30
对我来说有点可疑。而且频率也很奇怪,u(x,0)
的延续应该是一个周期为8的奇数矩形波。注意,是a=3
但是L=4
.