Access both solutions of a SecondOrderODEProblem solution

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

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:

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?

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).

1 Like

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?

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

1 Like

Thanks! That’s what I needed! =D

@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.

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

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.

1 Like