# How to Draw This Spiral Solution Curve?

**URL:** <https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [November 19, 2022, 7:01pm UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510 "2022-11-19T19:01:28Z")\
**Posts on this page:** 13\
**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:** [November 19, 2022, 7:01pm UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/1 "2022-11-19T19:01:28Z")

</div>

Hi all,

I read this topic and it is still confusing since the differential equation is a bit different:

> [@Plotting a phase portrait of a differential equation](https://discourse.julialang.org/t/plotting-a-phase-portrait-of-a-differential-equation/29208):
>
> Hey Folks. I was just wondering if there was an existing package or tool for plotting the phase portrait of a system of differential equations, or a phase portrait plus direction field? I am thinking of a plot like this for the system of equations. \frac{dx}{dt} = -x \\ \frac{dy}{dt} = 2y The corresponding plot would be (taken from Haberman’s ODE textbook): [Selection\_002] I did not see any mention of this in the plots documentation for the Julia DifferentialEquations package, but perhaps …

I use only variable of t. That topic use dx/dt and dy/dt, thus I can’t really use it to my problem at hand.

This is the problem, and the solution. What I want to achieve is how to plot the spiral curve along with the arrow? Given the input of matrix A and the initial value x(0).

![Capture d’écran_2022-11-20_01-57-55](https://global.discourse-cdn.com/julialang/original/3X/8/1/81cf295e97224451dbba730d34ec80ab373e8537.png)

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 20, 2022, 3:44pm UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/2 "2022-11-20T15:44:57Z")

</div>

Something is weird with this example. At x0 the derivative is [-2,1] but the spiral curve clearly shows something like [0,1]. I haven’t looked carefully but perhaps this is just a poor drawing?

---

<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:** [November 20, 2022, 4:36pm UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/3 "2022-11-20T16:36:11Z")

</div>

The solution curve is for the solution:

\boldsymbol{x} (t) = e^{-2t}[\cos t \ \sin \ t]

with x\_{1} (t) = e^{-2t} \cos \ t and x\_{2} (t) = e^{-2t} \sin \ t

now if we put the t=0 (at the beginning) we will obtain:  
x\_{1} (0) = e^{-0} \cos \ 0 and x\_{2} (0) = e^{-0} \sin \ 0

which will become the initial coordinate for the solution curve of

\boldsymbol{x} (t) = e^{-0}[\cos 0 \ \sin \ 0] = 1[1 \ 0]

thus the coordinate is (1,0) at the beginning and x-axis is the x\_{1} and y-axis is the x\_{2} that is why the arrow is pointing to that way. the term e^{-2t} wil goes to 0 as t goes to infinity… that is why it is spiraling to (0,0).

That is from my understanding. Correct me if I am wrong… I know there are lots of silent reader who is an expert / more than PhDs here…

The problem is I want to draw the solution curve…

Can anyone help? Or just pray I can make it alone…  
either way thanks folks! or aliens or vampires here…

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 20, 2022, 5:12pm UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/4 "2022-11-20T17:12:44Z")

</div>

Plotting the solution given the formula is pretty straightforward.

```julia
ts=0.0:.01:5.0

x=[exp(-2*t) cos(t) for t in ts]
y=... Etc

plot(x,y)

```

---

<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:** [November 21, 2022, 7:44am UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/5 "2022-11-21T07:44:59Z")

</div>

