我正在尝试使用有限差分法在 python 中进行迭代计算。我从中找到了有限差分法:
http://depa.fquim.unam.mx/amyd/archivero/DiferenciasFinitas3_25332.pdf
代码计算的值是正确的。问题是它只显示最终值。我想要的是提取 x 方向上任何点的值,以便我可以绘制它们,以及提取任何时间点的值,例如计算中途点的值。这是进行迭代计算的正确方法吗?代码如下所示:
import numpy as np
import scipy as sp
import time
import matplotlib as p
L=0.005
Nx=3
T=5
N1=5
k=0.5
rho=1200
c=1000
a=(k/(rho*c))
x = np.linspace(0, L, Nx+1) # mesh points in space
dx = x[1] - x[0]
t = np.linspace(0, T, N1) # time
dt = t[1] - t[0]
toutside=5
Coefficient = a*dt/dx**2
bi=0.5
ui = sp.zeros(Nx+1)
u = sp.zeros(Nx+1)
for i in range(Nx+1):
ui[i] = 50 # initial values
for n in range(0, N1):
for i in range(0,1):
u[i] = 2*Coefficient*(ui[i+1]+bi*toutside)+(1-2*Coefficient-2*bi*Coefficient)*ui[i]
for i in range(1,Nx):
u[i] = Coefficient*(ui[i+1]+ui[i-1])+(1-2*Coefficient)*ui[i]
for i in range(Nx,Nx+1):
u[i] = 2*Coefficient*(ui[i-1])+(1-2*Coefficient)*ui[i]
ui[:]= u #updates matrix for next loop
print ui
我已经根据 danodonovan 对以下问题的回答修改了我的代码:
for n in range(0, N1):
for i in range(0,1):
u[i] = 2*Coefficient*(ui[i+1]+bi*toutside)+(1-2*Coefficient-2*bi*Coefficient)*ui[i]
for i in range(1,Nx):
u[i] = Coefficient*(ui[i+1]+ui[i-1])+(1-2*Coefficient)*ui[i]
for i in range(Nx,Nx+1):
u[i] = 2*Coefficient*(ui[i-1])+(1-2*Coefficient)*ui[i]
ui=u
a=list(ui)
print a
当我尝试将整个列表从循环中取出时,只会产生最终值。如何提取整个列表?这是使用前一行的值来计算新行的值进行迭代计算的正确方法吗?