Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i'm trying to make an interpolation code that returns the temperature at a given height

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

i keep getting this error:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
163 views
Welcome To Ask or Share your Answers For Others

1 Answer

You said y[i]:

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

but I think you meant alt[i]:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...