寻找适合多个参数的函数

Finding a function that fits several arguments

我想找到一个满足多个参数的 sympy 函数。它应该经过几个点,并且在某些点上具有一定的导数。我该怎么做?

values = np.array([1.0, 2.0], [2.0, 3.0])
derivats = np.array([1.0, 3.0], [2.0, 5.0])

请帮助我,我的数学英语不是很好,所以请使用我会的单词google。

这里是对 SymPy 的 solve (docs) 和多项式的解释,以给出可能性的概念:

from sympy import *

x = symbols('x', real=True)
n = 4  # degree of the polynomial
a = [symbols('a%d' % i, real=True) for i in range(n)]
print(a)
f = sum([a[i]*x**i for i in range(n)])  # f is a polynomial of degree n
g = f.diff(x)
sol = solve([Eq(f.subs(x, 1), 2), Eq(f.subs(x, 2), 3), Eq(g.subs(x, 1), 3), Eq(g.subs(x, 2), 5), ], a)
print(sol)
print(f.subs(sol))

这会打印:

{a0: -15, a1: 37, a2: -26, a3: 6}
6*x**3 - 26*x**2 + 37*x - 15

直接使用您的阵列,这可能是:

from sympy import *

x = symbols('x', real=True)
n = 4  # degree of the polynomial
a = [symbols('a%d' % i, real=True) for i in range(n)]
f = sum([a[i]*x**i for i in range(n)])  # f is a polynomial of degree n
g = f.diff(x)
values = [[1.0, 2.0], [2.0, 3.0]] # suppose an array of pairs [x, f(x)]
derivats = [[1.0, 3.0], [2.0, 5.0]]  # suppose an array of pairs [x, f'(x)]
equations = [Eq(f.subs(x, xi), yi) for xi, yi in values] + [Eq(g.subs(x, xi), yi) for xi, yi in derivats]
sol = solve(equations)
print(sol)
print(f.subs(sol))

这输出:

{a0: -15.0000000000000, a1: 37.0000000000000, a2: -26.0000000000000, a3: 6.00000000000000}
6.0*x**3 - 26.0*x**2 + 37.0*x - 15.0

请注意,与整数和分数相比,SymPy 不太愿意处理实数。