English 中文(简体)
页: 1
原标题:Python - Google Colab - IndexError: index 2 is out of bounds for axis 0 with size 1

我正试图解决零散的矩阵方程式。

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 )
问题回答

你特别看到的错误是你重新构建阵列(<>u>)的方法造成的。

Doing u = np.array([(i*h)、(j*h)] results in u:

[[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.], [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]]

这是一系列形状(1、2、10)。 因此,如果您有<代码>u[i+1,j]。 <代码>i+1的唯一有效价值是0,其他一切都不受约束。

鉴于你试图解决的问题,这种做法还有几个问题。

  1. 你希望A成为一种零敲碎打的矩阵,但你将它改成一个密集的矩阵(即:A = csr_matrix(N, N), dtype = np.float64).toarray()。 请删除<代码>toarray()的电话。

  2. You re also initialising u incorrectly, you want a 1-dimensional array of N**2 elements, instead you re initialising a 2D array.

  3. 页: 1 你们首先需要建立矩阵A,然后解决系统问题。

import numpy as np
import scipy.sparse
from scipy.sparse import lil_matrix

def discretise_delta_u_v4(N, method):
    
    # Initialise the matrix A as a list of lists first
    h = 2 / N
    A = lil_matrix((N ** 2, N ** 2), dtype=np.float64)

    if method ==  implicit :
        for i in range(N):
            for j in range(N):
                index = i * N + j
                if i == 0 or i == N - 1 or j == 0 or j == N - 1:
                    A[index, index] = 1
                else:
                    A[index, index] = -4 / (h ** 2)
                    A[index, index - 1] = 1 / (h ** 2)
                    A[index, index + 1] = 1 / (h ** 2)
                    A[index, index - N] = 1 / (h ** 2)
                    A[index, index + N] = 1 / (h ** 2)

    A = A.tocsr(). # You conver to a CSR matrix here

    u = np.zeros(N ** 2)
    u[0:N] = 5  # Per your boundary condition

    u_der_1 = scipy.sparse.linalg.spsolve(A, u)
    return u_der_1.reshape(N, N)

trial1 = discretise_delta_u_v4(10,  implicit )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签