绘制扩散方程时的量纲问题
Dimension problem when plotting diffusion equation
我重新post回答了这个问题,因为我在第一个 post 中犯了一个错误。
我正在研究扩散方程。但是,当我尝试绘制它时,我收到一条与维度相等性相关的错误消息
x and y must have same first dimension, but have shapes (401,) and (2001, 401)
x 轴应给出粒子的位置,而 y 轴应给出给定位置的粒子数。我不确定在这种情况下如何使尺寸相等。
我的代码:
import numpy as np
import matplotlib.pyplot as plt
D = 0.5
M = 2000
L = 200
tmax = 2000
dx = 1
dt = 1
s = D*dt/dx**2 #diffusion number
nx = int(2*L/dx)
nt = int(tmax/dt)
nx = nx + 1
nt = nt + 1
x = np.linspace(-L,L,nx)
t = np.linspace(0,tmax,not)
def InitCond(x, M):
if x == -50:
c0 = M
elif x == 50:
c0 = M
else:
c0 = 0
return c0
def LeftEnd(t):
return 0
def RightEnd(t):
return 0
def DiffusionSolver(x,t,s,L,c0,left,right):
nt = np.size(t)
nx = np.size(x)
u = np.zeros((nt,nx))
# Use the initial condition to define values of 'u' at the
#zeroeth timestep:
for j in range(nx):
u[0,j] = c0(x[j], M)
# Use the boundary condition to fix the string on its ends:
u[:,-L] = left(t)
u[:,L] = right(t)
# Entering the double loop to calculate the solution at every space
#point at every timestep:
for k in range(0,nt-1):
for j in range(1,nx-1):
u[k+1,j] = s*(u[k,j+1] + u[k,j-1]) + (1 - 2*s)*u[k,j]
return u
u_num = DiffusionSolver(x,t,s,L,InitCond,LeftEnd,RightEnd)
plt.plot(x, u_num)
您只需转换 u_num 数组,使其成为正确的形状。像这样:
import numpy as np
import matplotlib.pyplot as plt
D = 0.5
M = 2000
L = 200
tmax = 2000
dx = 1
dt = 1
s = D*dt/dx**2 #diffusion number
nx = int(2*L/dx)
nt = int(tmax/dt)
nx = nx + 1
nt = nt + 1
x = np.linspace(-L,L,nx)
t = np.linspace(0,tmax,nt)
def InitCond(x, M):
if x == -50:
c0 = M
elif x == 50:
c0 = M
else:
c0 = 0
return c0
def LeftEnd(t):
return 0
def RightEnd(t):
return 0
def DiffusionSolver(x,t,s,L,c0,left,right):
nt = np.size(t)
nx = np.size(x)
u = np.zeros((nt,nx))
# Use the initial condition to define values of 'u' at the
#zeroeth timestep:
for j in range(nx):
u[0,j] = c0(x[j], M)
# Use the boundary condition to fix the string on its ends:
u[:,-L] = left(t)
u[:,L] = right(t)
# Entering the double loop to calculate the solution at every space
#point at every timestep:
for k in range(0,nt-1):
for j in range(1,nx-1):
u[k+1,j] = s*(u[k,j+1] + u[k,j-1]) + (1 - 2*s)*u[k,j]
return u
u_num = DiffusionSolver(x,t,s,L,InitCond,LeftEnd,RightEnd)
u_num = u_num.T #HERE!
plt.plot(x, u_num)
plt.show()
这对我有用。
我重新post回答了这个问题,因为我在第一个 post 中犯了一个错误。 我正在研究扩散方程。但是,当我尝试绘制它时,我收到一条与维度相等性相关的错误消息
x and y must have same first dimension, but have shapes (401,) and (2001, 401)
x 轴应给出粒子的位置,而 y 轴应给出给定位置的粒子数。我不确定在这种情况下如何使尺寸相等。
我的代码:
import numpy as np
import matplotlib.pyplot as plt
D = 0.5
M = 2000
L = 200
tmax = 2000
dx = 1
dt = 1
s = D*dt/dx**2 #diffusion number
nx = int(2*L/dx)
nt = int(tmax/dt)
nx = nx + 1
nt = nt + 1
x = np.linspace(-L,L,nx)
t = np.linspace(0,tmax,not)
def InitCond(x, M):
if x == -50:
c0 = M
elif x == 50:
c0 = M
else:
c0 = 0
return c0
def LeftEnd(t):
return 0
def RightEnd(t):
return 0
def DiffusionSolver(x,t,s,L,c0,left,right):
nt = np.size(t)
nx = np.size(x)
u = np.zeros((nt,nx))
# Use the initial condition to define values of 'u' at the
#zeroeth timestep:
for j in range(nx):
u[0,j] = c0(x[j], M)
# Use the boundary condition to fix the string on its ends:
u[:,-L] = left(t)
u[:,L] = right(t)
# Entering the double loop to calculate the solution at every space
#point at every timestep:
for k in range(0,nt-1):
for j in range(1,nx-1):
u[k+1,j] = s*(u[k,j+1] + u[k,j-1]) + (1 - 2*s)*u[k,j]
return u
u_num = DiffusionSolver(x,t,s,L,InitCond,LeftEnd,RightEnd)
plt.plot(x, u_num)
您只需转换 u_num 数组,使其成为正确的形状。像这样:
import numpy as np
import matplotlib.pyplot as plt
D = 0.5
M = 2000
L = 200
tmax = 2000
dx = 1
dt = 1
s = D*dt/dx**2 #diffusion number
nx = int(2*L/dx)
nt = int(tmax/dt)
nx = nx + 1
nt = nt + 1
x = np.linspace(-L,L,nx)
t = np.linspace(0,tmax,nt)
def InitCond(x, M):
if x == -50:
c0 = M
elif x == 50:
c0 = M
else:
c0 = 0
return c0
def LeftEnd(t):
return 0
def RightEnd(t):
return 0
def DiffusionSolver(x,t,s,L,c0,left,right):
nt = np.size(t)
nx = np.size(x)
u = np.zeros((nt,nx))
# Use the initial condition to define values of 'u' at the
#zeroeth timestep:
for j in range(nx):
u[0,j] = c0(x[j], M)
# Use the boundary condition to fix the string on its ends:
u[:,-L] = left(t)
u[:,L] = right(t)
# Entering the double loop to calculate the solution at every space
#point at every timestep:
for k in range(0,nt-1):
for j in range(1,nx-1):
u[k+1,j] = s*(u[k,j+1] + u[k,j-1]) + (1 - 2*s)*u[k,j]
return u
u_num = DiffusionSolver(x,t,s,L,InitCond,LeftEnd,RightEnd)
u_num = u_num.T #HERE!
plt.plot(x, u_num)
plt.show()
这对我有用。