简体   繁体   English

使用 sympy 关于时间的导数

[英]Derivative with respect to time using sympy

I looking for a way to declare a variable as a function of time, to then perform the time derivative.我正在寻找一种方法来将变量声明为时间的函数,然后执行时间导数。 ie IE

import sympy as sp
from sympy import cos
from sympy import sin

t = sp.symbols('t')
x(t) = sp.symbols('x(t)')
f = cos(x(t))*sin(x(t))
df = sp.diff(f, t)

However, this code generates the following error:但是,此代码会生成以下错误:

    x(t) = sp.symbols('x(t)')
    ^
SyntaxError: cannot assign to function call

Well why not use Function like so那么为什么不像这样使用Function

import sympy as sp
from sympy import sin, cos, Function

t = sp.symbols('t')
x = Function('x')
f = cos(x(t))*sin(x(t))
df = sp.diff(f, t)
df

衍生物

I found a solution to this problem by using the "Function" function, as follows:我通过使用“Function”函数找到了解决这个问题的方法,如下:

t = sp.symbols('t')
x = sp.Function('x')(t)
f = cos(x)*sin(x)
df = sp.diff(f, t)

The result yields the following expression:结果产生以下表达式:

-sin(x(t))**2*Derivative(x(t), t) + cos(x(t))**2*Derivative(x(t), t)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM