# Plotting a phase portrait for the nonlinear system

**URL:** https://discourse.julialang.org/t/plotting-a-phase-portrait-for-the-nonlinear-system/98514
**Category:** General Usage
**Created:** [May 8, 2023, 11:21pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-for-the-nonlinear-system/98514 "2023-05-08T23:21:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![moxeeeem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moxeeeem/32/47902_2.png) [@moxeeeem](https://discourse.julialang.org/u/moxeeeem)
#### Post date: [May 8, 2023, 11:21pm UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-for-the-nonlinear-system/98514/1 "2023-05-08T23:21:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [May 9, 2023, 5:51am UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-for-the-nonlinear-system/98514/2 "2023-05-09T05:51:56Z")

</div>

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

```julia
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")

```

 ![phase-portrait](https://global.discourse-cdn.com/julialang/original/3X/8/a/8acd5124cbc80a37ce6a9b4eb90c83df1853374a.png)  
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.

---

<div class="post-metadata">

### Author: ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)
#### Post date: [May 9, 2023, 6:43am UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-for-the-nonlinear-system/98514/3 "2023-05-09T06:43:21Z")

</div>

```julia
"""
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](https://global.discourse-cdn.com/julialang/original/3X/a/f/af68e2052d2303e85d06dafe9e6754c28587145a.png)

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [May 9, 2023, 7:05am UTC](https://discourse.julialang.org/t/plotting-a-phase-portrait-for-the-nonlinear-system/98514/4 "2023-05-09T07:05:31Z")

</div>

`Makie.streamplot!`
