我想绘制这个非齐次抛物线 pde 函数的 100,200 和 400 次迭代
def solver(L, Nx, Nt, T, theta=0.5):
x = np.linspace(-L,L, Nx+1) # mesh points in space
dx = (L-(-L))/(Nx-1)
dt = 1/Nt
Nt = int(round(T/float(dt)))
t = np.linspace(0, T, Nt+1) # mesh points in time
h = (L-(-L))/(Nx+1)
t = T / Nt
m = t/h**2
#print("m =", round(m,2))
u = np.zeros(Nx+1) # solution array at t[n+1]
u_n = np.zeros(Nx+1) # solution at t[n]
a = np.zeros(Nx+1) + (1+x**2)
c = np.ones(Nx+1) * -1
Dl = 0.5*theta
Dr = 0.5*(1-theta)
diagonal = np.zeros(Nx+1)
lower = np.zeros(Nx)
upper = np.zeros(Nx)
b = np.zeros(Nx+1)
diagonal[1:-1] = 1 + Dl*(a[2:] + 2*a[1:-1] + a[:-2])
lower[:-1] = -Dl*(a[1:-1] + a[:-2])
upper[1:] = -Dl*(a[2:] + a[1:-1])
diagonal[0] = 1
upper[0] = 0
diagonal[Nx] = 1
lower[-1] = 0
A = scipy.sparse.diags(
diagonals=[diagonal, lower, upper],
offsets=[0, -1, 1],
shape=(Nx+1, Nx+1),
format='csr')
def I(x):
return(np.sin(np.pi*x))
for i in range(0,Nx+1):
u_n[i] = I(x[i])
# Time loop
for n in range(0, Nt):
b[1:-1] = u_n[1:-1] + Dr*(
(a[2:] + a[1:-1])*(u_n[2:] - u_n[1:-1]) -
(a[1:-1] + a[0:-2])*(u_n[1:-1] - u_n[:-2])) + dt*theta*c[1:-1] + dt*(1-theta)*c[1:-1]
# Boundary conditions
b[0] = 0
b[-1] = 0
# Solve
u[:] = scipy.sparse.linalg.spsolve(A, b)
u_n,u = u, u_n
plt.plot(u[:,100])
我收到此错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-91-41c85be859a3> in <module>
----> 1 Back = solver(L=1, Nx=39, Nt=400, T=1, theta=1)
<ipython-input-90-ef7886a9d8f0> in solver(L, Nx, Nt, T, theta)
68 u_n,u = u, u_n
69
---> 70 plt.plot(u[:,100])
71
IndexError: too many indices for array
有人能帮我吗?

