uho*_*hoh 1 python numpy scipy numerical-methods
我有一个引力势的表达式(这里的方程 15 ),为了计算轨道,我需要评估引力,即局部梯度,对我来说,这意味着评估勒让德多项式P2、P4 和P6 单值数万倍。
我可以使用这个问题中的表达式来计算它,但我想知道是否有一种方法可以向 python 询问导数,而不会明确涉及我将导数评估为有限差分。
我在 SciPy 中找不到任何可以自动执行此操作的内容。有numpy.polynomial.legendre.Legendre一种deriv()方法,但我没有使用多项式类的经验。
计算低阶勒让德多项式的一阶导数(一次一个适合数值积分的值)的最快方法是什么?
我知道这是一个老问题,但仍然没有关于如何使用 numpy/scipy 计算导数的答案。
这就是它仅与 numpy 和 scipy 一起使用的方式:
from scipy.special import legendre
import numpy as np
n = 2 # degree of Legendre polynomial
poly = legendre(n) # coefficients of n^th degree Legendre polynomial
polyd= poly.deriv() # coefficients of derivative of n^th degree Legendre Polynomial
x = np.linspace(0,1,10000) # arbitrary coordinates
evald = np.polyval(polyd,x) # evaluate derivative at desired coordinates(s)
Run Code Online (Sandbox Code Playgroud)
另外,上面接受的答案包含一个小错误(我无法发表评论,但也许有人可以编辑答案):导数应该读P2' = 3*x