# Subsampling an ODESolution at Integer Times (and then plotting)

**URL:** <https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440>\
**Category:** New to Julia\
**Tags:** diffeq\
**Created:** [October 15, 2020, 4:38pm UTC](https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440 "2020-10-15T16:38:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sam_power/32/18972_2.png) [@Sam\_Power](https://discourse.julialang.org/u/Sam_Power)\
**Post date:** [October 15, 2020, 4:38pm UTC](https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440/1 "2020-10-15T16:38:32Z")

</div>

Hi,

I’m currently playing around with DifferentialEquations.jl and Catalyst.jl for simulating the paths of Markov Jump Processes, with an end goal of eventually using it to prototype some Bayesian inference algorithms.

Having followed the documentation [here](https://diffeq.sciml.ai/stable/tutorials/discrete_stochastic_example/), I’m able to simulate paths of my MJP of interest, and plot their solution with Plots.jl, as below.

```julia
using Catalyst, DifferentialEquations

lotvol_model = @reaction_network begin
    c1, x1 --> 2x1
    c2, x1 + x2 --> 2x2
    c3, x2 --> 0
end c1 c2 c3

rates = (1.0, 0.005, 0.6)
prob = DiscreteProblem(lotvol_model, [70, 80], (0.0, 50.0), rates)
jump_prob = JumpProblem(lotvol_model, prob, Direct())

sol = solve(jump_prob, SSAStepper())
using Plots; plot(sol)

```

and everything is looking great so far.

What I’d like to do next is take my solution, subsample it at integer times, and be able to plot that in the same format (as this is the sort of data upon which I will eventually be running inference algorithms).

Thus far, I’ve tried both

```julia
sol_int_times = [sol(t) for t in 1:50]

```

and

```julia
sol_int_times = [(t, sol(t)) for t in 1:50]

```

With the former attempt, I can plot it, but it forgets the temporal structure of the solution, and so is not really what I’m looking for. The issue with the latter attempt is that it won’t plot at all. Upon inspecting `sol_int_times`, the time and position components seem to be nested unevenly, or something like this - I don’t completely understand, but clearly I’m transforming things wrongly somehow.

Anyways, I would like to be able to subsample the solution to the MJP at the specified times, and have it stored in a format which will plot cleanly (and hopefully, be easy to work with downstream). What’s the correct syntax for doing so?

---

<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:** [October 15, 2020, 4:48pm UTC](https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440/2 "2020-10-15T16:48:49Z")

</div>

`sol = solve(jump_prob, SSAStepper(), saveat=1.0)`

---

<div class="post-metadata">

**Author:** ![Sam\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sam_power/32/18972_2.png) [@Sam\_Power](https://discourse.julialang.org/u/Sam_Power)\
**Post date:** [October 15, 2020, 4:51pm UTC](https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440/3 "2020-10-15T16:51:47Z")

</div>

Great, thanks. Am I correct in understanding that this re-runs the solver from scratch? If so, is there a way of extracting the original path without re-solving?

---

<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:** [October 15, 2020, 4:55pm UTC](https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440/4 "2020-10-15T16:55:46Z")

</div>

If you solve like this it solves once and saves at the integers. Isn’t that what you want?

---

<div class="post-metadata">

**Author:** ![Sam\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sam_power/32/18972_2.png) [@Sam\_Power](https://discourse.julialang.org/u/Sam_Power)\
**Post date:** [October 15, 2020, 4:59pm UTC](https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440/5 "2020-10-15T16:59:06Z")

</div>

I think it ought to be fine for the most part — I was thinking that, further down the track, it might be useful to be able to subsample the solution more generally, if manipulating the solutions in this way is not too messy. It’s not super important though, I think that I should be able to do what I want with this.

---

<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:** [October 15, 2020, 5:05pm UTC](https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440/6 "2020-10-15T17:05:24Z")

</div>

I mean, from the way you solved it you could do `sol(1:50)`.

---

<div class="post-metadata">

**Author:** ![Sam\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sam_power/32/18972_2.png) [@Sam\_Power](https://discourse.julialang.org/u/Sam_Power)\
**Post date:** [October 15, 2020, 5:07pm UTC](https://discourse.julialang.org/t/subsampling-an-odesolution-at-integer-times-and-then-plotting/48440/7 "2020-10-15T17:07:58Z")

</div>

Perfect - thank you!
