You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SymPy中对含时间的sin、cos函数求关于时间的导数

问题成因
  • 你运行diff(cos(t), t)得到-sin(t)是完全符合预期的:此时cos的参数就是时间t本身,对应你提到的导数规则中d(theta)/dt = 1,代入计算可得-sin(theta) * d(theta)/dt = -sin(t) * 1 = -sin(t),和输出结果一致,不存在错误。
  • 你运行diff(cos(theta), t)得到0的核心原因是:没有显式声明theta是关于时间t的函数,SymPy会默认theta是和t无关的独立常量,常量对t求导的结果自然为0。
正确实现方法

你需要通过Function类将theta显式定义为t的函数,即可触发链式求导得到你预期的结果,完整示例代码如下:

from sympy import Symbol, diff, cos, sin, Function

t = Symbol('t')
# 显式声明theta为t的函数
theta = Function('theta')(t)

# 对cos(theta)求t的导数
print(diff(cos(theta), t))
# 输出:-sin(theta(t))*Derivative(theta(t), t)

# 对sin(theta)求t的导数
print(diff(sin(theta), t))
# 输出:cos(theta(t))*Derivative(theta(t), t)

如果需要代入theta的具体表达式计算最终结果,可以搭配subs和doit方法执行:

# 示例:theta = 2*t + 5
res = diff(cos(theta), t).subs(theta, 2*t + 5).doit()
print(res)
# 输出:-2*sin(2*t + 5)

内容的提问来源于stack exchange,提问作者TMoover

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 07:45:05