查找适合多个参数的函数

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

textarea

请帮助我,我的数学英语不太好,所以请使用我可以在Google上搜索的词。

weiqchen 回答:查找适合多个参数的函数

以下是对SymPy的solvedocs)和多项式的解释,以给出可能的想法:

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,3),Eq(g.subs(x,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) 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],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,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不愿意使用实数。

本文链接:https://www.f2er.com/3099973.html

大家都在问