I am trying to implement a heat equation solver using the Euler scheme for time integration, here is the particular equation implemented :
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 :
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])
. 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
). To solve, change to plt.plot(x, T)
as x
and T
has the same shape.