# How to use pictures in plot to show area preservation using julia?

**URL:** <https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [September 6, 2023, 1:08pm UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570 "2023-09-06T13:08:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)\
**Post date:** [September 6, 2023, 1:08pm UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/1 "2023-09-06T13:08:22Z")

</div>

I have a data of symplectic Euler method

```julia
using Plots
h = 0.05;
N = 200;
const g = 9;
const ℓ = 1.46;
θ = zeros(N+1)
v = zeros(N+1)
θ[1] = 1.0; # this is θ₀
v[1] = 0.5; # this is v₀
for k in 1:N  
   θ[k+1] = θ[k] + h * v[k]
   v[k+1] = v[k] - h * ( g / ℓ ) * sin(θ[k+1])   
 end
plot(θ,v,lc=:red, lw=1)

```

![pend](https://global.discourse-cdn.com/julialang/original/3X/8/1/815f63cd98e5c3084771f13dfd3b6cc4aa977c68.png)

The flow of this method preserve the area and I would like to investigate this property by using some pictures inside the plot like in the following

![Screenshot from 2023-09-06 14-36-36](https://global.discourse-cdn.com/julialang/original/3X/8/8/889348f6f3b1c46745296adf29df6a638131c209.png)  
 ![Screenshot from 2023-09-06 14-37-25](https://global.discourse-cdn.com/julialang/original/3X/4/c/4ccc5487be4e497c259fcaf6beb80b803c19fccb.png)

---

<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:** [September 6, 2023, 10:24pm UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/2 "2023-09-06T22:24:36Z")

</div>

Take as starting positions a number of points on a circle and compute the iterates, F^{kn}, of these points, for a fixed n, and k=1, 2, …kmax. Plot the interpolating curve of the corresponding points after each k.

---

<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:** [September 7, 2023, 7:14am UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/3 "2023-09-07T07:14:00Z")

</div>

Here is the code that implements what is described above:

```julia
using Plots
function F(p::Vector{T}; h=0.05, g=9, ℓ=1.46) where T<:AbstractFloat
   Theta =p[1]+h*p[2]
   return [Theta, p[2]-h*(g/ℓ)*sin(Theta)]
end  
composition(F, n) = ∘(ntuple(_ -> F, n)...)

function circle(t; center=[1.0, 0], r=0.3)
    [center[1]+r*cos(t), center[2]+r*sin(t)]
end    

t=range(0, 2pi, 24)
pc=circle.(t;)
pl= plot(getindex.(pc, 1), getindex.(pc, 2), lc=:red, lw=1, 
         legend=false, size=(450, 450), aspect_ratio = :equal)
for _ in 1:10
    pc=composition(F, 5).(pc)
    plot!(pl, getindex.(pc, 1), getindex.(pc, 2), lc=:red, lw=1)
end    
display(pl)

```

---

<div class="post-metadata">

**Author:** ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)\
**Post date:** [September 8, 2023, 6:19am UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/4 "2023-09-08T06:19:59Z")

</div>

Thanks @empet ! The code work perfectly in this case, but when using explicit Euler like

```julia
using Plots
h = 0.05;
N = 200;
const g = 9;
const ℓ = 1.46;
θ = zeros(N+1)
v = zeros(N+1)
θ[1] = 1.0; # this is θ₀
v[1] = 0.5; # this is v₀
for k in 1:N  
   θ[k+1] = θ[k] + h * v[k]
   v[k+1] = v[k] - h * ( g / ℓ ) * sin(θ[k])   
 end
plot(θ,v)

```

it didn’t give me the expected result like  
 ![Screenshot from 2023-09-08 08-19-16](https://global.discourse-cdn.com/julialang/original/3X/0/d/0db146189442c057d7cf471c2581a4df8cf1ddd0.png)

---

<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:** [September 8, 2023, 7:55am UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/5 "2023-09-08T07:55:22Z")

</div>

Please post your code that generated your last plot.  
The function F has the same effect as your discrete system.

---

<div class="post-metadata">

**Author:** ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)\
**Post date:** [September 8, 2023, 8:30am UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/7 "2023-09-08T08:30:25Z")

</div>

The first code contain `sin(θ[k+1])` and the last code contains `sin(θ[k])` which give me  
 ![Screenshot from 2023-09-08 10-29-24](https://global.discourse-cdn.com/julialang/original/3X/f/f/ffff93ff806d469d5c571c2d77e51c8fafa97168.png)

---

<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:** [September 8, 2023, 9:05am UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/8 "2023-09-08T09:05:51Z")

</div>

Without your complete code I cannot help you.

---

<div class="post-metadata">

**Author:** ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)\
**Post date:** [September 8, 2023, 9:07am UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/9 "2023-09-08T09:07:47Z")

</div>

Sorry, @empet see this code about explicit Euler

```julia
using Plots
function F()
h = 0.05;
N = 200;
 g = 9;
ℓ = 1.46;
θ = zeros(N+1)
v = zeros(N+1)
θ[1] = 1.0; # this is θ₀
v[1] = 0.5; # this is v₀
for k in 1:N  
   θ[k+1] = θ[k] + h * v[k]
   v[k+1] = v[k] - h * ( g / ℓ ) * sin(θ[k])   
 end
   return hcat(θ, v)
end  

composition(F, n) = ∘(ntuple(_ -> F, n)...)

function circle(t,k)
    [1+(inv(1+h^2))^k*cos(t), (inv(1+h^2))^k*sin(t)]
end    

t=range(0, 2pi, 35)
pc=circle.(t;)
pl= plot(getindex.(pc, 1), getindex.(pc, 2), lc=:red, lw=1, 
         legend=false, size=(450, 450), aspect_ratio = :equal)
for _ in 1:5:100
    pc=composition(F, 10).(pc)
    plot!(pl, getindex.(pc, 1), getindex.(pc, 2), lc=:red, lw=1)
    plot!(x[:,1],x[:,2])
end    
display(pl)

```

---

<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:** [September 8, 2023, 9:42am UTC](https://discourse.julialang.org/t/how-to-use-pictures-in-plot-to-show-area-preservation-using-julia/103570/10 "2023-09-08T09:42:24Z")

</div>

Your discrete system defined today is not the same as that you posted initially, when you formulated your question.  
The last one, defined today,

```julia
θ[k+1] = θ[k] + h * v[k]
v[k+1] = v[k] - h * ( g / ℓ ) * sin(θ[k]) 

```

is not defined by an area preserving map, because the determinant of its Jacobian matrix is not 1 (one). That’s why your plot reveals an expansive effect, as the number of iterations increases.

Your initial system I have used to illustrate the area preserving of a disk under the action of the map F. was:

```julia
 θ[k+1] = θ[k] + h * v[k]
 v[k+1] = v[k] - h * ( g / ℓ ) * sin(θ[k+1])

```
