hansonhill - 6 months ago 80

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?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

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)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**