# Basins of attractros for 6D system DynamicalSystems.jl

**URL:** https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781
**Category:** Modelling & Simulations
**Created:** [August 15, 2022, 2:21pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781 "2022-08-15T14:21:17Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 15, 2022, 2:21pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/1 "2022-08-15T14:21:17Z")

</div>

I trye found basins for attractors, but i have some problems.  
First problem is out of memmory when i use big length for  
` xg1 = yg1 = zg1 = xg2 = yg2 = zg2 = range(-6.0, 6.0; length = 10)`  
If i using for example `length = 100` i have error out of memory. Can i fix this or i need using poincare map?

Second problem is projection basins and attractors on 3D. I think I can do a projection of attractors on 3D, but I have a strange result  
Image:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/9/497f148e6a4de3f6a37e29bfbe70d3e8c9e41fd3.png)  
For this i using this code:

```julia
xg1 = yg1 = zg1 = xg2 = yg2 = zg2 = range(-6.0, 6.0; length = 10)
mapper = AttractorsViaRecurrences(ds_HR, (xg1, yg1, zg1, xg2, yg2, zg2))
basins, attractors = basins_of_attraction(mapper; show_progress = true)

function scatter_attractors!(ax, attractors)
    
    for k ∈ keys(attractors)
        x1, y1, z1, x2, y2, z2 = columns(attractors[k])
        scatter!(ax, x1, y1, z1;
            color = Cycled(k),
            strokewidth = 3, strokecolor = :white
        )
    end
end

fig = Figure()

ax = fig[1,1] = Axis3(fig; title = "found attractors")

scatter_attractors!(ax, attractors)

fig

```

Also i don’t understand how need modification variable `basins` for projection on 3D.

Full code:

```julia
function sigma(x)
    return @fastmath 1.0 / ( 1.0 + exp( -10.0 * ( x - ( - 0.25 ) ) ) )
end

function HR(u, p, t)
        
    a, b, c, d, s, xr, r, I, vs, k1, k2, el_link = p
    x1, y1, z1, x2, y2, z2 = u
    
    du1 = y1 + b * x1 ^ 2 - a * x1 ^3 - z1 + I - k1 * ( x1 - vs ) * sigma(x2) + el_link * ( x2 - x1 )
    du2 = c - d * x1 ^2 - y1
    du3 = r * ( s * ( x1 - xr ) - z1 )
    
    du4 = y2 + b * x2 ^ 2 - a * x2 ^3 - z2 + I - k2 * ( x2 - vs ) * sigma(x1) + el_link * ( x1 - x2 )
    du5 = c - d * x2 ^2 - y2
    du6 = r * ( s * ( x2 - xr ) - z2 )
    return SVector(du1, du2, du3,
                    du4, du5, du6)
end

a = 1.; b = 3.; c = 1.; d = 5.;
xr = -1.6; r = 0.01; s = 5.; I = 4.; xv = 2.;
k1= -0.17; k2 = -0.17;
k = 1.0
condition = SA[-1.5, 0.0, 0.0, -2.5, 0.0, 0.0]
# condition = SA[-0.5, -0.5, -0.5, -0.5, 0-0.5, -0.5]

p = SA[a, b, c, d,
        s, xr, r, I, xv, k1, k2, k]
p = [a, b, c, d, s, xr, r, I, xv, k1, k2, k]

ds_HR = ContinuousDynamicalSystem(HR, condition, p )

xg1 = yg1 = zg1 = xg2 = yg2 = zg2 = range(-6.0, 6.0; length = 10)
mapper = AttractorsViaRecurrences(ds_HR, (xg1, yg1, zg1, xg2, yg2, zg2))
basins, attractors = basins_of_attraction(mapper; show_progress = true)

function scatter_attractors!(ax, attractors)
    
    for k ∈ keys(attractors)
        x1, y1, z1, x2, y2, z2 = columns(attractors[k])
        scatter!(ax, x1, y1, z1;
            color = Cycled(k),
            strokewidth = 3, strokecolor = :white
        )
    end
end

fig = Figure()

ax = fig[1,1] = Axis3(fig; title = "found attractors")

scatter_attractors!(ax, attractors)

fig

```

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 15, 2022, 2:29pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/2 "2022-08-15T14:29:03Z")

