# Can I use if statements in ParameterisedFunctions.jl? Is there a work around?

**URL:** <https://discourse.julialang.org/t/can-i-use-if-statements-in-parameterisedfunctions-jl-is-there-a-work-around/79347>\
**Category:** Optimization (Mathematical)\
**Tags:** matlab, ode\
**Created:** [April 11, 2022, 2:57pm UTC](https://discourse.julialang.org/t/can-i-use-if-statements-in-parameterisedfunctions-jl-is-there-a-work-around/79347 "2022-04-11T14:57:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BinaryBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/binarybot/32/45258_2.png) [@BinaryBot](https://discourse.julialang.org/u/BinaryBot)\
**Post date:** [April 11, 2022, 2:57pm UTC](https://discourse.julialang.org/t/can-i-use-if-statements-in-parameterisedfunctions-jl-is-there-a-work-around/79347/1 "2022-04-11T14:57:05Z")

</div>

I am trying to check weather my Differential Equation implemented using DifferentialEquation.jl matches the results obtained in Matlab. I learnt I could do this by implementing the same differential equation using MATLABDiffEq.jl and ParameterisedFunctions.jl. My code contains several if-statements that look something like this:

```julia
function diffeq(du,u,p,t)
    if u[1]>0
        du[1]= u[1]+10
    end    
end
u0=[0.0,1.0]
tspan= (0,1)
prob= ODEProblem(diffeq,u0,tspan,p)
sol= solve(prob, MATLABDiffEq.ode15s())

```

However, when I debug I notice that the u0 passed does not contain number but instead it is something like this.

 ![paramfunc](https://global.discourse-cdn.com/julialang/original/3X/3/4/34ad04c6a115b42cfb00bb1e0d02fdec756fba49.png)

I believe this is due to the metaprogramming capabilities of Julia. Because of which I get a type error in the if-statement

> Blockquote TypeError: non-boolean (Num) used in boolean context

How do I go about this and implement my quite complex diff equation as a parameterised function?

---

<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:** [April 11, 2022, 3:34pm UTC](https://discourse.julialang.org/t/can-i-use-if-statements-in-parameterisedfunctions-jl-is-there-a-work-around/79347/2 "2022-04-11T15:34:27Z")

</div>

Duplicate with a solution posted:

> [@Replicating Matlab ode15s results in Julia](https://discourse.julialang.org/t/replicating-matlab-ode15s-results-in-julia/79280/9):
>
> Does this work? using IfElse function diffeq(du,u,p,t) du[1] = IfElse.ifelse(u[1]\>0, u[1]+10, u[1]) end

---

<div class="post-metadata">

**Author:** ![shoumikdc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shoumikdc/32/50602_2.png) [@shoumikdc](https://discourse.julialang.org/u/shoumikdc)\
**Post date:** [June 7, 2023, 12:53pm UTC](https://discourse.julialang.org/t/can-i-use-if-statements-in-parameterisedfunctions-jl-is-there-a-work-around/79347/3 "2023-06-07T12:53:00Z")

</div>

Quick question on this topic @ChrisRackauckas. The posted solution above uses functionality from the [IfElse.jl package](https://github.com/SciML/IfElse.jl), though it seems like the package itself is archived? Is `IfElse` going to be deprecated?

In general, if I have an ODE problem that requires if statements in each update step depending on the state of the integrator, it seems one could either (i) use the `IfElse` functionality as listed above, or (ii) use a [`DiscreteCallback`](https://docs.sciml.ai/DiffEqDocs/stable/features/callback_functions/). Is there any reason to use one over the other in terms of performance?

---

<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:** [June 7, 2023, 12:57pm UTC](https://discourse.julialang.org/t/can-i-use-if-statements-in-parameterisedfunctions-jl-is-there-a-work-around/79347/4 "2023-06-07T12:57:32Z")

</div>

> [@shoumikdc](#):
>
> Quick question on this topic @ChrisRackauckas. The posted solution above uses functionality from the [IfElse.jl package](https://github.com/SciML/IfElse.jl), though it seems like the package itself is archived? Is `IfElse` going to be deprecated?

Yes, IIRC it’s Julia v1.7 that `ifelse` in Base Julia works just fine (the compiler was patched so that `Core.ifelse` is allowed to dispatch), so after that `IfElse.ifelse` is the same as Julia’s core `ifelse`. Therefore, just use `Base.ifelse` like you would in any other Julia code and it’ll work. IfElse.jl is just a relic of a past now.

> [@shoumikdc](#):
>
> In general, if I have an ODE problem that requires if statements in each update step depending on the state of the integrator, it seems one could either (i) use the `IfElse` functionality as listed above, or (ii) use a [`DiscreteCallback`](https://docs.sciml.ai/DiffEqDocs/stable/features/callback_functions/). Is there any reason to use one over the other in terms of performance?

If you have discontinuities you normally want to declare them with a callback in order to have better convergence. Technically the ODE solver is okay, because adaptivity will adjust dt around the discontinuity to be very small which will then help it hit a low amount of error, but of course that can impact performance.
