python 描述扩散方程的代码

rbl8hiat  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(134)

我有一些描述扩散方程的代码,但它给出了以下错误:
IndexError:只有整数、切片(:)、省略号(...)、numpy.newaxis(None)以及整数或布尔数组是有效的索引
扩散方程的代码是:

import numpy as np
import matplotlib.pyplot as plt

dt = 0.0005 # grid size for time (s)
dy = 0.0005 # grid size for space (m)
viscosity = 2*10**(-4) # kinematic viscosity of oil (m2/s)
y_max = 0.04 # in m
t_max = 1 # total time in s
V0 = 10 # velocity in m/s

def diffusion(dt,dy,t_max,y_max,viscosity,V0):
    s = viscosity*dt/dy**2 
    y = np.arange(0,y_max+dy,dy) 
    t = np.arange(0,t_max+dt,dt)
    r = len(t)
    c = len(y)
    V = np.zeros([r,c])
    V[:,0] = V0
    for n in range(0,r-1): # time
        for j in range(1,c-1): # space
            V[n+1,j] = V[n,j] + s*(V[n,j-1] - 2*V[n,j] + V[n,j+1]) 
    return y,V,r,s

y,V,r,s = diffusion(dt,dy,t_max,y_max,viscosity,V0)

# plotting:
plt.figure(figsize=(7,5))
plot_times = np.arange(0.2,1.0,0.1)
for t in plot_times:
    plt.plot(y,V[t/dt,:],'Gray',label='numerical')
plt.xlabel('distance from wall (m)',fontsize=12)
plt.ylabel('velocity (m/s)',fontsize=12)
plt.axis([0,y_max,0,V0])
nfzehxib

nfzehxib1#

我看到的问题是在情节线上。我认为你应该把数组V中的元素定义为:

for t in plot_times:
plt.plot(y,V[int(t/dt),:],'Gray',label='numerical')

通过这样做,您应该能够运行代码。

相关问题