</div>

If i using poincare map i can fix fisrt problem, but can i fix this without poincare map?  
Also i have this progress bar when using` length = 50`

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/e/aeaaa42b8abd01ef05eab2aa3894ed1ead0bcc1e.png)

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [August 15, 2022, 2:58pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/3 "2022-08-15T14:58:38Z")

</div>

> [@Sergey\_Novak](#):
>
> but I have a strange result

what’s the strange thing?

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [August 15, 2022, 2:59pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/4 "2022-08-15T14:59:02Z")

</div>

> [@Sergey\_Novak](#):
>
> but can i fix this without poincare map?

You have to wait a couple of weeks for our new sparse algorithm to be published!

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 15, 2022, 3:04pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/5 "2022-08-15T15:04:43Z")

</div>

I don’t understand why i have so many dots. I have 3 attractors

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/c/7c68492930b0b0ce7f173273510f9e40d89bd907.png)

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 15, 2022, 3:07pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/6 "2022-08-15T15:07:02Z")

</div>

Can i reshape `basins` from 6D to 3D or 2D and then plot him?

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [August 15, 2022, 3:14pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/7 "2022-08-15T15:14:34Z")

</div>

> [@Sergey\_Novak](#):
>
> I don’t understand why i have so many dots. I have 3 attractors

Yeah and your plot has 3 colors, because you have 3 attractors. Attractors may have “many points”, i.e., not be fixed points. I’m a bit confused by the question.

> [@Sergey\_Novak](#):
>
> Can i reshape `basins` from 6D to 3D or 2D and then plot him?

You can project them if you want, after computing them, but it makes no sense to “reshape” the basins into 3D. What sense does it make to reshape a sphere into a circle?

Given your questions, perhaps you should consider going through a couple of textbooks of nonlinear dynamics to sharpen your understanding in these topics. Unfortunately, it is not possible to get this understanding only by running code

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 15, 2022, 3:25pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/8 "2022-08-15T15:25:01Z")

</div>

I’m a little confused why the attractors had so many points, now I understand thanks.  
I’m trying to see basins and attractors in 3D and 2D, I just don’t understand how to do it.

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 15, 2022, 3:52pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/9 "2022-08-15T15:52:03Z")

</div>

Also I found a small typo in the documentation code.  
In sectiond 3D in code for ploting 3d Image using variable `bsn` but it not declared. I think the need replace `bsn` on `basins`  
Full code from section:

```julia
using GLMakie
fig = Figure()
display(fig)
ax = fig[1,1] = Axis3(fig; title = "found attractors")
cmap = cgrad(:dense, 6; categorical = true)

for i in keys(attractors)
    tr = attractors[i]
    markersize = length(attractors[i]) > 10 ? 2000 : 6000
    marker = length(attractors[i]) > 10 ? :circle : :rect
    scatter!(ax, columns(tr)...; markersize, marker, transparency = true, color = cmap[i])
    j = findfirst(isequal(i), bsn)
    x = xg[j[1]]
    y = yg[j[2]]
    z = zg[j[3]]
    tr = trajectory(ds, 100, SVector(x,y,z); Ttr = 100)
    lines!(ax, columns(tr)...; linewidth = 1.0, color = cmap[i])
end

a = range(0, 2π; length = 200) .+ π/4

record(fig, "cyclical_attractors.mp4", 1:length(a)) do i
    ax.azimuth = a[i]
end

```

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 15, 2022, 5:55pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/10 "2022-08-15T17:55:02Z")

</div>

I found one of your articles. By my question I meant how to get an image similar to (h) with dots of attractor for example from (c).

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/1/61820c257945d7925e1bf9f35c2217cbb36bf788.jpeg)

Also i needed images how this  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/1/9146bfae826d236e9f1aacd6092240329b4b3203.png)

Now it has become a little clearer to me how to get them, sorry for spaming

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [August 15, 2022, 7:15pm UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/11 "2022-08-15T19:15:54Z")

</div>

