第19行,在<模块>中,u [i + 1] = u [i]-c * dt / dx *(u [i]-n [i-1])TypeError:'int'对象不可下标

import numpy as np                    # load numpy as np
import matplotlib.pyplot as plt
c=1
n=51
dx=2/(n-1)
t_final=60
dt=0.1
u=np.ones(n)
u[int(.5 / dx):int(1 / dx + 1)] = 2
print(u)
x=np.linspace(0,2,n)
print(x)
t=np.arange(0,t_final,dt)

for j in range(len(t)):
    for i in range(1,n-1):
        u[i+1] = u[i] - c * dt / dx * (u[i] - n[i-1])
    u[0]=1
    u[n-1]=1
plt.figure(1)
plt.plot(x,u)
plt.axis([0,2,0,2])
plt.show()

每当我运行此代码时,都会显示称为第19行的错误,其中u [i + 1] = u [i]-c * dt / dx *(u [i]-n [i-1])TypeError:“ int”对象不可下标

评论
iut
iut
n[i-1]

It's because n is not an array. Did you mean u[i-1] ?

点赞
评论