简体   繁体   English

隐式方案:错误类型:ValueError:x 和 y 必须具有相同的第一维

[英]Implicit Scheme: error type : ValueError : x and y must have same first dimension

I am trying to implement a heat equation solver using the Euler scheme for time integration, here is the particular equation implemented:我正在尝试使用欧拉方案来实现热方程求解器以进行时间积分,这是实现的特定方程:

在此处输入图像描述 And here is my code:这是我的代码:

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

It works when the two values of Nt and Nx are the same but when I have to modify the value of Nt for the exercise that've to do it générâtes this error:当 Nt 和 Nx 的两个值相同但我必须修改 Nt 的值以进行必须执行的练习时,它会产生此错误:

ValueError: x and y must have same first dimension, but have shapes (101,) and (21,)

I Don't know how to deal with it: I understand the meaning but I Don't know how to avoid it?我不知道如何处理:我明白意思但我不知道如何避免?

Thaks a lot for your help,非常感谢您的帮助,

Best regards,此致,

When I wanted to reproduce I hit index error.当我想重现时,我遇到了索引错误。 In np.exp(-t[i]) should be np.exp(-t[n]) .np.exp(-t[i])应该是np.exp(-t[n]) Then the whole line will be:那么整行将是:

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

You are trying to plot, 21 number (shape of t ) in relation to 101 numbers (shape of T ).您正在尝试 plot, 21 个数字( t的形状)相对于 101 个数字( T的形状)。 To solve, change to plt.plot(x, T) as x and T has the same shape.要解决,请更改为plt.plot(x, T)因为xT具有相同的形状。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输入错误,然后输入值错误:x 和 y 必须具有相同的第一维 - Type error, and then ValueError: x and y must have same first dimension Matplotlib:ValueError:x和y必须具有相同的第一维错误 - Matplotlib: ValueError: x and y must have same first dimension Error ValueError: x 和 y 必须具有相同的第一维异常被抛出,但 x 和 y 的类型和长度相同 - ValueError: x and y must have same first dimension exception is thrown, but x and y are of the same type and length ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes “ ValueError:x和y必须具有相同的第一维”的不同情况 - A different situation of “ValueError: x and y must have same first dimension” ValueError:绘制时x和y的第一维必须相同 - ValueError: x and y must have same first dimension when plotting ValueError:x和y必须具有相同的第一个维度 - ValueError: x and y must have same first dimension Matplotlib:ValueError:x 和 y 必须具有相同的第一维 - Matplotlib: ValueError: x and y must have same first dimension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM