# DifferentialEquations.jl inputting out of domain value

**URL:** <https://discourse.julialang.org/t/differentialequations-jl-inputting-out-of-domain-value/70501>\
**Category:** General Usage\
**Tags:** package, diffeq, differentialequation\
**Created:** [October 27, 2021, 7:01pm UTC](https://discourse.julialang.org/t/differentialequations-jl-inputting-out-of-domain-value/70501 "2021-10-27T19:01:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hellmrf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hellmrf/32/21834_2.png) [@hellmrf](https://discourse.julialang.org/u/hellmrf)\
**Post date:** [October 27, 2021, 7:01pm UTC](https://discourse.julialang.org/t/differentialequations-jl-inputting-out-of-domain-value/70501/1 "2021-10-27T19:01:23Z")

</div>

Hello everyone.

I’m trying to solve a differential equation (actually it’s part of an optimization process, so it’s solved each optimization iteration). The problem is: my u is a parameter that has a physical meaning of extent, so it can only be between 0 and 1. However, sometimes the solver tries to use `u < 0.0` or `y > 1.0`, which doesn’t make (physical) sense and causes the resulting value to be complex.  
I followed the advice of saturating the value with `u = max(min(u, 1.0), 0.0)`, but when I do this, the next iteration comes with `u = Inf`.

I wrote a minimum (but complete) example showing this behavior. The hardcoded values come from the optimizer.  
The code is [here](https://gist.github.com/hellmrf/d2fa1377af5c8a91abb1916c3f52ba52).

What am I missing? How can I tell OrdinaryDiffEq.jl the domain of my function?  
Thank you.

---

<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:** [October 29, 2021, 2:26pm UTC](https://discourse.julialang.org/t/differentialequations-jl-inputting-out-of-domain-value/70501/2 "2021-10-29T14:26:43Z")

</div>

> [@hellmrf](#):
>
> How can I tell OrdinaryDiffEq.jl the domain of my function?

There has a lot been written on this, so you might want to just read the docs on it.

[https://diffeq.sciml.ai/stable/basics/common\_solver\_opts/#Miscellaneous](https://diffeq.sciml.ai/stable/basics/common_solver_opts/#Miscellaneous)

> - `isoutofdomain` : Specifies a function `isoutofdomain(u,p,t)` where, when it returns true, it will reject the timestep. Disabled by default.

[https://diffeq.sciml.ai/stable/features/callback\_library/#PositiveDomain](https://diffeq.sciml.ai/stable/features/callback_library/#PositiveDomain)

If these “fail”, see

[https://diffeq.sciml.ai/stable/basics/faq](https://diffeq.sciml.ai/stable/basics/faq) see " [My ODE goes negative but should stay positive, what tools can help?](https://diffeq.sciml.ai/stable/basics/faq/#My-ODE-goes-negative-but-should-stay-positive,-what-tools-can-help?)".

Also,

> [@PSA: How to help yourself debug differential equation solving issues](https://discourse.julialang.org/t/psa-how-to-help-yourself-debug-differential-equation-solving-issues/62489):
>
> Debugging differential equation solver issues almost always boils down to doing the same thing, so this is a summary to help you out. For more information on specific issues, check out the FAQ section of the DifferentialEquations.jl documentation which highlights common issues and questions: [https://diffeq.sciml.ai/dev/basics/faq/](https://diffeq.sciml.ai/dev/basics/faq/)How do I debug why the differential equation solver is diverging? dt \<= dtmin. Aborting. There is either an error in your model specification or the true solution i…
