DifferentialEquations.jl plotting multiple components with vars = [(f, a, b) ] form

First off, I should say I am very new to using DifferentialEquations, but I have not seen this question addressed anywhere else :slight_smile: .

I am trying to plot the solution to a second order ODE, particularly the sum of two components of the solution. I can extract those components with sol'[...] and plot it that way, but I was hoping there was a way using the plotting recipe.

Attempting plot(sol, vars = [(+, 1, 2)] yields the following error (on v0.6.2):

ERROR: MethodError: no method matching eachindex(::Float64)
Closest candidates are:
  eachindex(::Cmd) at /Users/tomer/.julia/v0.6/Compat/src/Compat.jl:363
  eachindex(::IndexLinear, ::AbstractArray) at abstractarray.jl:820
  eachindex(::IndexLinear, ::AbstractArray, ::AbstractArray...) at abstractarray.jl:822
  ...

I assumed this would work based on the plotting section of the docs, which says that (f, a, b) should work. The error seems to have something to do with the fact that + is being called in way that is attempting to index a and b incorrectly(?) Is there a particular form that f should take in order to work in this case, e.g. does it need to be broadcast somehow?

Hello tomerarnon! I never used this feature of DifferentialEquations before but judging from docs you linked:

Functions f1 , f2 and f3 should take in scalars and return a tuple.

and + returns a single value.
Just tried it on Julia 1.0.0. I have exactly same result for f(a,b) = a+b and just + (looks like both display some nonsense but it doesn’t crash). To get actual sum I had to use f(a,b) = a, a+b.
I’m not an experienced Julia user yet but I hope it will help you a bit.

1 Like

Thanks for the reply Anton. (a, a+b) results in a plot of a vs a+b, while I was looking for a plot of a+b vs time. Inspired by your suggestion, however, I tried f(t, a, b) = (t, a+b) which worked! Applied as in: plot(sol, vars = [(f, 0, 3, 4)])

I also tried f(a, b, t) = (t, a + b), with the input argument in a different order (called with (f, 3,4,0)), which for some reason did not work. I thought the results should be the same for the two but inexplicably they are not.

Anyway, thanks for your help!

I’m not sure why that wouldn’t work. That’s worth filing an issue.

Thanks for the note @ChrisRackauckas. I’ve filed an issue here.

1 Like