Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器:索引超出范围_Python_Arrays_Syntax Error_Pde_Odeint - Fatal编程技术网

Python 索引器:索引超出范围

Python 索引器:索引超出范围,python,arrays,syntax-error,pde,odeint,Python,Arrays,Syntax Error,Pde,Odeint,我是Python新手,非常感谢任何建设性的反馈。 我的任务是使用ODEINT解决PDE,其中我的空间网格定义如下: L = [8.0, 4.0, 8.0] # Lenght of spatial zones dN = 1.0e2 # Number of grid points N = [int(l*dN) for l in L] # Gives number of grid points over each spatial zone 通过使用ODEINT函数,我必须

我是Python新手,非常感谢任何建设性的反馈。 我的任务是使用ODEINT解决PDE,其中我的空间网格定义如下:

L = [8.0, 4.0, 8.0]   # Lenght of spatial zones 
dN = 1.0e2            # Number of grid points
N = [int(l*dN) for l in L] # Gives number of grid points over each spatial zone
通过使用ODEINT函数,我必须为我的解决方案定义一个零数组和一个时间点数组。我正在努力找出如何为数组的大小写入正确的大小。我试过:

dydt = np.zeros(shape=N) # For solution
t = np.linspace(0, 2, 2000) # for time
但这给了我一个错误信息: 索引器回溯(最后一次最近调用) 在() 70 t=np.linspace(0,2,lN) 71 --->72 U=odeint(数字,y0,t,args=(h,D1,D2,N)) 73 74 Fexit=U/Np

/odeint中的Users/severinlangeberg/anaconda/lib/python3.5/site-packages/scipy/integrate/odepack.py(func、y0、t、args、Dfun、col_deriv、full_输出、ml、mu、rtol、atol、tcrit、h0、hmax、hmin、ixpr、mxstep、mxhnil、mxordn、mxwords、printmessg) 213输出=_odepack.odeint(func、y0、t、args、Dfun、col_deriv、ml、mu、, 214全输出,rtol,atol,tcrit,h0,hmax,hmin, -->215 ixpr、mxstep、mxhnil、mxordn、mxords) 216如果输出[-1]<0: 217警告\u msg=\u msgs[output[-1]]+“使用完整的\u output=1运行以获取定量信息。”

数字形式(y、t、h、D1、D2、N) 39 40对于[N[0]+1]中的i:#800=special 2,必须从N[1]开始 --->41 dydt[i]=D2*(-3*y[i]+4*y[i+1]-y[i+2]/h**2)加 42 43对于范围(N[0]+2,N[0]+N[1]-2)内的i:#从801到1197,或从2到397

索引器:索引801超出大小为800的轴0的界限

Which seems to come from my loops:
# ODE setup
def numeric(y, t, h, D1, D2, N):

    dydt = np.zeros((N))

    # End grid points
    dydt[0] = 0
    dydt[-1] = 0


    # Inner grid points
    for i in range (1, N[0] - 2): # 1 to 797
        dydt[i] = D1 * (y[i - 1] - 2*y[i]+ y[i + 1]) / h**2 # range i = j - 1

    for i in [N[0] - 2]: # 798 = special 1
        dydt[i] =  D1 * (-3*y[i] - 4*y[i - 1] + y[i - 2]) / h**2 # minus

    for i in [N[0] - 1]: # 799 = unknown, and last index in N[0]
        dydt[i] = ((D1*(-3*y[i] + 4*y[i + 1] - y[i + 2])) - (D2*(-3*y[i] - 4*y[i - 1] + y[i - 2])))

    for i in [N[0] + 1]: # 800 = special 2, must be from begining of N[1]
        dydt[i] = D2 * (-3*y[i] + 4*y[i + 1] - y[i + 2] / h**2) # pluss

    for i in range (N[0] + 2, N[0] + N[1] - 2): # from 801 to 1197, or 2 to 397
        dydt[i] = D2 * (y[i - 1] - 2*y[i] + y[i + 1]) / h**2 # range

    for i in [N[0] + N[1] - 1]: # at 1198, or 398 = special 3
        dydt[i] = D2 * (-3*y[i] - 4*y[i - 1] + y[i - 2]) / h**2 # range

    for i in [N[0] + N[1]]: # 1199, or 399 = unknown
        dydt[i] = ((D1*(-3*y[i] + 4*y[i + 1] - y[i + 2])) - (D2*(-3*y[i] - 4*y[i - 1] + y[i - 2])))

    for i in [N[0] + N[1] + 1]: # at 1200, or 1 = special 4
        dydt[i] =  D1 * (-3*y[i + 1] + 4*y[i + 2] - y[i + 3] / h**2)  # pluss

    for i in range (N[0] + N[1] + 2, N[0] + N[1] + N[2] - 1): # 1202 to 1999
        dydt[i] = D1 * (y[i - 1] - 2*y[i]+ y[i + 1]) / h**2 # range

    return dydt

快速查看,问题似乎是for循环,而不是在[N[0]]中为i使用
,您确定它不是在范围(N[0])中为i使用

