我是python的新手。我正在做一个关于炉子热模型的项目。为此,我首先需要使用以下公式获得炉内材料的密度分布:dm / dt = d(ρV)/ dt = ∑m˙in-∑m˙out + Rg。最初,我需要3个网格点来解决,这是我尝试过的代码。但是我得到的输出并不令人满意。请帮忙。
我使用numpy和matplotlib.pyplot库编写了程序。
''' creating grids'''
gp = np.linspace(0, L, n)
rho = np.ones(n) * rho0
drhodt = np.empty(n)
t = np.arange(0, t_final, dt)
result = np.zeros((int(t_final/dt), n))
''' computation part'''
for j in range (1, len(t)):
plt.clf()
for i in range (1,n-1):
rho[0] = rho1
rho[n - 1] = rho2
drhodt[i] = (V_r / V) * (rho[i-1] - rho[i] + Rg)
rho = rho + drhodt*dt
result[j] = rho
print(result)
''' plotting the values'''
plt.figure(1)
plt.plot(gp, rho)
plt.axis([0, L, 0, 3000])
plt.xlabel('distance')
plt.ylabel('density')
plt.pause(0.01)
plt.figure(2)
plt.plot(t, result)
plt.axis([0, t_final, 0, 3000])
plt.xlabel('time')
plt.ylabel('density')
plt.show()
'''
我期望代码的结果是密度与网格点以及密度与时间的2个不同的配置文件。但是在运行密度与时间的关系时,我得到了3条不同曲线的错误,这不是必需的。我只需要一条曲线来描述不同时间步长的密度。
答案 0 :(得分:1)
您的3个网格点在适当的位置吗?如果是这样,使用矩阵网格将为您提供更好的状态信息。这是一个示例,您可以使用一个矩阵来设置它,该矩阵显示出您的空间网格在逐步变化。这是第二个问题A部分,如果存在稳定性问题,则b和c部分是可以使用的不同方法。 This is a homework that I had to do for numerical methods