> [@Sergey\_Novak](#):
>
> I’m trying to see basins and attractors in 3D and 2D, I just don’t understand how to do it.

Well you can do this if your system is 3D or 2D. Otherwise you have to **project the basins and/or the attractors into a lower dimensional space.** This is not reshaping, nor is it an invertible operation. You need to know the system to know when this is possible and when it retains some accuracy. In many systems the attractors can’t be separated by projection.

> [@Sergey\_Novak](#):
>
> Also I found a small typo in the documentation code.

Sounds like a great opportunity for a pull request!

> [@Sergey\_Novak](#):
>
> By my question I meant how to get an image similar to (h) with dots of attractor for example from (c).

(h) is just a slice of 6D basins. Like plotting `basins[1, 1, 1, 1, :, :]`. There are no attractors plotted there, because you simply can’t project the attractors into 2D space for this system.

> [@Sergey\_Novak](#):
>
> sorry for spaming

No worries, but what I said above is for your benefit, to spend some time with a textbook or two. It will raise your intuition levels which is the most important thing. Running code is easy, understanding it is the hard part.

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 16, 2022, 7:16am UTC](https://discourse.julialang.org/t/basins-of-attractros-for-6d-system-dynamicalsystems-jl/85781/12 "2022-08-16T07:16:24Z")

</div>

Thanks, thanks to the code from the article, it became clearer how to do this.  
But some code snippets worked a little incorrectly. These fragments code are related to the color of the attractors and they were also found in the documentation.

For example this fragment code from section 3D basins:

```julia
fig = Figure()

ax = fig[1,1] = Axis3(fig; title = "found attractors")
cmap = cgrad(:dense, 6; categorical = true)

for i in keys(attractors)
    
    tr = attractors[i]
    markersize = length(attractors[i]) > 10 ? 2000 : 6000
    marker = length(attractors[i]) > 10 ? :circle : :rect
    
    scatter!(ax, columns(tr)...; markersize, marker, transparency = true, color = cmap[i])
    j = findfirst(isequal(i), bsn)
    x = xg[j[1]]
    y = yg[j[2]]
    z = zg[j[3]]
    tr = trajectory(ds, 100, SVector(x,y,z); Ttr = 100)
    lines!(ax, columns(tr)...; linewidth = 1.0, color = cmap[i])
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/b/3b0a788739ba0557ac00820c31565a1fb183fbad.png)  
I fix this deleting line code `markersize = length(attractors[i]) > 10 ? 2000 : 6000`  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/2/12ad98750fac5b8bf2adb90e59448e81fa54bccc.png)

Also i have problem with this visualization on my system. I modification this code

```julia
fig = Figure()
ax = Axis3(fig[1,1])
cmap = cgrad(:dense, 8; categorical = true)

for i in keys(attractors)
    
    tr = attractors[i]
    
    marker = length(attractors[i]) > 10 ? :circle : :rect
    
    idxs = [1,2,3]
    
    x,y,z = columns(tr)[idxs]
    
    scatter!(ax, x,y,z; marker, transparency = true, color = cmap[i])
    
    
    tr = trajectory(ds_HR, 5000, tr[1], Ttr = 1000;
                    diffeq = (alg = AutoVern9(Rodas5()), abstol = 1e-11, reltol = 1e-11, maxiters = 10000000) )
    
    x,y,z = columns(tr)[idxs]
    
    lines!(ax, x,y,z; linewidth = 1.0, color = cmap[i])

end

fig

```

and get this result

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/4/04b0579593785f0b9112056f5cd752f2dcc712e4.png)  
I don’t see on this images trajectory from anothers attractors, for example for blue points. This may be due to the fact that they cannot be projected onto a given subspace? Or this error with visualization?

If i using more time and Ttr, for example 8000 and 4000 i have this images:

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

fragment of code for search basins and attractors

```julia
xg1 = yg1 = zg1 = xg2 = yg2 = zg2 = range(-5.0, 5.0; length = 15)

mapper = AttractorsViaRecurrences(ds_HR, (xg1, yg1, zg1, xg2, yg2, zg2),
                                    diffeq = (alg = AutoVern9(Rodas5()),
                                                abstol = 1e-11, reltol = 1e-11, dense = false, maxiters = 10000000))
basins, attractors = basins_of_attraction(mapper)
attractors

```
