![](/img/trans.png)
[英]Type error, and then ValueError: x and y must have same first dimension
[英]Implicit Scheme: error type : ValueError : x and y must have same first dimension
我正在嘗試使用歐拉方案來實現熱方程求解器以進行時間積分,這是實現的特定方程:
Nt = 20
Nx = 100
x = np.linspace(0,1,Nx+1)
t = np.linspace(0,1,Nt+1)
dx = 1/Nx
dt = 1/Nt
F = dt/(dx**2)
T_temps = np.zeros(Nx+1)
T = np.zeros(Nx+1)
for i in range(Nx+1):
T_temps[i] = np.sin(x[i])
for n in range(0,Nt):
for i in range(1,Nx):
T[i] = T_temps[i] + F*(T_temps[i-1]-2*T_temps[i]+T_temps[i+1]) + ((np.pi**2)-1)*np.exp(-t[i])*np.sin(x[i])
T[0] = 0.
T[Nx] = 0.
T_temps[:] = T
plt.plot(t,T)
plt.show()
當 Nt 和 Nx 的兩個值相同但我必須修改 Nt 的值以進行必須執行的練習時,它會產生此錯誤:
ValueError: x and y must have same first dimension, but have shapes (101,) and (21,)
我不知道如何處理:我明白意思但我不知道如何避免?
非常感謝您的幫助,
此致,
當我想重現時,我遇到了索引錯誤。 在np.exp(-t[i])
應該是np.exp(-t[n])
。 那么整行將是:
T[i] = T_temps[i] + F * (T_temps[i - 1] - 2 * T_temps[i] + T_temps[i + 1]) + ((np.pi ** 2) - 1) * np.exp(-t[n]) * np.sin(x[i])
您正在嘗試 plot, 21 個數字( t
的形狀)相對於 101 個數字( T
的形狀)。 要解決,請更改為plt.plot(x, T)
因為x
和T
具有相同的形狀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.