将一根长为一米,温度为0摄氏度的金属棒两段用温度恒定为100摄氏度的夹子夹住,求金属棒上温度分布随时间变化。


1、将金属棒划分成N小段,例如N = 100,用一个长度为100的列表Tcurent来保存当前时刻t(最开始时,当前时刻为t =0)每一段的温度,用一个同样长度的列表Tnext来保存下一个时刻t+△t每一段的温度。金属棒的比热容c为1,传热系数k为0.1,密度rho为1,时间步△r为0.0003,时间步数量为10000
N = 100
c = 1
k = 0.1
rho = 1
dt = 0.0003
num_steps = 10000
Tcurrent = [0] * N
Tnext = [0] * N
Tclamp = 100
2、对于最左边和最右边一段金属棒,计算下一个时刻的温度时需要特殊处理,写出用于计算最左边和最右边一段金属棒下一个时刻温度的代码
Tnext[0] = Tcurrent[0] + dt * k / (c * rho) * (Tclamp - Tcurrent[0])
Tnext[N-1] = Tcurrent[N-1] + dt * k / (c * rho) * (Tclamp - Tcurrent[N-1])
3、写出计算中间的第i段金属棒下一个时刻温度的代码
Tnext[i] = Tcurrent[i] + dt * k / (c * rho) * (Tcurrent[i-1] - 2 * Tcurrent[i] + Tcurrent[i+1])
4、在0时刻,用for循环遍历每一个小段,计算每一个小段下一时刻的温度,注意两端的小段需要特殊处理
for i in range(N):
if i == 0:
Tnext[i] = Tcurrent[i] + dt * k / (c * rho) * (Tclamp - Tcurrent[i])
elif i == N-1:
Tnext[i] = Tcurrent[i] + dt * k / (c * rho) * (Tclamp - Tcurrent[i])
else:
Tnext[i] = Tcurrent[i] + dt * k / (c * rho) * (Tcurrent[i-1] - 2 * Tcurrent[i] + Tcurrent[i+1])
5、从0时刻开始,计算金属棒温度分布随时间的变化(将上一步的for循环嵌套进另一个for循环)
for t in range(num_steps):
for i in range(N):
if i == 0:
Tnext[i] = Tcurrent[i] + dt * k / (c * rho) * (Tclamp - Tcurrent[i])
elif i == N-1:
Tnext[i] = Tcurrent[i] + dt * k / (c * rho) * (Tclamp - Tcurrent[i])
else:
Tnext[i] = Tcurrent[i] + dt * k / (c * rho) * (Tcurrent[i-1] - 2 * Tcurrent[i] + Tcurrent[i+1])
Tcurrent = Tnext[:]