【发布时间】:2018-11-28 14:10:52
【问题描述】:
我已经绘制了扩散方程 du/dt=D(d^2 u/dx^2) + Cu 的数值解的代码,其中 u 是 x 和 t 的函数 - 我已经解决了数值并用 direchtlet 边界条件 u(-L/2,t)=u(L/2,t)=0 绘制它,临界长度是函数以指数方式爆炸之前的值,我已经计算出成为圆周率。我正在尝试将正确的边界条件更改为 Neumann 边界条件 u_x(L/2)=0,这应该会减少临界长度并导致函数呈指数增长,但我不太确定该怎么做而且我的似乎不太正确-有人可以看看他们是否可以确定我哪里出错了吗?谢谢!
L=np.pi # value chosen for the critical length
s=101 # number of steps in x
t=10002 # number of timesteps
ds=L/(s-1) # step in x
dt=0.0001 # time step
D=1 # diffusion constant, set equal to 1
C=1 # creation rate of neutrons, set equal to 1
Alpha=(D*dt)/(ds*ds) # constant for diffusion term
Beta=C*dt # constant for u term
x = np.linspace(-L/2, 0, num=51)
x = np.concatenate([x, np.linspace(x[-1] - x[-2], L/2, num=50)]) # setting x in the specified interval
u=np.zeros(shape=(s,t))
u[50,0]=1/ds # delta function
for k in range(0,t-1):
u[0,k]=0 #direchtlet boundary condition
for i in range(1,s-1):
u[i,k+1]=(1+Beta-2*Alpha)*u[i,k]+Alpha*u[i+1,k]+Alpha*u[i-1,k] # numerical solution
u[s-1,k+1]=u[s-2,k+1] # neumann boundary condition
if k == 50 or k == 100 or k == 250 or k == 500 or k == 1000 or k == 10000: # plotting at times
plt.plot(x,u[:,k])
plt.show()
【问题讨论】:
-
您是指绘图中最后一个
k的曲线,即k=10000,其中右侧的曲线不为零?请解释清楚 -
根据您的问题,您只需使用
u[s-1,k+1] = 0而不是u[s-1,k+1]=u[s-2,k+1] -
对不起,基本上我认为曲线应该随着时间的增加而呈指数增长,但它似乎根本没有这样做。
-
我之前尝试过使用'u[s-1,k+1] = 0',但如果没有边界条件,它根本不会改变图形。
-
我使用了
u[s-1,k+1] = 0并得到了最后 k 个不同的曲线,其中曲线的右边缘在 0 处
标签: python numpy matplotlib math numerical-methods