# Solving and ODE system starting from different initial points

**URL:** https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480
**Category:** New to Julia
**Created:** [March 3, 2020, 7:18pm UTC](https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480 "2020-03-03T19:18:25Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)
#### Post date: [March 3, 2020, 7:18pm UTC](https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480/1 "2020-03-03T19:18:25Z")

</div>

Hello,

I have a simple system of which i would like to build the phase portrait.  
I thought of creating a mesh of interesting points and solve the problem for each of them.  
This is what i naively tried until now but it does not work.

```julia
xs = -0.5:0.1:1.5
ys = -0.5:0.1:3
u0s = [[x,y] for x in xs, y in ys]
tspan = (0.0, 200.0)
λ = 0
prob = ODEProblem(f, u0s, tspan, λ)
sol = solve(prob, progress=true)

```

I could not find anything specific in the documentation on how to approach this problem, i know about `remake` but i am not sure if it is useful or a good idea to use it with so many new initialisations.

Could a for loop work where every time i store the solution for every initial point?  
something on the likes of

```julia
for u0 in u0s
    sol = solve(remake(prob; u0=u0))
end

```

and then store sol somewhere for later access and/or plotting.  
(in this case, how do i store the solutions correctly?)

Any smarter ideas, this feels really dumb.

Thanks!

EDIT: added the fix in the second answer

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [March 3, 2020, 7:56pm UTC](https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480/2 "2020-03-03T19:56:02Z")

</div>

Two possibilities:

- Shouldn’t `for u0 in eachindex(u0s)` instead be `for u0 in u0s`?
- The `remake` documentation suggests you should use keyword assignment; this may be unnecessary, though?? Thus `sol = solve(remake(prob; u0 = u0))`

---

<div class="post-metadata">

### Author: ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)
#### Post date: [March 3, 2020, 8:01pm UTC](https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480/3 "2020-03-03T20:01:44Z")

</div>

You are right about both points, i fixed the code and now at least runs.  
But now if i try to do

```julia
plot!(sol)

```

with the new computed solution it does not get updated.

Anyhow, i was mainly wondering if the ODEProblem interface accepted some kind of “multiple initial point” kind of thing, the current approach feels rather hacky.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [March 3, 2020, 8:41pm UTC](https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480/4 "2020-03-03T20:41:38Z")

</div>

The following works – I’ve used a boring pendulum model, since you don’t give your model:

```julia
function pendulum(du,u,p,t)
    g = 9.81
    L = 2
    alpha = u[1]
    omega = u[2]
    du[1] = omega
    du[2] = -g/L*sin(alpha)
end
#
as = -2pi:pi/6:2pi
ws = -0.5:0.1:0.5
us = [[a,w] for a in as, w in ws]
tspan = (0,5.)
#
prob = ODEProblem(pendulum,us[1],tspan)
sol = solve(prob)
plot(sol,vars=(1,2),label="")
#
for u in us
    sol = solve(remake(prob,u0 = u))
    plot!(sol,vars=(1,2),label="")
end
#
plot!()

```

The result is as follows (not very interesting…):

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

---

<div class="post-metadata">

### Author: ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)
#### Post date: [March 3, 2020, 8:59pm UTC](https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480/5 "2020-03-03T20:59:40Z")

</div>

Thanks a bunch!  
I had tried before without the final `plot!()` and it did not work since i forgot the `display` inside the for loop!

---

<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: [March 4, 2020, 12:51pm UTC](https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480/6 "2020-03-04T12:51:26Z")

</div>

> [@cshen](#):
>
> Anyhow, i was mainly wondering if the ODEProblem interface accepted some kind of “multiple initial point” kind of thing, the current approach feels rather hacky.

[https://docs.juliadiffeq.org/latest/features/ensemble/](https://docs.juliadiffeq.org/latest/features/ensemble/) is for that.

---

<div class="post-metadata">

### Author: ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)
#### Post date: [March 4, 2020, 3:35pm UTC](https://discourse.julialang.org/t/solving-and-ode-system-starting-from-different-initial-points/35480/7 "2020-03-04T15:35:20Z")

</div>

I had trouble finding the right terminology in the documentations, thanks for pointing me to it!
