“数组索引太多”的错误

时间:2015-05-20 09:56:06

标签: python for-loop matrix

我想生成一个带有两个for循环的2D矩阵I[x,t]。这是一个70*60矩阵 我首先用一行和60列计算我的向量。然后,在内部for循环中,对于每个t步骤,我使用I为我的x计算矩阵VI[x,t]是一列和70行的向量。
我面临IndexError: too many indices for array 的以下错误:

from math import *
from pylab import *
from numpy import *

N=60 #number of cells
Ir=zeros((1,N))
Ir=Ir+1000.0
Temp=zeros((1,N))
Temp=Temp+25.0
V = arange(0,0.7,0.01)
Area=243.36
ns=1
np=1
Jsc_cell = 0.03785
Isc_cell = Jsc_cell * Area
n1=1.0
J01 = 6.2e-13
Is1 = J01 * Area
n2= 2.0
J02 = 7.3e-9
Is2= J02 * Area
T_co = 0.0005
Rs = 2.7487398e-3
Rsh = 410913.8725
k = 1.38e-23
q = 1.6e-19
Eg=1.11
Tmeas= 273+25.0

Iph=zeros((1,N))
I0=zeros((1,N))
I02=zeros((1,N))
Vt=zeros((1,N))
I=zeros((len(V),N))
P=zeros((len(V),N))
T=zeros((1,N))
Pmpp_cell=zeros((1,N))
Impp_cell=zeros((1,N))
Vmpp_cell=zeros((1,N))
Isc_cell_c=zeros((1,N))

for t in range(0,N):
    T[0,t] = Temp[0,t]+273.0
    Vt[0,t]=(k*T[0,t])/q
    I0[0,t]=Is1*((T[0,t]/Tmeas)**(3/n1))*exp(Eg*((T[0,t]/Tmeas)-1)/(n1*Vt[0,t]));
    I02[0,t]=Is2*((T[0,t]/Tmeas)**(3/n2))*exp(Eg*((T[0,t]/Tmeas)-1)/(n2*Vt[0,t]));
    Iph[0,t] = Isc_cell*(Ir[0,t]/1000.0)*(1+(T_co*(Temp[0,t]-25)));

    for x in range(0,len(V)):
        I[x,t] = Iph[0,t] - I0[0,t]*(exp((V[x,0]+I[x,t]*Rs)/(n1*Vt[0,t]))-1)-I02[0,t]*(exp((V[x,0]+I[x,t]*Rs)/(n2*Vt[0,t]))-1)-((V[x,0]+I[x,t]*Rs)/Rsh)
        P[x,t] = I[x,t]*V[x,0]
        x=x+1
t=t+1 

如果有人帮我纠正,我会很高兴的。

-- Find top 5 queries
SELECT TOP 5 query_stats.query_hash AS "Query Hash", 
    SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) AS "Avg CPU Time",
    MIN(query_stats.statement_text) AS "Statement Text"
FROM 
    (SELECT QS.*, 
    SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,
    ((CASE statement_end_offset 
        WHEN -1 THEN DATALENGTH(st.text)
        ELSE QS.statement_end_offset END 
            - QS.statement_start_offset)/2) + 1) AS statement_text
     FROM sys.dm_exec_query_stats AS QS
     CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as ST) as query_stats
GROUP BY query_stats.query_hash
ORDER BY 2 DESC;
GO

P.S。是否有任何解决方案可以避免提前定义参数及其尺寸?

1 个答案:

答案 0 :(得分:0)

在运行代码时,我看到你的变量V是初始化的1D-array:

V = arange(0,0.7,0.01)

但是,在最终计算I[x,t]P[x,t]期间,您使用的是V的二维索引。将V[x,0]替换为V[x],您的代码就会完成而不会出现错误。