# How to draw a phase portrait of a system of differential equations

**URL:** <https://discourse.julialang.org/t/how-to-draw-a-phase-portrait-of-a-system-of-differential-equations/89492>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [October 29, 2022, 1:51pm UTC](https://discourse.julialang.org/t/how-to-draw-a-phase-portrait-of-a-system-of-differential-equations/89492 "2022-10-29T13:51:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 1:51pm UTC](https://discourse.julialang.org/t/how-to-draw-a-phase-portrait-of-a-system-of-differential-equations/89492/1 "2022-10-29T13:51:25Z")

</div>

Hi all,

I have this uncoupled linear system

\dot{x}\_{1} = -x\_{1}

\dot{x}\_{2} = 2x\_{2}

the system can be written in matrix form as

\dot{x} = Ax

with A = [-1 \ 0; 0 \ 2]

the general solution are:

x\_{1} (t) = c\_{1} e^{-t}

x\_{2} (t) = c\_{2} e^{2t}

or

x(t) = [e^{-t} \ 0; 0 \ e^{2t}]c

in the book they draw the phase portrait as below:  
 ![Capture d’écran_2022-10-29_20-44-58](https://global.discourse-cdn.com/julialang/original/3X/5/b/5ba083e3a0233800a033e90624e1f5cf916e4d4d.png)

My question is how to draw such phase portrait?

Thanks.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 29, 2022, 1:59pm UTC](https://discourse.julialang.org/t/how-to-draw-a-phase-portrait-of-a-system-of-differential-equations/89492/2 "2022-10-29T13:59:29Z")

</div>

For example like this with Makie: [streamplot](https://docs.makie.org/stable/examples/plotting_functions/streamplot/#examples)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 6:04pm UTC](https://discourse.julialang.org/t/how-to-draw-a-phase-portrait-of-a-system-of-differential-equations/89492/3 "2022-10-29T18:04:16Z")

</div>

Is there any way to use CairoMakie in REPL and save the image as png, instead of using Jupyter Notebook / IJulia?

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 6:14pm UTC](https://discourse.julialang.org/t/how-to-draw-a-phase-portrait-of-a-system-of-differential-equations/89492/4 "2022-10-29T18:14:14Z")

</div>

I need to add

```julia
using Meshes

```

but the codes do not work:

**Function passed to streamplot must return Point2 or Point3**

**Stacktrace:**  
\*\* [1] error(s::String)\*\*  
\*\* @ Base ./error.jl:33\*\*  
\*\* [2] streamplot\_impl(CallType::Type, f::typeof(f), limits::Rect2D{Float64}, resolutionND::Tuple{Int64, Int64, Int64}, stepsize::Float64, maxsteps::Int64, dens::Float64)\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/basic\_recipes/streamplot.jl:112\*\*  
\*\* [3] (::Makie.var"#611#616")(f::Function, limits::Rect2D{Float64}, resolution::Tuple{Int64, Int64, Int64}, stepsize::Float64, maxsteps::Int64, density::Float64)\*\*  
\*\* @ Makie ~/.julia/packages/Makie/c5WJV/src/basic\_recipes/streamplot.jl:167\*\*

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [October 29, 2022, 6:50pm UTC](https://discourse.julialang.org/t/how-to-draw-a-phase-portrait-of-a-system-of-differential-equations/89492/5 "2022-10-29T18:50:00Z")

</div>

I am trying this code but can’t work to save the image as svg, which line do i miss?

```julia
using CairoMakie

struct FitzhughNagumo{T}
    ϵ::T
    s::T
    γ::T
    β::T
end

P = FitzhughNagumo(0.1, 0.0, 1.5, 0.8)

f(x, P::FitzhughNagumo) = Point2f(
    (x[1]-x[2]-x[1]^3+P.s)/P.ϵ,
    P.γ*x[1]-x[2] + P.β
)

f(x) = f(x, P)

streamplot(f, -1.5..1.5, -1.5..1.5, colormap = :magma)

save("/home/browni/LasthrimProjection/JupyterLab/CairoMakie/example.svg",fig)

```

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 29, 2022, 6:50pm UTC](https://discourse.julialang.org/t/how-to-draw-a-phase-portrait-of-a-system-of-differential-equations/89492/6 "2022-10-29T18:50:22Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> Function passed to streamplot must return Point2 or Point3

This is the reason. It’s a simple fix, best share your code
