# Running simulations over range of parameters

**URL:** https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573
**Category:** General Usage
**Created:** [March 5, 2020, 12:22pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573 "2020-03-05T12:22:42Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [March 5, 2020, 12:22pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/1 "2020-03-05T12:22:42Z")

</div>

Hi,  
I am running a two differential equations over a range of parameters, and it works fine. I loop over two parameters (double loop). I would like to know the Julia way of doing this. Here is my code.

> # Run all α, β pairs
> 
> println(αs)  
> sols = Any  
> for α ∈ αs  
> for β ∈ βs  
> p1 = [α, β, 8., 5.] # α, β, D1, D2  
> println("α, β= ", α, β)  
> prob = ODEProblem(coupled!, u0, tspan, p1)  
> sol = solve(prob, Tsit5())  
> println("sol: ", size(sol))  
> append!(sols, [sol]) # \<\<\< NOTICE  
> end  
> end  
> plot([sols[1]])

Notice the use of “append!”. “sol” is the output to “solve”, and I wish to concatenate the solutions into a Vector of four elements. To do this and properly access the variables, I had to enclose “sol” with brackets. If I do not do this, size(sols) returns 84 instead of 4. I do not quite understand why.

Next step, DiffEqFlux perhaps, which is my ultimate goal for now.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 5, 2020, 12:38pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/2 "2020-03-05T12:38:30Z")

</div>

Please quote your code and provide an MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [March 5, 2020, 2:15pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/3 "2020-03-05T14:15:31Z")

</div>

You can use the function `map` and map over the two ranges. This would automatically produce the output array for you.

```julia
map(as, bs) do a, b
   Body of for loop
end 

```

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [March 5, 2020, 2:36pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/4 "2020-03-05T14:36:04Z")

</div>

Thank you! Looks much nicer.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [March 5, 2020, 2:36pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/5 "2020-03-05T14:36:53Z")

</div>

I will read the link. I did not think a MWE was necessary in this case since the code worked properly, and I was only asking for an alternative to the section I posted.  
But I will do so next time.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [March 5, 2020, 2:38pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/6 "2020-03-05T14:38:00Z")

</div>

By the way, inserting an element at the end of a vector is called `push! `. Append is for appending a vector to another.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [March 5, 2020, 2:44pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/7 "2020-03-05T14:44:54Z")

</div>

The map solution does not work. If as=[1,2] and bs=[3,4], I expect to iterate over the combinations (1,3), (1,4), (2,3), (2,4). Your approach loops over (1,3) and (2,4).

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [March 5, 2020, 3:39pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/8 "2020-03-05T15:39:11Z")

</div>

Then you want `Iterators.product(v1, v2)` or perhaps `CartesianIndices((v1, v2))` if both `v1` and `v2` are ranges.

---

<div class="post-metadata">

### Author: ![sgjanssens](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sgjanssens](https://discourse.julialang.org/u/sgjanssens)
#### Post date: [March 5, 2020, 3:45pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/9 "2020-03-05T15:45:53Z")

</div>

Probably the OP has already considered this himself, and it also depends a bit on his particular purpose, but instead of creating simulations over a parameter grid, it may be more elegant and provide more insight to use a numerical continuation package.

(This is particularly true if you are interested in stability and destabilization of simple invariants such as stationary and periodic solutions.)

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [March 5, 2020, 4:02pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/10 "2020-03-05T16:02:28Z")

</div>

Right now, I am learning Julia, but I would be interested in a continuation package to study a system of reaction/diffusion equations. Could you point me to the latest stable resources? Thanks.

---

<div class="post-metadata">

### Author: ![sgjanssens](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sgjanssens](https://discourse.julialang.org/u/sgjanssens)
#### Post date: [March 5, 2020, 4:21pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/11 "2020-03-05T16:21:10Z")

</div>

Although it overlaps with packages that I am familiar with (AUTO, MATCONT) or involved with (DDE-BIFTOOL), I think it is only fair to point you to [PseudoArcLengthContinuation.jl](https://github.com/rveltz/PseudoArcLengthContinuation.jl) for a project by @rveltz specifically in Julia. Hope, that helps, and enjoy the learning.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [March 5, 2020, 7:40pm UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/12 "2020-03-05T19:40:20Z")

</div>

I indeed missed that map is elementwise by default. Note the additional requirement to destructure the argument tuple when using `product`

```julia
julia> map(Iterators.product(1:2, 3:4)) do (a,b)
                  (a,b)
       end
2×2 Array{Tuple{Int64,Int64},2}:
 (1, 3) (1, 4)
 (2, 3) (2, 4)

```

Notice the parenthesis in `map(...) do (a,b)`

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [March 6, 2020, 10:12am UTC](https://discourse.julialang.org/t/running-simulations-over-range-of-parameters/35573/13 "2020-03-06T10:12:31Z")

</div>

You also have [Bifurcations.jl](https://github.com/tkf/Bifurcations.jl) for “small” systems
