# Plotting an attractor solution

**URL:** https://discourse.julialang.org/t/plotting-an-attractor-solution/35992
**Category:** New to Julia
**Tags:** question
**Created:** [March 14, 2020, 11:41pm UTC](https://discourse.julialang.org/t/plotting-an-attractor-solution/35992 "2020-03-14T23:41:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Eris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eris/32/8438_2.png) [@Eris](https://discourse.julialang.org/u/Eris)
#### Post date: [March 14, 2020, 11:41pm UTC](https://discourse.julialang.org/t/plotting-an-attractor-solution/35992/1 "2020-03-14T23:41:00Z")

</div>

I’m new to Julia and learning how to use ODEProblem. I’m trying to solve the following system

 ![imgonline-com-ua-twotoone-7xMJ4sodegZ6hKh](https://global.discourse-cdn.com/julialang/original/3X/2/d/2dcd9fd8015be69f8be9af84f0bf47f83817b3bd.jpeg)

However my code is only producing a straight line that does not reflect this behavior. What is wrong with my implementation?

The code

> attrac = @ode\_def begin  
> dy= -σ \* sqrt(y^2+ρ^2_ϕ^2)+ρ^2_ϕ/y #y stands for phi dot  
> dϕ = y  
> end σ ρ  
> u0 = [1.0,0.0] # initial conditions  
> tspan = (-10.0,10.0) # timespan  
> p = [sqrt(12)\*pi,.0] # parameters  
> prob = ODEProblem(attrac, u0, tspan, p)  
> sol = solve(prob)  
> plot(sol, vars = (1, 2))

---

<div class="post-metadata">

### Author: ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)
#### Post date: [March 15, 2020, 7:34am UTC](https://discourse.julialang.org/t/plotting-an-attractor-solution/35992/2 "2020-03-15T07:34:05Z")

</div>

It seems that you made some small mistakes when typing in the equation, for example, `-σ * sqrt(y^2+ρ^2ϕ^2)+ρ^2ϕ/y` should be `-(σ * sqrt(y^2+ρ^2ϕ^2)*y+ρ^2ϕ)/y`. However, I think that the major source of confusion is to do with how an ODE is passed to DifferentialEquations.

`ODEProblem` expects an equation of the form:  
\frac{du}{dt}=f(t,u)  
which is a little different to the equation that you are solving, which is in phase space. To get your equation into the form that `ODEProblem` wants, you have to multiply both sides of the equation by \frac{d\phi}{dt}.

Doing so, you will end with the system of equations:  
\frac{d\phi}{dt}=\dot{\phi},  
\frac{d\dot{\phi}}{dt}=-(\sqrt{12\pi}(\dot{\phi}^2+m^2\phi^2)^\frac{1}{2}\dot{\phi}+m^2\phi).  
This equation can be passed to `ODEProblem` as follows:

```julia
using DifferentialEquations
using Plots
using ParameterizedFunctions

# EDIT The system has been changed as described above.
attrac = @ode_def begin
    dϕ = ϕ_dot
    dϕ_dot = -σ*sqrt(ϕ_dot^2+ρ^2*ϕ^2)*ϕ_dot-ρ^2*ϕ
end σ ρ

u0 = [1.0,0.0] # initial conditions
tspan = (0.0,10.0) # timespan
p = [sqrt(12π),1.0] # parameters --EDIT-- Set σ=sqrt(12π) and ρ=1.
prob = ODEProblem(attrac, u0, tspan, p)
sol = solve(prob,alg=Trapezoid(),abstol=1e-10,reltol=1e-10) # --EDIT-- changed the algorithm and set low tolerances
plot(sol,vars=(1,2),leg=false)

```

![single_trajectory](https://global.discourse-cdn.com/julialang/original/3X/6/3/63099f990c0e67031449abbc1fc75ce1a45189a8.png)  
I got a bit carried away and did the multiple curve example too.

```julia
using DifferentialEquations
using Plots
using ParameterizedFunctions
using LaTeXStrings

fig = plot(leg=false,xlabel=L"\phi",ylabel=L"\dot{\phi}")
for u0 in [[[x,1.0] for x in range(-1,stop=0.5,length=5)];
           [[x,-1.0] for x in range(-0.5,stop=1,length=5)]]
    prob = ODEProblem(attrac, u0, tspan, p)
    sol = solve(prob,alg=Trapezoid(),abstol=1e-10,reltol=1e-10)
    
    plot!(fig,sol,vars=(1,2),xlim=(-1,1),ylim=(-1,1),color=:black,w=1.5)
end
fig

```

![multi_trajectory](https://global.discourse-cdn.com/julialang/original/3X/7/4/74663bf6281b62cbe30ae8ef89d5e4faa2b9945e.png)

**EDIT**  
To get LaTeX to match the phi with the one in the example figure that you posted, use \varphi

---

<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 15, 2020, 9:54am UTC](https://discourse.julialang.org/t/plotting-an-attractor-solution/35992/3 "2020-03-15T09:54:21Z")

</div>

As a small side note, an autonomous ODE in the plane (such as your system) cannot generate chaotic dynamics.

---

<div class="post-metadata">

### Author: ![Eris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eris/32/8438_2.png) [@Eris](https://discourse.julialang.org/u/Eris)
#### Post date: [March 15, 2020, 12:01pm UTC](https://discourse.julialang.org/t/plotting-an-attractor-solution/35992/4 "2020-03-15T12:01:04Z")

</div>

This looks beautiful 😃 Thank you!

---

<div class="post-metadata">

### Author: ![Eris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eris/32/8438_2.png) [@Eris](https://discourse.julialang.org/u/Eris)
#### Post date: [March 15, 2020, 12:07pm UTC](https://discourse.julialang.org/t/plotting-an-attractor-solution/35992/5 "2020-03-15T12:07:59Z")

</div>

I thought an attractor solution signals a chaotic system but you are right; there exist nonchoatic systems with attractor solution just as this example.
