Python 如何获取某一特定点的导数值?

Bin*_*Bin 2 python sympy lambdify

from sympy import *
x = Symbol('x')
y = x ** 2
dx = diff(y, x)
Run Code Online (Sandbox Code Playgroud)

这段代码可以得到y的导数。这很容易dx = 2 * x

dx现在我想获取for的值x = 2

显然,dx = 2 * 2 = 4x = 2

但是我如何用Python代码实现这一点呢?

感谢您的帮助!

Reb*_*que 5

也许最通用的方法是lambdify

sympy.lambdify创建并返回一个函数,您可以为其指定名称并调用,就像任何其他 Python 可调用函数一样。

from sympy import *

x = Symbol('x')
y = x**2
dx = diff(y, x)
print(dx, dx.subs(x, 2))  # this substitutes 2 for x as suggested by @BugKiller in the comments

ddx = lambdify(x, dx)     # this creates a function that you can call
print(ddx(2))
Run Code Online (Sandbox Code Playgroud)