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

**URL:** <https://discourse.julialang.org/t/differentialequations-jl-plotting-multiple-components-with-vars-f-a-b-form/15799>\
**Category:** General Usage\
**Tags:** question, package, plotting\
**Created:** [October 2, 2018, 6:29pm UTC](https://discourse.julialang.org/t/differentialequations-jl-plotting-multiple-components-with-vars-f-a-b-form/15799 "2018-10-02T18:29:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 2, 2018, 6:29pm UTC](https://discourse.julialang.org/t/differentialequations-jl-plotting-multiple-components-with-vars-f-a-b-form/15799/1 "2018-10-02T18:29:48Z")

</div>

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

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

```julia
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](https://docs.juliadiffeq.org/latest/basics/plot.html#Choosing-Variables-1), 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?

---

<div class="post-metadata">

**Author:** ![anton](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@anton](https://discourse.julialang.org/u/anton)\
**Post date:** [October 2, 2018, 8:09pm UTC](https://discourse.julialang.org/t/differentialequations-jl-plotting-multiple-components-with-vars-f-a-b-form/15799/2 "2018-10-02T20:09:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 3, 2018, 12:22am UTC](https://discourse.julialang.org/t/differentialequations-jl-plotting-multiple-components-with-vars-f-a-b-form/15799/3 "2018-10-03T00:22:32Z")

</div>

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!

---

<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 3, 2018, 12:26am UTC](https://discourse.julialang.org/t/differentialequations-jl-plotting-multiple-components-with-vars-f-a-b-form/15799/4 "2018-10-03T00:26:22Z")

</div>

> [@tomerarnon](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [October 3, 2018, 6:09pm UTC](https://discourse.julialang.org/t/differentialequations-jl-plotting-multiple-components-with-vars-f-a-b-form/15799/5 "2018-10-03T18:09:50Z")

</div>

Thanks for the note @ChrisRackauckas. I’ve filed an issue [here](https://github.com/JuliaDiffEq/DifferentialEquations.jl/issues/359).
