# How to sum outputs from DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034>\
**Category:** General Usage\
**Tags:** plotting, diffeq\
**Created:** [August 26, 2021, 12:53pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034 "2021-08-26T12:53:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgp/32/28599_2.png) [@mgp](https://discourse.julialang.org/u/mgp)\
**Post date:** [August 26, 2021, 12:53pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034/1 "2021-08-26T12:53:44Z")

</div>

I am making a series of compartmental ODE infection models using DifferentialEquations. I would like to be able to plot the sum of two or more compartments (e.g. I might have people with undiagnosed infection and diagnosed infection, and want to plot the total number with infection).

A simple example is that I’d like to sum the entire population each time I change the model to check that I haven’t made a mistake that causes the population size to change.

I’ve put a simple example below that is very unattractive. I can work at changing `saveat` to make the plot smoother, but I’m hoping there’s something within DifferentialEquations that I’ve missed that will perform this more elegantly. Thanks.

```julia
using DifferentialEquations, Plots, StaticArrays

function sir(u, p, t)
    S, I, R = u 
    β, γ = p
    
    N = S + I + R 

    dS = -β * S * I / N 
    dI = β * S * I / N - γ * I 
    dR = γ * I

    return @SVector [dS, dI, dR]
end 

u₀ = @SVector [.999, .001, 0]
p = [.3, .1]
tspan = (0., 100.)

prob = ODEProblem(sir, u₀, tspan, p)
sol = solve(prob)

function plot_total_size(solution)
    y::Array = sum.(solution.u')'
    x = solution.t
    plot(x, y)
end 

plot_total_size(sol)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/d/6/d68622803663676eb63c8dedd0f851b19f4b14a2.png)

---

<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:** [August 26, 2021, 1:59pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034/2 "2021-08-26T13:59:02Z")

</div>

sum(sol,dims=2)

---

<div class="post-metadata">

**Author:** ![mgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgp/32/28599_2.png) [@mgp](https://discourse.julialang.org/u/mgp)\
**Post date:** [August 26, 2021, 2:07pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034/3 "2021-08-26T14:07:41Z")

</div>

Thank you. I think that’s summing within the compartments across all time but `sum(sol,dims=1)` looks like what I want – across compartments at each time point.

It still only plots at the discrete time points shown, but I think that if I make `saveat` small then it will look smooth.

---

<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:** [August 26, 2021, 2:11pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034/4 "2021-08-26T14:11:28Z")

</div>

You can also add a function to the plotter

---

<div class="post-metadata">

**Author:** ![mgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgp/32/28599_2.png) [@mgp](https://discourse.julialang.org/u/mgp)\
**Post date:** [August 26, 2021, 4:04pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034/5 "2021-08-26T16:04:32Z")

</div>

`plot(sum(sol, dims = 1))` doesn’t work for me, but so long as I don’t want all the compartments summed, I get good results with `plot(sol.t, (@. sol[2,:] + sol[3,:]))`, which is, in any case, more like the plots I will want to produce. Thank you

---

<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:** [August 26, 2021, 4:07pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034/6 "2021-08-26T16:07:00Z")

</div>

Look at the documentation:

[https://diffeq.sciml.ai/stable/basics/plot/](https://diffeq.sciml.ai/stable/basics/plot/)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 26, 2021, 5:12pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034/7 "2021-08-26T17:12:37Z")

</div>

> [@ChrisRackauckas](#):
>
> You can also add a function to the plotter

i.e. something like `plot(sol, vars=((t,s1,s2) -> (t,s1+s2), 0,1,2))`, I guess?

---

<div class="post-metadata">

**Author:** ![mgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgp/32/28599_2.png) [@mgp](https://discourse.julialang.org/u/mgp)\
**Post date:** [August 26, 2021, 5:31pm UTC](https://discourse.julialang.org/t/how-to-sum-outputs-from-differentialequations-jl/67034/8 "2021-08-26T17:31:00Z")

</div>

Thank you both - that works perfectly.
