# Programmatically check if a variable is in an equation in ModelingToolkit.jl

**URL:** https://discourse.julialang.org/t/programmatically-check-if-a-variable-is-in-an-equation-in-modelingtoolkit-jl/101124
**Category:** Modelling & Simulations
**Tags:** question, modelingtoolkit
**Created:** [July 3, 2023, 1:31pm UTC](https://discourse.julialang.org/t/programmatically-check-if-a-variable-is-in-an-equation-in-modelingtoolkit-jl/101124 "2023-07-03T13:31:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [July 3, 2023, 1:31pm UTC](https://discourse.julialang.org/t/programmatically-check-if-a-variable-is-in-an-equation-in-modelingtoolkit-jl/101124/1 "2023-07-03T13:31:39Z")

</div>

Given an `Equation` generated by ModelingToolkit.jl, I want to check, programmatically, whether some variable exists in the equation. MWE:

```julia
using ModelingToolkit

@variables t # independent variable (time)
dDt = Differential(t) # define an operator for the differentiation w.r.t. time
@variables T(t) = 300.0 # temperature, in Kelvin

eq = (dDt(T) ~ T^4)

hasvariable(eq, T) # true, but how to code this function?

# I am also happy with having
hasvariable(eq.rhs, T)

```

context: i am developing some code that should in principle allow me to swap dynamical processes while generating a model, and if some variables exist or not, these processes need to be included or not.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [July 3, 2023, 3:52pm UTC](https://discourse.julialang.org/t/programmatically-check-if-a-variable-is-in-an-equation-in-modelingtoolkit-jl/101124/2 "2023-07-03T15:52:59Z")

</div>

The best thing is to check the incidence instead of analyzing this symbolically. @YingboMa might have a preference on how to expose this better.

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [July 3, 2023, 10:59pm UTC](https://discourse.julialang.org/t/programmatically-check-if-a-variable-is-in-an-equation-in-modelingtoolkit-jl/101124/3 "2023-07-03T22:59:07Z")

</div>

One way would be to use `get_variables` or the in-place version from Symbolics to find the variables within `eq.rhs` and `eq.lhs` separately. Then you can compare against this list.
