Plotting a phase portrait for the nonlinear system

How can I plot a phase portrait for the nonlinear system in Julia? For example: u=Y
v=X * (1 - X**2) + Y.

To study and plot the phase portrait of a discrete dynamical system, use DynamicalSystems.jl

using DynamicalSystems, Plots
plotlyjs()
function syst_def(u, p, n) #n is for the n^th point in trajectory, but it isn't used
    x, y = u 
    xnew = y
    ynew = x*(1-x^2)+y
    return SVector(xnew, ynew)
end

initialCond=[[0.7, -0.3]]   #, [0.1, 0.8], [0.4, 0.31], [-0.25, -0.37]]
p0=[0, 0] #dummy parameter, because this system does not depend on parameters

plt=scatter()
for u0 in initialCond
    syst= DeterministicIteratedMap(syst_def, u0, p0)
    X, t = trajectory(syst, 8000) #8000 represents the number of points to be computed on an orbit/trajectory
    scatter!(X[:, 1],X[:,2],  markersize=1, 
            framestyle=:box, size=(500, 300), legend=fals    
end
xlabel!("x")
ylabel!("y")


Since your system exhibits an attractor it is sufficient to take a single initial condition, but for a general system you can compute and plot more than one trajectory. The code above was initially written for more initial conditions.

2 Likes
"""
step :
1. get derivatives function
2. use arrow function to plot vector according to  derivatives function
here use Symbolics.jl method  computing  symbolic  derivates, then build julia funtion
"""

using Plots,Symbolics

function make_func()
  @variables x, y
  u=x*(1-(x^2))+y
  Dx=Differential(x)
  Dy=Differential(y)
  f=u|>fxy->(build_function(fxy,x,y))|>eval
  fx′=Dx(u)|>expand_derivatives|>dx->(build_function(dx,x,y))|>eval
  fy′=Dy(u)|>expand_derivatives|>dy->(build_function(dy,x,y))|>eval

  return f,fx′,fy′
end

scale=0.1
xspan=range(-1.0,1.0,20)
yspan=range(-1.0,1.0,20)

f,fx′,fy′=make_func()

xs = vec([x for (x, y) = Iterators.product(xspan, yspan)])
ys = vec([y for (x, y) = Iterators.product(xspan, yspan)])
us =  scale*vec([fx′(x,y) for (x, y) = Iterators.product(xspan, yspan)])
vs =  scale*vec([fy′(x,y) for (x, y) = Iterators.product(xspan, yspan)])
p1=quiver(xs,ys , quiver = (us, vs),label=false,frame=:zerolines)  # vectorfields plot

zs= vec([f(x,y) for (x, y) = Iterators.product(xspan, yspan)])
p2=surface(xs,ys,zs,label=false)    # original function plot

p3=plot(p1,p2,layout=(1,2))

#savefig(p3,"./Fig/vector_field.png")

vector_fields7

1 Like

Makie.streamplot!

1 Like