[N[0]]
中i的
是相当冗余的,因为列表只包含一个元素,即
N[0]
,因此当您尝试查找
dydt[800]
2 dydt[i]=0时,大小800的列表将超出索引 3. 索引器:索引800超出大小为800的轴0的界限 [11]中:对于范围(N[0])中的i: ..:dydt[i]=0 ....:
在此代码中:

for i in[N[0]]: 
    dydt[i] = 0 # Unknown point
I始终为800,因此它实际上与
dydt[800]=0
相同。比799大

看起来你用错了numpy数组

是的,在低级别中,它们表示为int或float的连续数组。但是在python级别上,np.array表示为数组的数组。(即第一个元素是3d矩阵
mtx=np.zero(python中的shape=[800400800]
将是
mtx[0][0][0]

因此[N[0]]中i的
之后的所有代码:
将不起作用。它试图访问超出矩阵边界的子阵列

更新:

例如,您有3d矩阵(3x3x3)


是的,我想在点N[0]中指定,dydt[I]=0。这就是为什么我写了“因为我在[N[0]]”。关于我能做什么,你有什么建议吗?@GSEL,如果你想把dydt的最后一个索引赋值为0,那么应该是
dydt[N[0]-1]=0
。谢谢!那么,如果N[0]的最后一个元素是N[0-1],这意味着N[0]不存在?我想做的是将PDE的空间网格划分为三个区域,并在每个区域的末尾和开头应用额外的边界条件。结果应该都在一个矩阵中,但从我之前所做的方式来看,我有一个由三列而不是一列组成的解矩阵。这是正确的吗?你知道用ODEINT求解时会不会有问题吗?@GSEL,我不擅长PDE或任何数学理论。。。但从你想说的话来看,这可能就是你想要的吗?对于边界值,我有一个谷歌搜索,你可能会感兴趣,这意味着语法:for I in[N[0]]:dydt[I]=0将所有矩阵值设置为0,或者程序试图在dydt矩阵中找到更多的子数组?很抱歉,我不熟悉Python。dydt[I]=0将2d矩阵设置为0。因为这是多余的。太好了!我已将代码更新为:L=[8.0,4.0,8.0]#长度区域(cm)dN=1.0e2#网格点/cm N=[int(L*dN)表示L中的L]#好!lN=N[0]+N[1]+N[2],带:dydt=np.zero((lN)),我希望这会给我一个lNx1矩阵来存储我的解。方程更新为不含dydt[N0-1]=0,但根据您在更新中的解释,我想知道我设置for循环的方式是否只给出了最后一个循环方程的结果?这意味着循环会互相覆盖吗?感谢您抽出时间回答!它们只有在重叠时才会相互覆盖。在第四个循环中(例如)(范围(N[0]+1)中的i的
):范围将生成从0到N[0]的数字(包括),它将覆盖第一个
(从0到N[0]-2包括),第二个
(从N[0]-1到N[0]-1包括)和第三个(从N[0]到N[0])。第五、第六、第七和第八个
for
循环不会覆盖以前循环中的任何内容。但最后一个循环的范围是从N[0]+N[1]+2到N[2]-1(即从1202到799),它将以向后顺序覆盖privoius循环。我想最后一个循环应该是范围(N[0]+N[1]+2,N[0]+N[1]+N[2]-1)(即从1202到1999)或(N[0]+N[1]+2,in-1)(这是相同的)请只共享您面临问题的代码的相关部分。你的问题应该简洁明了,这样在回答问题时可以节省我们的时间。
L = [8.0, 4.0, 8.0] 
dN = 1.0e2 
N = [int(l*dN) for l in L] # create array [800, 400, 800] 

dydt = np.zeros(shape=N) # create 3 dimensional matrix 
                         # 1d has size 800 (from 0 to 799 including)
                         # 2d has size 400 (from 0 to 399 including)
                         # 3d same as 1st
for i in[N[0]]: 
    dydt[i] = 0 # Unknown point
 mtx=np.zero(shape=[3,3,3])
 print(mtx) 
 #[ [ [ 0.  0.  0.]
 #    [ 0.  0.  0.]
 #    [ 0.  0.  0.] ]

 #  [ [ 0.  0.  0.]
 #    [ 0.  0.  0.]
 #    [ 0.  0.  0.] ]

 #  [ [ 0.  0.  0.]
 #    [ 0.  0.  0.]
 #    [ 0.  0.  0.] ] ]

 mtx[0] = 1 
 print( mtx )
 #[ [ [ 1.  1.  1.]
 #    [ 1.  1.  1.]
 #    [ 1.  1.  1.] ]

 #  [ [ 0.  0.  0.]
 #    [ 0.  0.  0.]
 #    [ 0.  0.  0.] ]

 #  [ [ 0.  0.  0.]
 #    [ 0.  0.  0.]
 #    [ 0.  0.  0.] ] ]

 mtx[0][1] = 2
 #[ [ [ 1.  1.  1.]
 #    [ 2.  2.  2.]
 #    [ 1.  1.  1.] ]

 # others are zeros

 mtx[0][1][2] = 3
 #[ [ [ 1.  1.  1.]
 #    [ 2.  2.  3.]
 #    [ 1.  1.  1.] ]

 # others are zeros