# Access both solutions of a SecondOrderODEProblem solution

**URL:** https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291
**Category:** Modelling & Simulations
**Tags:** diffeq
**Created:** [May 19, 2022, 12:12am UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291 "2022-05-19T00:12:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![GHRALIMA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghralima/32/19244_2.png) [@GHRALIMA](https://discourse.julialang.org/u/GHRALIMA)
#### Post date: [May 19, 2022, 12:12am UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291/1 "2022-05-19T00:12:47Z")

</div>

I’m trying to use DifferentialEquations.jl to solve simple mechanical problems with SecondOrderODEProblem, something like:

```julia
k=0.01
v0 = 10.0
x0 = 10.0
tspan = (0.0, 100.0) 
ax1(v,u,p,t) = -k*v - g
prob = SecondOrderODEProblem(ax1, v0, x0, tspan)
sol = solve(prob, Nystrom4(), reltol=1e-8, abstol=1e-8, dt = 0.01)

```

It works perfectly. However, I’m a bit lost. I’m trying to find the zeros of the solution with Roots.jl, and when I use, for example:

```julia
find_zero(sol, (1, 100))

```

the function returns the zero of the solution’s first derivative instead of the zero of the solution. Is there any way to apply find\_zero to find the root of the solution in some interval?

---

<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: [May 19, 2022, 10:37am UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291/2 "2022-05-19T10:37:16Z")

</div>

> [@GHRALIMA](#):
>
> Is there any way to apply find\_zero to find the root of the solution in some interval?

If you use `save_idxs` in the solve, you can choose which indices to save, like `save_idxs = 2` then gives you only the second index of the solve, which IIRC is the position one (double check).

---

<div class="post-metadata">

### Author: ![GHRALIMA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghralima/32/19244_2.png) [@GHRALIMA](https://discourse.julialang.org/u/GHRALIMA)
#### Post date: [May 19, 2022, 12:16pm UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291/3 "2022-05-19T12:16:46Z")

</div>

Thanks! It works! But in this case, if I want to work with both solutions separately I will have to solve the problem twice. Is that so, or I’m missing something?

---

<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: [May 19, 2022, 12:21pm UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291/4 "2022-05-19T12:21:00Z")

</div>

you can do `t -> sol(t;idxs = 2)` to rootfind just over the second index.

---

<div class="post-metadata">

### Author: ![GHRALIMA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghralima/32/19244_2.png) [@GHRALIMA](https://discourse.julialang.org/u/GHRALIMA)
#### Post date: [May 19, 2022, 12:31pm UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291/5 "2022-05-19T12:31:47Z")

</div>

Thanks! That’s what I needed! =D

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [May 19, 2022, 2:04pm UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291/6 "2022-05-19T14:04:36Z")

</div>

@ChrisRackauckas to be honest, looking at the docs I also didn’t see anywhere on a quick glance that explained the ordering of variables in the solution object for a `SecondOrderProblem`. (I think this is also an issue for dynamical and Hamiltonian problems too.)

If you think it is not making the tutorial too long, I could update the very first tutorial’s example 3 (the pendulum) with a short second version that is equivalent but uses `SecondOrderProblem`, and then shows briefly what the solution components correspond to.

Alternatively, it might be worth adding a new tutorial in the main docs that covers the dynamical, Hamiltonian and 2nd order problem types, showing how to use their solutions. Maybe this would make sense as the second tutorial, before the code optimization one.

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [May 19, 2022, 2:10pm UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291/7 "2022-05-19T14:10:22Z")

</div>

Though maybe this should just be documented in each of the problem types with an example and/or note.

---

<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: [May 19, 2022, 2:10pm UTC](https://discourse.julialang.org/t/access-both-solutions-of-a-secondorderodeproblem-solution/81291/8 "2022-05-19T14:10:43Z")

</div>

> [@isaacsas](#):
>
> If you think it is not making the tutorial too long, I could update the very first tutorial’s example 3 (the pendulum) with a short second version that is equivalent but uses `SecondOrderProblem` , and then shows briefly what the solution components correspond to.
> 
> Alternatively, it might be worth adding a new tutorial in the main docs that covers the dynamical, Hamiltonian and 2nd order problem types, showing how to use their solutions. Maybe this would make sense as the second tutorial, before the code optimization one.

We don’t have a tutorial on SecondOrderODEProblem, so adding one is probably a good place to start. I would place it after the code optimization one, since growing standard ODEs is by far the #1 use case of the package. So all of the ODEs, handling PDE semi-discretizations, and stiff ODE stuff comes first, and then everything else.

But yes, we should document this in the dynamical problems section in more detail too.
