簡體   English   中英

隱式方案:錯誤類型:ValueError:x 和 y 必須具有相同的第一維

[英]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)因為xT具有相同的形狀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM