插值温度与高度

时间:2014-09-17 03:23:39

标签: python list int interpolation

我正在尝试制作一个返回给定高度温度的插值代码

def Fdrag(y):
    alt = [a1, a2, a3,....,an]
    temp = [t1, t2, t3,...., tn]
    for i in range(len(alt)):
        if alt[i] < y < alt[i+1]:
            temp = temp[i] + (y - y[i])*(temp[i+1]-temp[i])/(alt[i+1] - alt[i])
    return temp

我一直收到这个错误:

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    Fdrag(10)
  File "C:/Users//Desktop/interp_test.py", line 6, in Fdrag
    temp = temp[i] + (y - y[i])*(temp[i+1]-temp[i])/(alt[i+1] - alt[i])
TypeError: 'int' object is not subscriptable

1 个答案:

答案 0 :(得分:0)

你说y[i]

        temp = temp[i] + (y - y[i])*(temp[i+1]-temp[i])/(alt[i+1] - alt[i])

但我认为你的意思是alt[i]

        temp = temp[i] + (y - alt[i])*(temp[i+1]-temp[i])/(alt[i+1] - alt[i])