首页 文章

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

提问于
浏览
2

我有一个相当复杂的函数 H(x) ,我正在尝试求解 x 的值 H(x) = constant . 我想用离散间隔生成的插值对象和H(间隔)的相应输出来做到这一点,其中其他输入保持不变 . 我表示插值对象 f .

我的问题是插值对象的调用函数接受array_like,因此将符号传递给 f(x) 以使用sage的求解器方法是不可能的 . 有关如何解决这个问题的任何想法?

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

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:根据规则'safe',无法将数组数据从dtype('0')转换为dtype('float64') .

编辑:任何函数 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)

1 回答

  • 0

    使用numpy和scipy时,更喜欢Python类型到Sage类型 .

    而不是Sage Integers和Reals,使用Python整数和浮点数 .

    也许你可以像这样修复你的代码 .

    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)
    

相关问题