【问题标题】:Implicit Scheme: error type : ValueError : x and y must have same first dimension隐式方案:错误类型:ValueError:x 和 y 必须具有相同的第一维
【发布时间】:2020-10-18 16:37:56
【问题描述】:

我正在尝试使用欧拉方案来实现热方程求解器进行时间积分,这是实现的特定方程:

这是我的代码:

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,)

我不知道如何处理它:我理解它的含义但我不知道如何避免它?

非常感谢您的帮助,

最好的问候,

【问题讨论】:

    标签: python matplotlib valueerror


    【解决方案1】:

    当我想重现时,我遇到了索引错误。在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])
    

    您正在尝试绘制 21 个数字(t 的形状)与 101 个数字(T 的形状)的关系。要解决,请更改为plt.plot(x, T),因为xT 具有相同的形状。

    【讨论】:

    • 感谢您的回答,我已经更正了您发现错误的行,但对于您的最终建议,我不确定这是我所要求的,因为目标是绘制温度演变“T”作为时间的函数“t”首先不是“x”,它是一个空间参数……我不知道我的观点是否正确?再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2022-01-09
    相关资源
    最近更新 更多