符号计算插值函数的输入?

2024-06-28 18:58:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个相当复杂的函数H(x),我试图求解x的值,这样H(x) = constant。我想用一个由离散区间和相应的H(区间)输出生成的插值对象来实现这一点,其中其他输入保持不变。我表示插值对象f。你知道吗

我的问题是interpolation对象的call函数接受一个数组,因此将一个符号传递给f(x)以使用sage的solver方法是不可能的。你知道怎么解决这个问题吗?你知道吗

我有插值函数f。我想解方程f(x) == sageconstant forx .

    from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
    import numpy as np

    #Generating my interpolation object
    xint = srange(30,200,step=.1)
    val = [H(i,1,.1,0,.2,.005,40) for i in srange(30,299,step=.1)]
    f = IUspline(xint,val,k=4)

    #This will yield a sage constant
    eq_G(x) = freeB - x 

    #relation that I would like to solve
    eq_m(x) = eq_G(39.9) == f(x)
    m = solve(eq_m(x),x)

上面的代码(f(x)更具体地说)生成

"TypeError: Cannot cast array data from dtype('0') to dtype('float64') according to the rule 'safe'.

编辑:任何函数H(x)都会导致相同的错误,因此H(x)是什么并不重要。为了简单起见(我说H很复杂不是开玩笑),试试H(x) = x。然后该块将显示:

    from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
    import numpy as np

    #Generating my interpolation object
    xint = srange(30,200,step=.1)
    H(x) = x
    val = [H(i) for i in srange(30,299,step=.1)]
    f = IUspline(xint,val,k=4)

    #This will yield a sage constant
    eq_G(x) = freeB - x 

    #relation that I would like to solve
    eq_m(x) = eq_G(39.9) == f(x)
    m = solve(eq_m(x),x)

Tags: to对象函数importasstepvaleq
1条回答
网友
1楼 · 发布于 2024-06-28 18:58:54

使用numpy和scipy时,首选Python类型而不是Sage类型。你知道吗

使用Python int和float代替Sage整数和实数。你知道吗

也许你可以这样修改你的代码。你知道吗

from scipy.interpolate import InterpolatedUnivariateSpline as IUspline
import numpy as np

# Generate interpolation object
xint = srange(30,200,step=.1)
xint = [float(x) for x in xint]
val = [float(H(i,1,.1,0,.2,.005,40)) for i in srange(30,299,step=.1)]
f = IUspline(xint,val,k=4)

# This will yield a Sage constant
eq_G(x) = freeB - x 

# relation that I would like to solve
eq_m(x) = eq_G(39.9) == f(x)
m = solve(eq_m(x),x)

相关问题 更多 >