英文:
IndexError: index 3650 is out of bounds for axis 0 with size 3650
问题
为什么会出现越界错误?
t_end = 3650
TS = np.zeros(t_end)
TS[0] = 288
for tt in range(0,len(TS)):
TS[tt+1] = TS[tt]+(deltat/cw)*((s0/4)*(1-a_earth)+(ea_new[tt]*sigma*(T_earth**4))-(sigma*TS[tt]**4))
先谢谢啦
我只是在尝试完成我的作业。
英文:
Why do i get the Out of Bounds Error?
t_end = 3650
TS = np.zeros(t_end)
TS[0] = 288
for tt in range(0,len(TS)):
TS[tt+1] = TS[tt]+(deltat/cw)*((s0/4)*(1-a_earth)+(ea_new[tt]*sigma*(T_earth**4))-(sigma*TS[tt]**4))
Thanks already:)
I was just trying to do my Homework
答案1
得分: 1
Python的索引从0开始。当您循环遍历数组TS的长度范围时,在最后一次迭代中,您将获得tt = 3649,这是数组TS的最后一个索引。由于您正在添加1,因此在tt = 3649处越界,因为它试图到达索引TS[3649 + 1],该索引不存在。
只需将此部分代码替换为:TS[tt]
,以使其保持在边界内。
编辑:
如果您想将第一个值设置为288,只需添加以下if语句:
for tt in range(0, len(TS)):
if tt == 0:
TS[tt] = 288
else:
TS[tt] = TS[tt] + (deltat / cw) * ((s0 / 4) * (1 - a_earth) + (ea_new[tt] * sigma * (T_earth ** 4)) - (sigma * TS[tt] ** 4))
英文:
Python indexing starts at 0. As you are looping through the range of the length of array TS, at the last iteration you will get tt = 3649, which is the last index of the array TS. Since you are adding 1, it is going out of bounds at tt = 3649 as it is trying to reach the index TS[3649 + 1] which does not exist.
Just replace this: TS[tt+1]
with TS[tt]
to keep it within the bounds.
Edit:
If you'd like to set the first value to be 288 then just add the following if statement:
for tt in range(0,len(TS)):
if tt == 0:
TS[tt] = 288
else:
TS[tt] = TS[tt]+(deltat/cw)*((s0/4)*(1-a_earth)+(ea_new[tt]*sigma*(T_earth**4))-(sigma*TS[tt]**4))
答案2
得分: 0
根据先前的值TS[tt]
来计算新值TS[tt + 1]
时,循环需要在倒数第二个位置结束:
for tt in range(0, len(TS) - 1):
此外,你需要提供一个起始值TS[0]
,以便在循环之前计算第一个值TS[1]
,如TS[0] = 0
(或适当的任何值)。
英文:
As you calculate the new value TS[tt + 1]
depending on the previous one TS[tt]
, the loop needs to end at the last-to-one position:
for tt in range(0,len(TS) - 1):
In addition, you have to provide a starting value TS[0]
to be able to calculate the first value TS[1]
, right before the loop, as TS[0] = 0
(or whatever value is suitable).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论