Error DynamicalSystems.jl Plotting trajectories

Hello,

I am trying to follow the Intro to DynamicalSystems.jl YouTube Video from the Official Julia programming Account: Intro to dynamical systems in Julia - YouTube

I want to plot the trajectories of a simple deterministic Dynamical System but always get an error while I try to plot the trajectories.

The Plotting error is: Cannot convert StateSpaceSet{2, Float64} to series data for plotting

While calling tr[:,1] I should get a vector but always get an error:

ERROR: MethodError: no method matching getindex(::Tuple{StateSpaceSet{2, Float64}, StepRange{Int64, Int64}}, ::Colon, ::Int64)

Somehow I can not work with these Datset types.

Code is below

## 1) Define function of System: 
# Using a Static Vector because the System is small
h_eom(x,p, t) = SVector{2}(1.0 - p[1]*x[1]^2 + x[2], p[2]*x[1] )

## 2) State 
state = zeros(2) # Does not matter if we use Vector of SVector for the state 

## 3) Parameters
p = [1.4, 0.3] # p = [a,b] from the equations of motion


## 1-3) Are enough to make a DynamicalSystems
henon = DiscreteDynamicalSystem(h_eom, state, p )

##### Getting a trejectory
# trajectory(ds:DynamicalSystems, T [, u]; kwargs... )

##### Getting a trajectory

## Trajectory from initial condition:
tr = trajectory(henon, 10000)

##Trajectory from a different starting point:
tr2 = trajectory(henon, 10000 , 0.01rand(2))

## Plotting
figure(figsize=(6,4))
plot(tr[1][:], tr[2][:], lw=0.0, marker="o", ms=0.1, alpha=0.5)
plot(tr2[1][:], tr2[2][:], lw=0.0, marker="o", ms=0.1, alpha=0.5)
xlabel("x")
ylabel("y")

Hey

The trajectory function was updated and now returns both the trajectory as a StateSpaceSet and the corresponding times points as a StepRange. The error is telling you exactly this, actually. Since you assign tr to both, tr in your code is a tuple of both a StateSpaceSet and a StepRange - these are the trajectory and the time points.

To separate the two, you can simply do:

tr, ts = trajectory(henon, 10000)

That should solve your problem. But just as a note, you can easily transform a StateSpaceSet into a Matrix by doing:

tr_matrix = Matrix(tr)

Then, the plotting interfaces should handle them fine.

2 Likes

Thank you! That fixed all my problems.

1 Like