# Plotting an attractor solution

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

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

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:

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)


I got a bit carried away and did the multiple curve example too.

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


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

11 Likes

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

5 Likes

This looks beautiful Thank you!

2 Likes

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

1 Like