# Lyapunov spectrum, HyperChaos, DynamicalSystems.jl

**URL:** <https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539>\
**Category:** Modelling & Simulations\
**Created:** [August 9, 2022, 2:46pm UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539 "2022-08-09T14:46:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 9, 2022, 2:46pm UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/1 "2022-08-09T14:46:49Z")

</div>

I calculate lyapunov spectrum in hyperchaos. Why i have such spectrum:

```julia
[0.015581182326147337,
 0.0028325903213687266,
 1.0762649529273773e-6,
 -0.007004756972893843,
 -5.7866575928839605,
 -11.883439707053556]

```

For hyperchaos need two positive exponent lyapunov, one zero exponent and other negative. Why third exponent don’t zero and not infinitesimal value?  
Code:

```julia
function sigma(x)
    return @fastmath 1.0 / ( 1.0 + exp( -10.0 * ( x - ( - 0.25 ) ) ) )
end

function HR(u, p, t)
        
    a, b, c, d, s, xr, r, I, vs, k1, k2, el_link = p
    x1, y1, z1, x2, y2, z2 = u
    
    du1 = y1 + b * x1 ^ 2 - a * x1 ^3 - z1 + I - k1 * ( x1 - vs ) * sigma(x2) + el_link * ( x2 - x1 )
    du2 = c - d * x1 ^2 - y1
    du3 = r * ( s * ( x1 - xr ) - z1 )
    
    du4 = y2 + b * x2 ^ 2 - a * x2 ^3 - z2 + I - k2 * ( x2 - vs ) * sigma(x1) + el_link * ( x1 - x2 )
    du5 = c - d * x2 ^2 - y2
    du6 = r * ( s * ( x2 - xr ) - z2 )
    return SVector(du1, du2, du3,
                    du4, du5, du6)
end

condition = SA[-0.6925302979542225,
 -2.2083565485726155,
  3.4251812063410423,
 -0.49710798909310244,
 -0.24112901660181446,
  4.585643148476261]

tspan = (0.0, 500000.0)

a = 1.
b = 3.
c = 1.
d = 5.
xr = -1.6
r = 0.01 # 0.01
s = 5.
I = 4.
xv = 2.

k1= -0.17
k2 = k1
k = 0.155

p = SA[a, b, c, d, s, xr, r, I, xv, k1, k2, k]

k_space = range(0.155, 0.161, step = 0.001)

spectrum_array = zeros(6, length(k_space))
condition_array = zeros(6, length(k_space))

int(x) = floor(Int, x)
length(k_space)

for (i, k) in enumerate(k_space)
    if i == 1
        global initialcondition = condition
    end
    println("Initial condition: $initialcondition"); flush(stdout)
    println("k: $k"); flush(stdout)
    
    condition_array[:, i] = initialcondition
    
    p = SA[a, b, c, d,
        s, xr, r, I, xv, k1, k2, k]
    
    prob = ODEProblem(HR, initialcondition, tspan, p)
    sol = solve(prob, Vern9(), abstol = 1e-11, reltol = 1e-11, dense=false, maxiters = 30000000)
    println("solve complete"); flush(stdout)
    ds_HR = ContinuousDynamicalSystem(HR, initialcondition, p )
    spectrum = lyapunovspectrum(ds_HR, tspan[2]; diffeq = (alg = Vern9(),
                                                            abstol = 1e-11, reltol = 1e-11,
                                                            maxiters = 30000000
                                                            ))
    spectrum_array[1:6, i] = spectrum[1:6]
    
    println("Spectrum: ", spectrum_array[1:6, i]); flush(stdout)
    
    initialcondition = sol[length(sol.u)]
    println(">>>>>>>>>>>>>")
    println("")
end

```

image for spectrum:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/d/fddf3ad39aa073870dde2cad629a347048d6a9a8.png)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 9, 2022, 8:09pm UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/2 "2022-08-09T20:09:36Z")

</div>

Sounds like a @Datseris question

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [August 9, 2022, 10:27pm UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/3 "2022-08-09T22:27:43Z")

</div>

tl;dr: you don’t get exact answers from floating-point arithmetic.

There are numerous approximations in this calculation that will result in not-exactly-zero values for things that are exactly zero in principle. There’s finite-timestep integration of ODEs, approximating an infinite-time average with a long finite-time sum, maybe using finite-differencing to approximate a Jacobian, doing linear algebra on moderately poorly-conditioned linear algebra in finite-precision arithmetic, and plain old floating-point round-off. For this calculation it looks like you’re getting about seven digits of precision (1e-06 compared to 11.88), which is as good as you can get if the Jacobians are 1st-order finite-difference (square root of machine epsilon, 1e-16).

---

<div class="post-metadata">

**Author:** ![j\_u](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_u/32/219081_2.png) [@j\_u](https://discourse.julialang.org/u/j_u)\
**Post date:** [August 9, 2022, 10:55pm UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/4 "2022-08-09T22:55:14Z")

</div>

This is of course absolutely off topic but I have to admit that sometimes, somehow I like this forum; some of the questions and answers are just simply outstanding. :- )

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [August 10, 2022, 8:04am UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/5 "2022-08-10T08:04:14Z")

</div>

> [@Sergey\_Novak](#):
>
> For hyperchaos need two positive exponent lyapunov, one zero exponent and other negative. Why third exponent don’t zero and not infinitesimal value?

that’s exactly what you have though 😃 @John_Gibson 's answer is what you need to read in detail why you get this.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 10, 2022, 8:09am UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/6 "2022-08-10T08:09:01Z")

</div>

is it possible to get more precise values, for example if I set the Jacobian?

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [August 10, 2022, 11:33am UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/7 "2022-08-10T11:33:03Z")

</div>

Yes, providing the analytic Jacobian is probably the most important way to improve the precision of the results. The other thing I’d do is increase the finite integration time to get a better approximation of the infinite-time average.

I’m not sure of the details of DynamicalSystem.jl’s Jacobian approximation, but there might be a way to increase the finite-difference approximation of the Jacobian from the first-order df/dx = (f(x+\epsilon) - f(x))/\epsilon + O(\epsilon) to the second-order df/dx = (f(x+\epsilon/2) - f(x-\epsilon/2))/\epsilon + O(\epsilon^2). With the right epsilon that could get you from seven digits to ten.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [August 10, 2022, 1:49pm UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/8 "2022-08-10T13:49:29Z")

</div>

Thank you, I will try to calculate the Jacobian analytically and set it

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [August 11, 2022, 1:19pm UTC](https://discourse.julialang.org/t/lyapunov-spectrum-hyperchaos-dynamicalsystems-jl/85539/9 "2022-08-11T13:19:53Z")

</div>

We use ForwardDiff.jl
