I have a code that describes the diffusion equation, but it gives me the following error:
IndexError: only integers, slices (:
), ellipsis (...
), numpy.newaxis (None
) and integer or boolean arrays are valid indices
Could you please help me with this matter? Many thanks!
The code that I have for the diffusion equation:
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])
CodePudding user response:
The problem as I see is in the plot line. I think you should define the element in the array V as:
for t in plot_times:
plt.plot(y,V[int(t/dt),:],'Gray',label='numerical')
By doing that, you should be able to run the code.