# ModelingToolkit -- if statements?

**URL:** https://discourse.julialang.org/t/modelingtoolkit-if-statements/125609
**Category:** Modelling & Simulations
**Created:** [February 6, 2025, 9:53am UTC](https://discourse.julialang.org/t/modelingtoolkit-if-statements/125609 "2025-02-06T09:53:32Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [February 6, 2025, 9:53am UTC](https://discourse.julialang.org/t/modelingtoolkit-if-statements/125609/1 "2025-02-06T09:53:32Z")

</div>

Silly question… I need to create a function that I call within an MTK model (computing friction force), and the friction depends on the direction of the velocity… two very different expressions if v\> 0, or whether v \<= 0.

So I tried to do (qualitatively):

```julia
function friction(v)
    if v < 0
        Ff1 = ...
        Ff = Ff1
    else 
        Ff2 = ...
        Ff = Ff2
    end
    return Ff
end

```

When I run the code, I get an error message:

```julia
TypeError: non-boolean (Num) used in boolean context
A symbolic expression appeared in a Boolean context.
...

```

So – I assume this implies that I cannot do boolean tests on MTK _variables_ (such as `v`).

Is the best way to handle this to do the following:

```julia
function friction(v)
    Ff1 = ...
    Ff2 = ...
    Ff = (1-sign(v))*Ff1/2 + (1+sign(v))*Ff2/2
    return Ff
end

```

[Possible problem if v=0?]

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [February 6, 2025, 10:47am UTC](https://discourse.julialang.org/t/modelingtoolkit-if-statements/125609/2 "2025-02-06T10:47:44Z")

</div>

Have you tried `@register_symbolic friction(v::Real)`?

- [Frequently Asked Questions · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/dev/basics/FAQ/#ERROR:-TypeError:-non-boolean-(Num)-used-in-boolean-context)?

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [February 6, 2025, 10:58am UTC](https://discourse.julialang.org/t/modelingtoolkit-if-statements/125609/3 "2025-02-06T10:58:00Z")

</div>

I didn’t try this.

Will the syntax `friction(v::Real)` force the MTK _variable_ (`v` is a function of time) to be converted to a real number within the function `friction`?

[Note: my trick with `(1-sign(v))/2*... + (1+sign(v))/2*...` seems to work for this case, but it is, of course, not a general solution to the problem.]

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [February 6, 2025, 11:02am UTC](https://discourse.julialang.org/t/modelingtoolkit-if-statements/125609/4 "2025-02-06T11:02:32Z")

</div>

> [@BLI](#):
>
> Will the syntax `friction(v::Real)` force the MTK _variable_ (`v` is a function of time) to be converted to a real number within the function `friction`?

No, it will rather prevent the symbols from going inside the function and instead treat the function call `friction(v)` as a symbolic “atom”.