> [@dlakelan](#):
>
> ` for t in ts`

After see this: [parametric function plot 3d | [“Julia Plots Gallery”]](https://goropikari.github.io/PlotsGallery.jl/src/parametric3d.html)  
I try this code:

```julia

using MTH229, ForwardDiff, Plots, LaTeXStrings, SymPy
gr()

t = range(0, stop=10, length=1000)
x = e^(-2t)*cos.(t)
y = e^(-2t)*sin.(t)

plot(x, y)

```

but it won’t work:

ERROR: LoadError: MethodError: no method matching ^(::Float64, ::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64})  
Closest candidates are:  
^(::Number, ::Missing) at ~/julia-1.7.3/share/julia/base/missing.jl:124  
^(::T, ::Rational) where T\<:AbstractFloat at ~/julia-1.7.3/share/julia/base/rational.jl:480  
^(::AbstractFloat, ::ForwardDiff.Dual{Ty}) where Ty at ~/.julia/packages/ForwardDiff/eqMFf/src/dual.jl:145  
…  
Stacktrace:  
[1] top-level scope  
@ ~/LasthrimProjection/plot.jl:6  
[2] include(fname::String)  
@ Base.MainInclude ./client.jl:451  
[3] top-level scope  
@ REPL[1]:1  
in expression starting at /home/browni/LasthrimProjection/plot.jl:6

---

<div class="post-metadata">

**Author:** ![Ininterrompue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ininterrompue/32/5594_2.png) [@Ininterrompue](https://discourse.julialang.org/u/Ininterrompue)\
**Post date:** [November 21, 2022, 7:51am UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/6 "2022-11-21T07:51:13Z")

</div>

You’re basically trying to take the exponential of a vector, which is undefined. Try

```julia
x = exp.(-2t)*cos.(t) 

```

or, which is equivalent,

```julia
x = @. exp(-2t)*cos(t)

```

so that the exponential function acts element-wise.

---

<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:** [November 21, 2022, 7:51am UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/7 "2022-11-21T07:51:18Z")

</div>

Wait I fot it works by using GoroPikari example:

```julia

using MTH229, ForwardDiff, Plots, LaTeXStrings, SymPy
gr()

xₜ(t) = exp(-2t)*cos(t)
yₜ(t) = exp(-2t)*sin(t)

plot(xₜ, yₜ, 0, 2π, leg=false)

```

but the question is how to make the small circle goes to (0,0) ? and the quiver or arrow…

---

<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:** [November 21, 2022, 7:52am UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/8 "2022-11-21T07:52:51Z")

</div>

> [@Ininterrompue](#):
>
> `@. exp(-2t)*cos(t)`

Alright this code works:

```julia
t = range(0, stop=10, length=1000)
x = @. exp(-2t)*cos(t)
y = @. exp(-2t)*sin(t)

plot(x, y)

```

but how to create the small circle rotating to (0,0) again plus the arrow?

---

<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:** [November 21, 2022, 8:04am UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/9 "2022-11-21T08:04:40Z")

</div>

This looks better:

```julia
t = range(0, stop=21, length=10000)
x = @. exp(-2t)*cos(t)
y = @. exp(-2t)*sin(t)

plot(x, y,xlims=(-0.1,1), ylims=(-0.1,0.21))

```

I see this CalculusWithJulia’ arrow feature, but it is straight line, how to bend it?

> [@Vectors Addition only work with initial (0,0)?](https://discourse.julialang.org/t/vectors-addition-only-work-with-initial-0-0/85144):
>
> Hi all, I have been learning from this site: [https://docs.juliahub.com/CalculusWithJulia/AZHbv/0.0.5/differentiable\_vector\_calculus/vectors.html#](https://docs.juliahub.com/CalculusWithJulia/AZHbv/0.0.5/differentiable_vector_calculus/vectors.html#) then I change the initial point and see the second vector is not in the place it should be, needs to modify the code… using CalculusWithJulia using Plots gr() # better arrows than plotly() quiver([0],[0], quiver=([1],[2])) unzip(vs) = Tuple(eltype(first(vs))[xyz[j] for xyz in vs] for j in eachindex(first(vs))) u = [2, 1] v = [-2, 1] w = u…

---

<div class="post-metadata">

**Author:** ![Ininterrompue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ininterrompue/32/5594_2.png) [@Ininterrompue](https://discourse.julialang.org/u/Ininterrompue)\
**Post date:** [November 21, 2022, 8:08am UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/10 "2022-11-21T08:08:35Z")

</div>

The solution looks different from the cartoon…

Anyway, you can plot the solution as two parts, one of which has an arrow:

```julia
x1(t) = exp(-2t)*cos(t)
x2(t) = exp(-2t)*sin(t)

plot(x1, x2, 0, π/6, legend=false, color=:black, arrow=true)
plot!(x1, x2, π/6, 2π, legend=false, color=:black)

```

---

<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:** [November 21, 2022, 8:13am UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/11 "2022-11-21T08:13:21Z")

</div>

Yes it works, anyway thanks a lot, now just thinking how to make the small spiraling to (0,0)… or zoom it in

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 21, 2022, 4:09pm UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/12 "2022-11-21T16:09:07Z")

</div>

> [@Freya\_the\_Goddess](#):
>
> spiraling to (0,0)

It reaches 0,0 when t goes to infinity…

The real solution is different from the cartoon. If you want something more like the cartoon try changing the exponential to be a smaller power… exp(-t/2) for example

---

<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:** [November 22, 2022, 5:55pm UTC](https://discourse.julialang.org/t/how-to-draw-this-spiral-solution-curve/90510/13 "2022-11-22T17:55:22Z")

</div>

Alright then I will try it, so the textbook gives me bias then.
