In [1]:
using Plots
the logistic map: first steps into chaos¶
read Sethna sec 4.3
$$ x_{n+1} = r x_n (1-x_n) $$
- stable when r in 1:3
- oscillation between 2 values when r in 3:4.5
- CRAZY? when r > 3.45
In [2]:
let
function iterate(x, r)
return r*x*(1-x)
end
rr = 2.5
N1 = 10
# different starting points
x1_arr = zeros(N1)
x2_arr = zeros(N1)
# intial values
x1_arr[1] = 0.01
x2_arr[1] = 0.9
x1 = x1_arr[1]
x2 = x2_arr[1]
for i1 = 2:N1
x1 = iterate(x1, rr)
x2 = iterate(x2, rr)
# record them
x1_arr[i1] = x1
x2_arr[i1] = x2
end
scatter(1:N1, x1_arr, ylim = (0, 1.1), marker=:square)
scatter!(1:N1, x2_arr, ylim = (0, 1.1), title="the logistic map @ r=$rr", xlabel="iterations")
# analytic limit
plot!(1:N1, x -> 1 - 1 / rr, label = "1-1/r", color="gray", line=(dash=:dash))
end
Out[2]:
In [3]:
let
iterate = (x, r) -> r*x*(1-x)
rr = 3.5
N1 = 20
# different starting points
x1_arr = zeros(N1)
x2_arr = zeros(N1)
# intial values
x1_arr[1] = 0.01
x2_arr[1] = 0.9
x1 = x1_arr[1]
x2 = x2_arr[1]
for i1 = 2:N1
x1 = iterate(x1, rr)
x2 = iterate(x2, rr)
# record them
x1_arr[i1] = x1
x2_arr[i1] = x2
end
scatter(1:N1, x1_arr, ylim = (0, 1.1), marker=:square)
scatter!(1:N1, x2_arr, ylim = (0, 1.1), title="the logistic map @ r=$rr", xlabel="iterations")
# analytic limit
plot!(1:N1, x -> 1 - 1 / rr, label = "1-1/r", color="gray", line=(dash=:dash))
end
Out[3]:
In [4]:
let
# visual aid to understand end points
# r = 3.4 has 2 end points
# solution is at x = f(x) = f(f(x)) = ...
r = 3.4
f(x) = r * x * (1 - x)
rv = range(0.0, 1.0, length=250)
Plots.plot(rv, x -> x, label = "x",legend=:bottomright)
Plots.plot!(rv, x -> f(x), label = "f(x)")
Plots.plot!(rv, x -> f(f(x)), label = "f(f(x))")
Plots.plot!(rv, x -> f(f(f(x))), label = "f(f(f(x)))")
Plots.plot!(rv, x -> f(f(f(f(x)))), label = "f(f(f(f(x))))")
end
Out[4]:
invariant mass density¶
use a histogram to collect the set of final points at a given r
In [5]:
let
# invariant density
# just plot the number of occurances in a histogram
rval = 3.64
Nmax = 5400
x_arr = zeros(Nmax)
iterate = x -> rval * x * (1-x)
# random start
x = rand()
for _ = 1:Nmax
x = iterate(x)
end
# start collection
for i1 in 1:Nmax
x = iterate(x)
x_arr[i1] = x
end
# analytic result for r -> 4
f = x -> 1 / sqrt(x * (1 - x)) / pi
histogram(x_arr, normalize = true, bins = 150, label="numerical", legend=:topleft)
plot!(0:0.01:1, f,
label="analytic (r=4)",
title="invariant density @ r = $rval",
color="gray", line=(dash=:dash)
)
end
Out[5]:
In [6]:
let
# invariant density
# just plot the number of occurances in a histogram
rval = 4.0
Nmax = 5400
x_arr = zeros(Nmax)
iterate = x -> rval * x * (1-x)
# random start
x = rand()
for _ = 1:Nmax
x = iterate(x)
end
# start collection
for i1 in 1:Nmax
x = iterate(x)
x_arr[i1] = x
end
# analytic result for r -> 4
f = x -> 1 / sqrt(x * (1 - x)) / pi
histogram(x_arr, normalize = true, bins = 150, label="numerical", legend=:top)
plot!(0:0.01:1, f,
label="analytic (r=4)",
title="invariant density @ r = $rval",
color="gray", line=(dash=:dash)
)
end
Out[6]:
In [7]:
let
# idea: for each r, collect all iterations to xv,
# and use only the last N1 points (as End Points EPs)
# start from a random number and try traials times
N1 = 10
trials = 50
function xv_gen(r; N = max(20*N1, 2500))
# given r, iterates enough times,
# report the next N1 points
x = rand() # random start
f = x -> r * x * (1-x)
# iterates to EPs
for _ in 1:N
x = f(x)
end
# start collection
xarr = zeros(N1)
for i1 in 1:N1
xarr[i1] = x
x = f(x)
end
return xarr
end
rv = range(0.9, 4.01, length = 120)
# collect the end points for each r
fig = plot(legend=:topleft, xlabel="r", ylim=(0.0, 1.0))
for rr in rv, _ in 1:trials
scatter!(fig, fill(rr, N1), xv_gen(rr),
markersize = 1, color="black", alpha=0.4, label="")
end
plot!(rv, r -> 1-1/r, label="r - 1/r", color="red", line=(dash=:dash))
plot(fig, size=(600, 400))
end
Out[7]:
other map¶
$$ x_{n+1} = r \sin(\pi x_n) $$
In [8]:
let
N1 = 10
trials = 50
function xv_gen(r; N = max(20*N1, 2500))
# random start
x = rand()
f = x -> r * sin(pi*x)
# iterates to EPs
for i1 = 1:N
x = f(x)
end
# start collection
xarr = zeros(N1)
for i1 in 1:N1
xarr[i1] = x
x = f(x)
end
return xarr
end
rv = range(1/pi - 0.1, 1.0, length = 120)
# collect the end points for each r
fig = plot(legend=:topleft, xlabel="r", ylim=(0.0, 1.0))
# vary starting point
trials = 50
for rr in rv, _ in 1:trials
scatter!(fig, fill(rr, N1), xv_gen(rr),
markersize = 1, color="black", alpha=0.4, label="")
end
ana = r -> r > 1/pi ? sqrt((r*pi-1)*6/pi^3/r) : Inf
plot!(rv, ana, label="small x", color="red", line=(dash=:dash))
plot(fig, size=(600, 400))
end
Out[8]:
further reading¶
- runs this with more points offline
- read more in J.F. Boudreau and E.S. Swanson, Applied Computational Physics (ch.13)
- article: simple math models
iteration of matrix¶
In [9]:
let
using LinearAlgebra
N1, trials = 10, 50
function xv_gen(r; N = max(20*N1, 2500))
# random start
x = rand(2,2)
f = x -> r * sin(pi*x)
# iterates to EPs
for i1 = 1:N
x = f(x)
end
# start collection
xarr = zeros(N1)
for i1 in 1:N1
# record the trace of matrix
xarr[i1] = tr(x)
x = f(x)
end
return xarr
end
rv = range(0.25, 2.0, length = 120)
# collect the end points for each r
fig = plot(xlabel="r")
for rr in rv, _ in 1:trials
scatter!(fig, fill(rr, N1), xv_gen(rr),
markersize = 1, color="black", alpha=0.4, label="")
end
plot(fig, size=(600, 400))
end
Out[9]: