hansonhill - 1 month ago 8

Python Question

I am trying to do symbolical calculations (derivatives mostly) on time-indexed variables using sympy.

Using indexed symbols like r[t] below produces an error:

`from sympy import *`

t = Idx('t',10)

r = IndexedBase('r')

diff(r[t],r[t])

diff(r,r)

ValueError:

Can't differentiate wrt the variable: r[t], 1

Could the reason be that something went wrong here:

`In [15]: r[t].indices`

Out[15]: (t,)

The comma after the index t looks suspicious to me, but I have no idea what went wrong.

Does anyone know how to do this in sympy?

Answer Source

You can differentiate wrt symbols, functions and derivatives. Will this work:

```
>>> t = Idx('t',10)
>>> r=Function('r')
>>> r(t).diff(r(t))
1
>>> var('t')
t
>>> r(t).diff(t)
Derivative(r(t), t)
```