# DifferentialEquations 2nd order ODE interpolation for position

**URL:** <https://discourse.julialang.org/t/differentialequations-2nd-order-ode-interpolation-for-position/105409>\
**Category:** Modelling & Simulations\
**Tags:** diffeq, differentialequation\
**Created:** [October 26, 2023, 1:51am UTC](https://discourse.julialang.org/t/differentialequations-2nd-order-ode-interpolation-for-position/105409 "2023-10-26T01:51:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwalters31](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rwalters31/32/25594_2.png) [@rwalters31](https://discourse.julialang.org/u/rwalters31)\
**Post date:** [October 26, 2023, 1:51am UTC](https://discourse.julialang.org/t/differentialequations-2nd-order-ode-interpolation-for-position/105409/1 "2023-10-26T01:51:43Z")

</div>

I am modeling simple projectile motion using the DifferentialEquations.jl package. I have read in the documentation that you can interpolate based on time to get an answer for an exact time.

Instead of time I have been looking for a way to interpolate based on position. For example I want the vertical position of the projectile at evenly spaced horizontal increments. Is this functionality already in the DifferentialEquations package or do I need to use a package like CurveFit.jl on the horizontal and vertical positions to approximate a solution at evenly spaced horizontal intervals? Thanks.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [October 26, 2023, 6:34am UTC](https://discourse.julialang.org/t/differentialequations-2nd-order-ode-interpolation-for-position/105409/2 "2023-10-26T06:34:10Z")

</div>

I think what you want can be achieved easily with one of the interpolation packages, e.g. [`Interpolations.jl`](https://juliamath.github.io/Interpolations.jl/latest/)  
Then you do something like:

```julia
using Interpolations
...
sol = solve(problem, solver) # solve the ODE

# interpolate at fixed time
tfix = 0.5
smoothfunction_fixed_t = linear_interpolation(xgrid, sol(tfix))
# can now do smoothfunction_fixed_t(x)

# interpolate everything
tgrid = range(0,sol.t[end]; length=100)
smoothfunction = linear_interpolation((xgrid, tgrid), mapreduce(sol, hcat, tgrid))
# can now do smoothfunction(x,t)

```

---

<div class="post-metadata">

**Author:** ![johnb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnb/32/44115_2.png) [@johnb](https://discourse.julialang.org/u/johnb)\
**Post date:** [October 28, 2023, 9:38am UTC](https://discourse.julialang.org/t/differentialequations-2nd-order-ode-interpolation-for-position/105409/3 "2023-10-28T09:38:28Z")

</div>

Edit:sorry, misunderstood the problem. Ignore me

> **old response**
>
> You can use the solver option `saveat=your_stepsize` to do this at solve if you know your stepsize in advance.
> 
> [Common Solver Options (Solve Keyword Arguments) · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/basics/common_solver_opts/)
> 
> I think you can also use the built in interpolations by calling the solution object as `sol([t_start:step:t_end])`
> 
> [Solution Handling · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#Interpolations-and-Calculating-Derivatives)
