# Question for shut down the simulation in DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 4, 2022, 2:08am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747 "2022-09-04T02:08:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Funkerfish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/funkerfish/32/37982_2.png) [@Funkerfish](https://discourse.julialang.org/u/Funkerfish)\
**Post date:** [September 4, 2022, 2:08am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747/1 "2022-09-04T02:08:08Z")

</div>

Hi everyone  
I read the Q&A document of DifferentialEquations.jl and find the following statement:

**if you see these warnings during a parameter estimation process, this is likely the underlying problem. Simply check `sol.retcode != :Success` and throw an `Inf` cost and most optimizers will reject steps in those parameter regimes!**  
[https://diffeq.sciml.ai/stable/basics/faq/#faq\_stability](https://diffeq.sciml.ai/stable/basics/faq/#faq_stability)

My question is : throw an Inf to what? Could someone please give me an example?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 4, 2022, 2:39am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747/2 "2022-09-04T02:39:17Z")

</div>

This means that your cost function to the optimizer should be something like

```julia
cost(prob, good_val)
    sol = solve(prob)
    if sol.retcode != :Success
        return Inf
    end
    return norm(sol.u .- good_value)
end
```

---

<div class="post-metadata">

**Author:** ![Funkerfish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/funkerfish/32/37982_2.png) [@Funkerfish](https://discourse.julialang.org/u/Funkerfish)\
**Post date:** [September 4, 2022, 2:45am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747/3 "2022-09-04T02:45:55Z")

</div>

Sorry… could you be more specific?  
What is the cost() function for? I didn’t search out any result in `help?` 。

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 4, 2022, 2:47am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747/4 "2022-09-04T02:47:17Z")

</div>

If you are doing any type of optimization process, you will likely have some function that you pass to the optimizer to tell it how good the solution is. That is what this `cost` function I gave represents.

---

<div class="post-metadata">

**Author:** ![Funkerfish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/funkerfish/32/37982_2.png) [@Funkerfish](https://discourse.julialang.org/u/Funkerfish)\
**Post date:** [September 4, 2022, 3:00am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747/5 "2022-09-04T03:00:25Z")

</div>

okey… I start to understand the `cost` function. but it doesn’t need to be a `cost()` right? like in a loop of parameter estimation process,

```julia
for i in (1:10)
sol = solve(prob[i])
    if sol.retcode != :Success
      continue
    end
end

```

Is that right?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 4, 2022, 3:01am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747/6 "2022-09-04T03:01:30Z")

</div>

right.

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [September 4, 2022, 3:05am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747/7 "2022-09-04T03:05:11Z")

</div>

But what indicates how well your model describes some existing data then? (Which is presumably why you do parameter estimation?)

---

<div class="post-metadata">

**Author:** ![Funkerfish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/funkerfish/32/37982_2.png) [@Funkerfish](https://discourse.julialang.org/u/Funkerfish)\
**Post date:** [September 4, 2022, 3:13am UTC](https://discourse.julialang.org/t/question-for-shut-down-the-simulation-in-differentialequations-jl/86747/8 "2022-09-04T03:13:53Z")

</div>

Actually, it is the official document mentioned the parameter estimation. I just meet a similar problem in calculating the Lyapunov spectrum, which needs a parameter to be changed in a certain range.
