# Looking for findfirst or similar for sol objects of DifferentialEquations

**URL:** https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134
**Category:** Modelling & Simulations
**Created:** [April 7, 2020, 12:08am UTC](https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134 "2020-04-07T00:08:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 7, 2020, 12:08am UTC](https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134/1 "2020-04-07T00:08:18Z")

</div>

Hello!

I’m doing some control systems work and wanting to find things like risetime of responses, so I want to find where a simulation variable crosses some value. `findfirst` works but it searches among the simulation points rather than finding where the interpolation crosses the desired point, which if things are smooth might be some distance from the point I would like to find.

I suspect someone has crossed this bridge already, but I’m struggling to find it.

I could do it using callbacks, but that seems a bit overkill for what feels more natural to be done with analysis after the simulation has been run.

Thanks for any pointers.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [April 7, 2020, 12:56am UTC](https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134/2 "2020-04-07T00:56:59Z")

</div>

This is a root-finding problem – try Roots.jl maybe? (Untested)

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [April 7, 2020, 12:57am UTC](https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134/3 "2020-04-07T00:57:24Z")

</div>

Note that you can call the DiffEq solution object as if it were a function.

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 7, 2020, 1:49am UTC](https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134/4 "2020-04-07T01:49:39Z")

</div>

That would certainly work. I was just trying to find out if someone had done the work before me.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [April 7, 2020, 2:02am UTC](https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134/5 "2020-04-07T02:02:15Z")

</div>

There’s no work to do, you just call the relevant function from Roots.jl (I believe).

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [April 7, 2020, 2:10am UTC](https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134/6 "2020-04-07T02:10:55Z")

</div>

@klaff Here’s an example using the first code from the DiffEq docs:

```julia
using DifferentialEquations
f(u, p, t) = 1.01 * u
u0 = 1 / 2
tspan = (0.0, 1.0)
prob = ODEProblem(f, u0, tspan)
sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)

julia> using Roots

julia> initial = 3.0;

julia> desired = 5.0;

julia> root = fzero(x -> sol(x) - desired, initial)
2.2906833822317667

julia> sol(root)
5.000000014083912

```

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [April 7, 2020, 6:21pm UTC](https://discourse.julialang.org/t/looking-for-findfirst-or-similar-for-sol-objects-of-differentialequations/37134/7 "2020-04-07T18:21:52Z")

</div>

Thanks, this works nicely.

```julia
"find 10-90 risetime of sol in interval ti to tf, 100% step from yi to yf; n(th) state var"
function findTr(sol, ti, tf, yi, yf, n=1)
    v10,v90 = [0.1,0.9].*(yf-yi).+yi
    t10 = fzero(t -> sol(t)[n] - v10, [ti,tf])
    t90 = fzero(t -> sol(t)[n] - v90, [ti,tf])
    return t90-t10
end

```
