2014-11-22 129 views
0

我目前得到以下,但它不會迭代我。我不明白爲什麼它不起作用。波長和吞吐量是列表。看來,我從0開始,但不會增加至1Python:爲什麼這不起作用?/這個錯誤究竟是什麼意思?

ABconstant=[] 

c=3e18 
for i in range(0, ((len(Bwavelength))-1)): 
    ABconstant1=(((3e18/((Bwavelength[i])**2))*throughput[i])) 
    ABconstant.append(ABconstant1) 
    i+=1 
    a=Bwavelength[0] 
    b=Bwavelength[-1] 
    h=((b-a)/len(Bwavelength)) 
    ABflux = numpy.trapz(Bwavelength, ABconstant, h) 
return ABflux 

我得到的錯誤是:

Traceback (most recent call last): 
    File "Rewrite17.11.2014.py", line 196, in <module> 
    ABflux1 = ABconversion(Bwavelength, throughput) 
    File "Rewrite17.11.2014.py", line 186, in ABconversion 
    ABflux = numpy.trapz(Bwavelength, ABconstant, h) 
    File "C:\Python27\lib\site-packages\numpy\lib\function_base.py, line 3234, in trapz 
    ret = add.reduce(d * (y[slice1]+y[slice2]/2.0, axis) 
ValueError: Operands could not be broadcast together with shapes (0,) (444,) 

Bwavelength和吞吐量長度相等。

我不知道這實際上是什麼意思,儘管查了它。

在此先感謝。

+0

http://stackoverflow.com/a/11856572/903790 – doniyor 2014-11-22 18:45:43

+0

Bwavelength和吞吐量具有相同的長度,所以可靠地ABconstant應該是相同的長度,如果我遍歷Bwavelength的長度? – NXW 2014-11-22 18:49:55

+0

應該整合什麼? 'Bwavelength'還是'ABconstant'? – Daniel 2014-11-22 19:02:34

回答

2

環路可以通過矢量計算取代:

c=3e18 
ABconstant = c/numpy.array(Bwavelength) ** 2 * throughput 
ABflux = numpy.trapz(ABconstant, Bwavelength) 
return ABflux 
+1

我認爲你已經糾正了,只有兩行,OP代碼中的錯誤數量不是微不足道的...... – gboffi 2014-11-22 20:14:18