如何在python中创建函数,使其以x作为变量返回?(插值)

如何在python中创建函数,使其以x作为变量返回?(插值),python,interpolation,numerical-methods,Python,Interpolation,Numerical Methods,如何在Python中创建function,例如: n = int(input("number of knots: ")) xsolmed=[] for i in range(n+1): xsolmed.append(-1+(2*i/n)) def x(x): return x lni=[] formula=1 for i in range(n+1): for j in range(n+1): if i==j: pass

如何在Python中创建function,例如:

n = int(input("number of knots: "))

xsolmed=[]

for i in range(n+1):
    xsolmed.append(-1+(2*i/n))

def x(x):
    return x
lni=[]
formula=1

for i in range(n+1):
    for j in range(n+1):
        if i==j:
            pass
        formula = (x(x)-xsolmed[i])/(xsolmed[j]-xsolmed[i])*formula
我想我需要它来返回函数,这样公式变量本身就是x的函数,所以以后我可以用这种方式调用它

formula(10)=output
公式(10)是一个函数的实例,因此只有一个值而不是一个变量名

编写上述代码的一个好方法是:

n = int(input("number of knots: "))

xsolmed=[]

for i in range(n+1):
    xsolmed.append(-1+(2*i/n))

def y(x):
    return x

def formula_calc(X):
    formula=1

    for i in range(n+1):
        for j in range(n+1):
            if i==j:
                pass
            formula = (X-xsolmed[i])/(xsolmed[j]-xsolmed[i])*formula

    return formula

# now formula is a function of x. X can itself be a function.
print(formula(y(7))
# this will print value of formula at 7 as x(7) is 7.


将函数调用的结果设置为所需的变量

def(x):
“不改变任何内容的函数”
返回x
a=f(5)#与a=5相同
为了避免混淆,我建议您不要给函数指定与其参数相同的名称(即不要执行
defx(x):…


如果希望
formula
成为一个函数,那么将其声明为一个函数,之后正确的语法将是
output=formula(10)

formula(10)=output有一个称为L值的东西,而R值formula(10)是一个函数的实例,因此只有一个值而不是一个变量名可分配给我知道的值。我想修改我的代码,使其能够以这种方式运行。这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论-