我正试图解决零散的矩阵方程式。
u_derivative_1 = A * u
(A是零散的矩阵)
但我错了以下错误:
IndexError Traceback (most recent call last) <ipython-input-24-f4af80e4ae52> in <cell line: 1>() ----> 1 trial1 = discretise_delta_u_v4(1000, implicit ) <ipython-input-23-731d13e4ddf7> in discretise_delta_u_v4(N, method) 61 for i in range (1 , N-1): 62 for j in range (1 , N-1): ---> 63 A[i,j] = (u[i-1,j] + u[i+1,j] + u[i,j-1] + u[i,j+1] - (4*u[i,j]))/(h**2) 64 IndexError: index 2 is out of bounds for axis 0 with size 1
我感到困惑的是,我为什么会看到这一错误,以及如何解决这一问题。 这是我的法典。
import numpy as np
import scipy
import scipy.sparse
from scipy.sparse import csr_matrix
from scipy.sparse import coo_matrix
def discretise_delta_u_v4(N, method):
i = np.arange(0,N)
j = np.arange(0,N)
h = 2/N
A = csr_matrix((N, N), dtype = np.float64).toarray()
u = np.array([[(i*h), (j*h)]])
#u[ih,jh] =
u[:,0] = 5 #Boundary
u[:,-1] = 0 #Boundary
u[0,:] = 0 #Boundary
u[-1,:] = 0 #Boundary
#Implicit
if (method== implicit ) :
A[0,:] = 0
A[-1,:] = 0
A[:,0] = 0
A[:,-1] = 0
for i in range (1 , N-1):
for j in range (1 , N-1):
A[i,j] = (u[i-1,j] + u[i+1,j] + u[i,j-1] + u[i,j+1] - (4*u[i,j]))/(h**2)
# u_der_1 = A * u
for i in range (0 , N):
for j in range (0 , N):
u_der_1 = scipy.sparse.linalg.spsolve(A,u)
trial1 = discretise_delta_u_v4(1000, implicit )