Running simulations over range of parameters

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

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

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.

Please quote your code and provide an MWE.

1 Like

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

map(as, bs) do a, b
   Body of for loop
1 Like

Thank you! Looks much nicer.

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.

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

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

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

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


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.

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 for a project by @rveltz specifically in Julia. Hope, that helps, and enjoy the learning.

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

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

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

You also have Bifurcations.jl for “small” systems