1
Tsup=[]
Te=0
dt=1
t=range(0,3600*2,dt)
r=len(t)
for i in range(0,r):
Tsup.append(20)
....
Tsup.append( (((Pj[i]+Ps-Pc[i]-Pr[i])*(dt))/((m_al*c_al[i])+(m_a*c_a[i])))+Tsup[i] )
if i==r:
Te=(((Pj[i]+Ps-Pc[i]-Pr[i])*(dt))/((m_al*c_al[i])+(m_a*c_a[i])))+Tsup[i]
variacao=[]
T_est=0
for i in range(0,l+1):
variacao.append(abs(Te-Tsup[i]))
if variacao[i] < 0.1 and variacao[i-1] > 0.1:
T_est=Tsup[i]
The error "list index out of range" appears in the variation line.append(abs(Te-Tsup[i])).
I think the
Tsup.append(20)
is unnecessary, you are always putting a value of20
in theTsup
every iteration of the firstfor
. You could use your ownTsup_i
that is20
, when you want to use this value. Already about the errorout of range
. You have to make sure thatTsup
contains at leastl+1
items.– Math
You are trying to access an index that does not exist in the list. For example access element 4 of a list with 3 elements.
– Rafael
Tsup.append(20) was to define that the 1st element of the Tsup list was 20, so it should be defined for the for cycle, right?
– Sofia Raimundo
Where is the variable
l
? The error is not in theappend
, but in theTsup[i]
, as pointed out by @Math. Sometimei
is bigger than the list size because it goes from zero tol+1
, then I assume your listTsup
is smaller thanl+1
.– mgibsonbr
It was a mistake and I already edited, the variable l was r. I tried to comment on the last part and print after "Tsup.append( (((Pj[i]+Ps-Pc[i]-Pr[i])(dt)/((m_alc_al[i])+(m_a*c_a[i]))+Tsup[i] )" to see if you noticed what Tsup values and the error "list index out of range" now appears in a line of code that I don’t have there and that is higher, that is, the error comes from behind..
– Sofia Raimundo
Okay, we found the bug, your list
Tsup
hasr
items and you try to access one beyond the last, with thefor
that goes up tor+1
, move tor
instead ofr+1
should solve this problem ofout of range
, I just don’t know if the logic is right.– Math
Yeah, that makes sense. I’ve already changed the two to for i in range(0,r): but it still gives the same error "list index out of range" in the variacao.append(abs(Te-Tsup[i])).
– Sofia Raimundo
Tsup
is a certain vector? if it is smaller thanlen(r)
that mistake is going to happen anyway– ederwander
It’s a list. So how do I fix this?
– Sofia Raimundo
humm I was looking calmly if
Pj
,Ps
,Pc
orPr
are less thanlen(r)
will happen the error mentioning, what is the size of these lists pj,ps,pc and pr ?? may not be smaller that7200
which is yourr
– ederwander
Inside the 1st is before calculating Tsup, it is calculated: Pj.append(((A)*2) * b * (1 + a * (Tsup[i] - (20+K)))) Ps = a_S * S * D Pc.append(Math.pila_f[i](Tsup[i]- Ta)Nu[i]) Pr.append(Math.piDandbo(((Tsup[i])4)-(Ta4)). as only depend on i, will have the size of r, right?
– Sofia Raimundo
yes everyone has to be greater or equal than
r
all your lists you have severalPr, Pj, Pc, c_a e c_al
check the size of all of them has to be bigger, I ran your code here and created these lists randomly your code ran without index error, have other errors like varl
that doesn’t exist.– ederwander
but simply doing this: dt=1 t=range(0,3600*2,dt) r=Len(t) Tsup=[] Pj=[] Tsup.append(Tsup_i) for i in range(0,r): Pj.append(((A)**2) * R20 * (1 + a * (Tsup[i] - (20+K)))) print Pj immediately gives error list index out of range in line Pj.append(((((A)**2) * R20 * (1 + a * (Tsup[i] - (20+K))), where A, R20, a and K are defined at the beginning. How is it possible?
– Sofia Raimundo
@Sofiaraimundo it is obvious that will give error
Tsup
you started with empty size it is not the size ofr
– ederwander