Derivative of a Derivative in sympy

48 views Asked by At

I am trying to do some calculations in python and now need to find the derivative of the a function, that I have derived prior and is now called 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

Because the derivative of x is called Derivative(x(t),t) by SymPy,the function Lx is equal to B*Derivative(x(t),t)*C and the derivative of our function must be called the following:

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

But I allways get an error message: NameError: name 'Derivative' is not defined what should I do.

I mean I can define the Derived function with antoher varibale, but the logic and cleaner way looks like this.

1

There are 1 answers

1
J.Tmr On

You are calling Derivative() without that being defined. According to the documentation of sympy the correct way to calculate a derivative is sp.diff().

The following code should do what you're trying to achieve:

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)

References: https://docs.sympy.org/latest/tutorials/intro-tutorial/calculus.html