# How to substitute variable in expression contains its derivative also

**URL:** <https://discourse.julialang.org/t/how-to-substitute-variable-in-expression-contains-its-derivative-also/88241>\
**Category:** General Usage\
**Tags:** symbolics, sympy\
**Created:** [October 4, 2022, 4:39pm UTC](https://discourse.julialang.org/t/how-to-substitute-variable-in-expression-contains-its-derivative-also/88241 "2022-10-04T16:39:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [October 4, 2022, 4:39pm UTC](https://discourse.julialang.org/t/how-to-substitute-variable-in-expression-contains-its-derivative-also/88241/1 "2022-10-04T16:39:02Z")

</div>

```julia
 using SymPy
using PyCall
sp = pyimport("sympy")
t1 = sp.Function("theta_1")(t)
f = t1^2 +t1
df = sp.diff(f,t)
# so df = 2*t1*(d(t1)/d(t))+ (d(t1)/d(t))
# if substitution done of t1 value that is t1 = 2 
s = df.subs([(t1,2)])
# the s is s = 2*2*(d(2)/d(t))+ (d(2)/d(t))
# but i need s =2*2*(d(t1)/d(t))+ (d(t1)/d(t))

```

so i need that sympy will treat two different things t1 and d(t1)/dt  
thanks…

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [October 4, 2022, 5:28pm UTC](https://discourse.julialang.org/t/how-to-substitute-variable-in-expression-contains-its-derivative-also/88241/2 "2022-10-04T17:28:02Z")

</div>

```julia
using SymPy

@syms t t1()

f = t1(t)^2 +t1(t)
df = diff(f, t)

@syms dt1
s = subs(df, (diff(t1(t), t), dt1))
s = subs(s, (t1(t), 2))
s = subs(s, (dt1, diff(t1(t), t)))

```

---

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [October 4, 2022, 11:35pm UTC](https://discourse.julialang.org/t/how-to-substitute-variable-in-expression-contains-its-derivative-also/88241/3 "2022-10-04T23:35:35Z")

</div>

Thanks, but this not a stable method, I also deal this problem like that. but if i have 10 variable with second order differential equation. i have to write substitute things every time. is there any method or library or python sympy method for that?  
thanks…
