Python Fanspeed插值-返回错误值

Python Fanspeed插值-返回错误值,python,interpolation,Python,Interpolation,目前,我正在使用python中的插值。你有一个温度和转速风扇表。输入为温度,输出为新处理的rpm值。我没有得到正确的值。你能帮我吗 TABLE = [ (0, 0), (20, 10), (50, 30), (80, 90), (100, 100)] def interPolation(table, input): if input < table[0][0]: return table[0][1] elif input > table[-1][0]

目前,我正在使用python中的插值。你有一个温度和转速风扇表。输入为温度,输出为新处理的rpm值。我没有得到正确的值。你能帮我吗

TABLE = [
(0, 0),
(20, 10),
(50, 30),
(80, 90),
(100, 100)]

def interPolation(table, input):
    if input < table[0][0]:
        return table[0][1]
    elif input > table[-1][0]:
        return table[-1][1]
    else:
        for n, _ in enumerate(table):
            if table[n][0] <= input <= table[n + 1][0]:
                return (table[n + 1][0] - table[n][0]) / (table[n + 1][1] - table[n][1]) * input + table[n][1]

print interPolation(TABLE, 66)
表=[
(0, 0),
(20, 10),
(50, 30),
(80, 90),
(100, 100)]
def插值(表格,输入):
如果输入<表[0][0]:
返回表[0][1]
elif输入>表[-1][0]:
返回表[-1][1]
其他:
对于n,枚举中的u(表):

如果表[n][0]我不知道你在做什么样的插值,但似乎你在尝试做线性插值,在这种情况下,你在这里应用了一个错误的公式,你所有公式中的第一个是维度错误的。我已尝试更正该部分,请检查它是否为您提供了正确的答案

data = [
(0, 0),
(20, 10),
(50, 30),
(80, 90),
(100, 100)]

def interPolation(table, temp):
    if temp < table[0][0]:
        return table[0][1]
    elif temp > table[-1][0]:
        return table[-1][1]
    else:
        for n, _ in enumerate(table):
            if table[n][0] <= temp <= table[n + 1][0]:
                return (table[n + 1][1] - table[n][1]) / (table[n + 1][0] - table[n][0]) * (temp-table[n][0]) + table[n][1]

print interPolation(data, 66)
数据=[
(0, 0),
(20, 10),
(50, 30),
(80, 90),
(100, 100)]
def插值(表,温度):
如果温度<表[0][0]:
返回表[0][1]
elif temp>表[-1][0]:
返回表[-1][1]
其他:
对于n,枚举中的u(表):

如果表[n][0]首先,我建议对表进行排序,以防万一。排序将在函数内部完成

如果您没有对其进行排序,并且它是以相反或任意顺序提供的(无论出于何种原因),那么检查以确保您在范围内的
If
elif
语句将是错误的。或者,您可以使用
min()
max()
函数

最后,对于插值发生的
else
部分,我将使用
while
语句来搜索最接近的记录值(这里排序表也很重要),并进行如下计算:

def interPolation(table, user_value):
    table = sorted(table, key=lambda x: x[0])
    if user_value < table[0][0]:
        return table[0][1]
    elif user_value > table[-1][0]:
        return table[-1][1]
    else:
        i = 0
        while user_value >= table[i][0]:
            i += 1
        return table[i-1][1] + (user_value - table[i-1][0]) * (table[i][1] - table[i-1][1]) / (table[i][0] - table[i-1][0])

table_inp = [(0, 0), (20, 10), (50, 30), (80, 90), (100, 100)]

print(interPolation(table_inp, 66))  # returns 62.0
def插值(表格,用户值):
table=已排序(table,key=lambda x:x[0])
如果用户_值<表[0][0]:
返回表[0][1]
elif用户值>表[-1][0]:
返回表[-1][1]
其他:
i=0
当用户_值>=表[i][0]时:
i+=1
返回表[i-1][1]+(用户_值-表[i-1][0])*(表[i][1]-表[i-1][1])/(表[i][0]-表[i-1][0])
表_inp=[(0,0)、(20,10)、(50,30)、(80,90)、(100100)]
打印(插值(表inp,66))#返回62.0

正如您可能已经注意到的,我更改了您的变量名。一般规则是使用小写字母(
TABLE
改为
TABLE
),并避免使用绑定到python函数的名称(
input()
改为
user\u value

您期望的值是什么,得到的是什么?是的,这是线性插值。您的代码看起来更好,但我没有得到正确的值。e、 g.Input=10,Output=0No对于Input 10,它会给我输出5,如果您使用的是python 2,那么请尝试从
\uuuuuuuuuuuuuuuuu
导入除法。