Sympy中的导数的导数

0 投票
1 回答
46 浏览
提问于 2025-04-14 15:44

我正在用Python做一些计算,现在需要找到一个函数的导数,这个函数我之前已经求过导数,现在叫做 Derivative(x(t), t)

t = sp.symbols('t')
x = sp.symbols('x')
B = sp.symbols('B')
C = sp.symbols('C')
x = sp.Function('x')(t)

Lx = B * sp.diff(x,t) * C

因为在SymPy这个库中,x的导数叫做 Derivative(x(t), t),所以函数Lx可以表示为 B*Derivative(x(t), t)*C。而我们这个函数的导数应该这样命名:

ELx = sp.diff(Lx,Derivative(x(t),t))

但是我总是收到一个错误信息:NameError: name 'Derivative' is not defined,我该怎么办呢?

我的意思是,我可以用另一个变量来定义这个导数函数,但这样看起来逻辑上不太清晰,也不够简洁。

1 个回答

0

你在调用 Derivative() 这个函数,但它并没有被定义。根据 sympy 的文档,正确计算导数的方法是 sp.diff()

下面的代码应该能实现你想要的功能:

import sympy as sp

t = sp.symbols('t')
x = sp.Function('x')(t)
B = sp.symbols('B')
C = sp.symbols('C')

Lx = B * sp.diff(x,t) * C

# Take the derivative of Lx with respect to x(t)
ELx = sp.diff(Lx, x)

参考资料: https://docs.sympy.org/latest/tutorials/intro-tutorial/calculus.html

撰写回答